(不推荐使用)springMVC基本配置+controller层实现Controller接口
2017-04-12 17:26
549 查看
基本配置:
web.xml
spring-servlet.xml
controller
jsp
结果:
web.xml
<!-- 配置springMVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- contextConfigLocation 是固定的 --> <param-name>contextConfigLocation</param-name> <!-- 配置读取路径--> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 配置拦截规则--> <url-pattern>/</url-pattern> </servlet-mapping>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--配置url映射的类 --> <bean name="/test/hello" class="com.tgb.web.controller.HelloWorld" /> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
controller
//实现Controller接口 public class HelloWorld implements Controller { //重写handleRequest方法,spring-servlet.xml配置的映射默认会调用该重写的方法 @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception { Map<String,Object> map = new HashMap<String,Object>(); map.put("name", "丽华"); map.put("password", "123"); return new ModelAndView("/welcome","map",map); } }
jsp
<div> <c:forEach items="${map }" var="m"> ${m.key } ---- ${m.value } <br> </c:forEach> </div>
结果:
相关文章推荐
- (不推荐使用)springMVC基本配置+继承MultiActionController来实现根据参数名指定要请求的方法
- (推荐使用)SpringMVC注解,基本配置
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- (csdn内转载)使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)
- 使用springMVC实现文件上传和下载之环境配置与上传
- solr环境搭建&基本使用 - 程序灵感 solr服务与tomcat整合 solr使用配置步骤 solr使用 推荐分词工具 一.Solr服务与tomcat整合 1、solr
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- Springmvc中配置Quartz使用,实现任务实时调度。
- [置顶] RecycleView的基本配置和使用方法(四)---RecycleView实现简单demo
- Springmvc 拦截器的基本配置和使用
- CentOS 6.3_ RSync实现文件定时备份同步配置与使用 推荐
- 使用springmvc,jsp,结合网页文本编辑器kindEditor实现基本博客编辑功能
- 总结Unity IOC容器通过配置实现类型映射的几种基本使用方法
- WebWork之--使用fileUpload interceptor实现文件上传[基本配置+注意事项]
- 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
- Android——UIL实现图片缓存基本配置及使用
- 使用springMVC实现文件上传和下载之环境配置与上传
- 使用springmvc实现404,500的配置
- Unity IOC容器通过配置实现类型映射的几种基本使用方法