springmvc注解开发(Annotation)

srping搭建步骤:

  1. 配置web项目中的web.xml文件:

    1. (必须配置)前端控制器(DispatcherServlet),DispatcherServlet是spring的核心部件

    2. 乱码过滤器(CharacterEncodingFilter),一般也都会配置上用于解决post提交是的乱码问题;乱码问题总结见:http://my.oschina.net/u/1757476/blog/411158

    <?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>

  2. 配置springmvc-servlet.xml,spring重要组件:映射器Mapping、适配器Adapter以及视图解析器ViewResolver

    1. Mapping配置:

    2. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/
    3. Adapter配置,消息转换器下一篇再述。

    4. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
          <!-- 定义消息转换器 -->
          <property name="messageConverters">
      	<ref bean="jsonHttpMessageConverter"/>
          </property>
      </bean>
    5. ViewResolver配置:其中prefix和suffic是其

    6. <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:

      <?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中添加

    <!-- 组件扫描 -->
    <context:component-scan base-package="springmvc.action" />

    上述的Mapping和Adapter可以直接写成注解驱动代替以上配置:

    <!-- 可以使用注解驱动代替下面配置的映射器和适配器 -->
    <mvc:annotation-driven />
  3. Controller代码部分:

    1. 在Controller类上需要标注@Controller注解,这样组建扫描才能够扫描的到。

    2. Controller方法上需要标注@RequestMapping(value = "/userAddSubmit", method = { RequestMethod.POST, RequestMethod.GET }),method是限定请求方法,可以不写。

    3. @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。

你可能感兴趣的:(springmvc注解开发(Annotation))