首页 > 代码库 > Spring Mvc 配置
Spring Mvc 配置
想要熟练的掌握Spring Mvc 配置,还是需要了解其运行原理。在java web里 Http请求最终都是Servlet来处理的,Spring Mvc 就是通过注册一个Servlet分发器来拦截http请求,然后进入到Srping Mvc框架进行处理,所以配置的第一步就是配置这个Servlet分发器。
一、在web.xml配置配置Servlet分发器和处理中文乱码的过滤器
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringMvc</display-name>
<!-- 处理中文乱码,只对POST请求有效 -->
<filter>
<filter-name>myEncoding</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>myEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC Servlet 分发器配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二、配置spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!--注解说明 -->
<mvc:annotation-driven />
<!-- 配置静态资源 -->
<mvc:resources mapping="/script/**" location="/script/" />
<mvc:resources mapping="/css/**" location="/css/" />
<!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->
<mvc:view-controller path="/" view-name="forward:/home/index"/>
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 使用jsp作为视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix" value="http://www.mamicode.com/jsp/"></property>
<property name="suffix" value="http://www.mamicode.com/.jsp"></property>
</bean>
<!-- 异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="http://www.mamicode.com/error/exception" />
<!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
<property name="exceptionAttribute" value="http://www.mamicode.com/ex"></property>
<property name="exceptionMappings">
<props>
<prop key="java.io.IOException">error/ioexception</prop>
</props>
</property>
</bean>
</beans>
这样配置后,Spring Mvc就可以跑起来了^_^