使用spring MVC也就意味着要在java Web项目中进行配置,java Web项目的核心配置文件是web.xml,要使用spring和springMVC框架同样需要在web.xml中进行配置说明,这样才能使spring和springMVC框架在项目中生效。下面进行对配置web.xml、spring-application.xml、spring-mvc.xml三个配置文件进行简单介绍和展示。
一、web.xml
<?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"
version="2.5">
<!-- spring configuration begin-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-application.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- spring configuration end -->
<!-- spring MVC core configuration begin -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- spring MVC core configuration end -->
<error-page>
<error-code>404</error-code>
<location>/404.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.xhtml</location>
</error-page>
<welcome-file-list>
<welcome-file>/welcome.xhtml</welcome-file>
</welcome-file-list>
</web-app>
二、spring-application.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
</beans>
三、spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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.xsd">
<context:component-scan base-package="com.zyiot.controller" />
<!-- configuration view resolver -->
<!-- 注解支持 -->
<!--context:annotation-config/>-->
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven />
<mvc:resources location="/" mapping="/**/*.js" />
<mvc:resources location="/" mapping="/**/*.css" />
<mvc:resources location="/" mapping="/**/*.ioc" />
<mvc:resources location="/" mapping="/**/*.svg" />
<mvc:resources location="/" mapping="/**/*.png" />
<mvc:resources location="/" mapping="/**/*.gif" />
<mvc:resources location="/" mapping="/**/*.xml" />
<mvc:interceptors>
<mvc:interceptor>
<!-- 需拦截的地址 -->
<!-- 一级目录 -->
<mvc:mapping path="/*.xhtml" />
<mvc:mapping path="/*.jsp" />
<!-- 二级目录 -->
<mvc:mapping path="/*/*.xhtml" />
<mvc:mapping path="/*/*.jsp" />
<!-- 需排除拦截的地址 -->
<mvc:exclude-mapping path="/welcome.xhtml" />
<bean class="com.zyiot.interceptor.SystemInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.zyiot.interceptor.TokenInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
四、SystemInteceptor.java
package com.zyiot.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class SystemInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
return true;
}
}
注:需要的Spring jar包
1、spring-beans
2、spring-context
3、spring-core
4、spring-expression
5、spring-tx
6、spring-web
7、spring-webmvc