SpringMVC学习

来源:互联网 发布:畅言通教学软件 编辑:程序博客网 时间:2024/06/15 21:41

刚开始学习SpringMVC查了些资料,把配置总结下,方便自己再次使用。不喜勿喷,欢迎指正。

一.SpringMVC介绍

SpringMVC是web开发的常用框架,基于model-view-controller模型实现的,实现了web应用的解耦。

过程:DispatcherServlet,一个前端控制器,底层就是servlet,主要任务是将请求发送到SpringMVC的控制器(Controller),通过查询处理器映射(handler mapping),hangler mapping 根据请求携带的url来确定下一站。控制器完成逻辑处理后,将模型(model)格式化后(如html格式),在加上所需的视图(view)名返回给DispatcherServlet,从而实现与视图的解耦。DispatcherServlet通过视图解析器(view resolver)将逻辑视图名匹配到特定视图,视图将模型数据渲染后通过响应对象返回给客户端。

二.SpringMVC的配置

1.web.xml配置:

<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/applicationContext.xml</param-value></context-param><!-- Spring配置 --><!-- ====================================== --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><listener><listener-class>org.springframework.web.util.WebAppRootListener</listener-class></listener><!-- 字符集--><filter><filter-name>encodingFilter</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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 配置DispatchcerServlet --><servlet><servlet-name>ServletName</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>ServletName</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 默认首页--><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
注意:

①<Servlet>和<servlet-mapping>中的<servlet-name>属性必须保持一致

②<load-on-startup>定义severlet加载的顺序,数字越小越早

③<context-param>指定了Spring配置文件的位置

④SpringMVC的配置文件默认为WEB-INF/servletName-servlet.xml,如果需要更改位置可以再<servlet>中的

 

<init-param>       <param-name>contextConfigLocation</param-name>          <param-value>配置文件位置</param-value>     </init-param>

配置。

⑤<servlet-mapping>中的<url-pattern>/</url-pattern>指定为所有请求都拦截,也可根据需要配置,如*.do--拦截以.do结尾的url路径请求,/*等,由于“/”拦截了所有的请求,因此需要配置静态资源的加载(css,js等),否着无法加载。可以再springMVc配置文件中添加<mvc:default-servlet-handler/>。

2.springMVC配置文件的配置

<?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:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 自动扫描且只扫描@Controller --><context:component-scan base-package="全路径包名(controller所在包)" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan><mvc:annotation-driven><mvc:message-converters register-defaults="true">            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    <property name="objectMapper" ref="customObjectMapper"></property>    </bean><!-- 将StringHttpMessageConverter的默认编码设为UTF-8 --><bean class="org.springframework.http.converter.StringHttpMessageConverter">    <constructor-arg value="UTF-8" />    <property name="supportedMediaTypes">                <list>                    <value>text/plain;charset=UTF-8</value>                    <value>text/html;charset=UTF-8</value>                </list>            </property></bean><!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true --><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <property name="prettyPrint" value="true"/>            </bean>  </mvc:message-converters></mvc:annotation-driven><!-- 定义JSP文件的位置 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean><!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL--><mvc:default-servlet-handler/><mvc:resources mapping="/views/**/*.js" location="/WEB-INF/views/" /><mvc:resources mapping="/static/**" location="/static/" /></beans>




0 0
原创粉丝点击