srping搭建步骤:javascript
配置web项目中的web.xml文件:html
(必须配置)前端控制器(DispatcherServlet),DispatcherServlet是spring的核心部件前端
乱码过滤器(CharacterEncodingFilter),通常也都会配置上用于解决post提交是的乱码问题;乱码问题总结见:http://my.oschina.net/u/1757476/blog/411158java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 若是这里不配置contextConfigLocation,会自动找"servlet名称+-servlet.xml"--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post提交解决乱码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
配置springmvc-servlet.xml,spring重要组件:映射器Mapping、适配器Adapter以及视图解析器ViewResolverweb
Mapping配置:spring
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/
Adapter配置,消息转换器下一篇再述。json
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!-- 定义消息转换器 --> <property name="messageConverters"> <ref bean="jsonHttpMessageConverter"/> </property> </bean>
ViewResolver配置:其中prefix和suffic是其spring-mvc
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean>
总体springmvc-servlet.xml:session
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 组件扫描 --> <context:component-scan base-package="springmvc.action" /> <!-- 可使用注解驱动代替下面配置的映射器和适配器 --> <!-- <mvc:annotation-driven /> --> <!-- Annotation Mapping--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- Annotation Adapter--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!-- 定义消息转换器 --> <property name="messageConverters"> <ref bean="jsonHttpMessageConverter"/> </property> </bean> <!-- 消息转换器 --> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <!-- <property name="supportedMediaTypes"> <list> <value>text/javascript;charset=UTF-8</value> </list> </property> --> </bean> <!-- View Resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- hello Controller --> <!-- <bean class="springmvc_annotation.action.HelloWorldController"/> --> <!-- 拦截器 --> <!-- 多个拦截器,顺序执行 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.filter.LoginInterceptor"></bean> </mvc:interceptor> <!-- <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.filter.HandlerInterceptor1"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="springmvc.filter.HandlerInterceptor2"></bean> </mvc:interceptor> --> </mvc:interceptors> </beans>
若是须要配置的controller比较多,能够采用组件扫描的方式:在springmvc-servlet.xml中添加mvc
<!-- 组件扫描 --> <context:component-scan base-package="springmvc.action" />
上述的Mapping和Adapter能够直接写成注解驱动代替以上配置:
<!-- 可使用注解驱动代替下面配置的映射器和适配器 --> <mvc:annotation-driven />
Controller代码部分:
在Controller类上须要标注@Controller注解,这样组建扫描才可以扫描的到。
Controller方法上须要标注@RequestMapping(value = "/userAddSubmit", method = { RequestMethod.POST, RequestMethod.GET }),method是限定请求方法,能够不写。
@Controller @RequestMapping("/user") //模块路径 public class UserAction { /** * 用户列表 * groupId是用户类型,进入查询页面默认显示那个用户类型 * @return */ @RequestMapping(value="/userList") public String userList(HttpServletRequest request, HttpServletResponse response, H ttpSession session, Model model, Map map, ModelMap modelMap, @RequestParam (defaultValue="2", value="group", required=true) String groupId) { return "user/userList"; //返回页面 }
详细见附件文档 springmvc.docx。