[Spring-4.3.14]
配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
......
</web-app>
配置 web.xml 设置欢迎界面(默认初始界面)
<welcome-file-list>
<welcome-file>[URL]</welcome-file>
</welcome-file-list>
配置 web.xml 解决中文问题
<!--处理从页面传递中文到后台而出现的中文乱码问题 -->
<!-- encoding start -->
<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>
<!-- encoding ends -->
配置 web.xml 设置主控制器
<!-- dispatcher 可以随便取,但要与下面 <servlet-mapping> 对应 -->
<!-- <param-value> 为主控制器配置文件的目录 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置主控制器
[dispatcher-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: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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
......
</beans>
扫描含有注解的类
<!-- 自动扫描 tickets 下或子包下的 Java 文件,如果扫描到有 @Component @Controller @Service 等注解的类,则把这些类注册为 Bean -->
<context:component-scan base-package="tickets"/>
<!-- 配置注解式处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解式处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
解决服务器返回界面中文问题,设置为UTF-8
<!-- 配置上面的 注解式处理器设配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property>
</bean>
解决界面无法访问静态资源,例如:js / css
<!-- location 是文件夹目录,mapping 是设置文件夹下匹配内容 -->
<mvc:resources location="WEB-INF/stylesheet/" mapping="/stylesheet/**" />
<mvc:resources location="WEB-INF/javascript/" mapping="/javascript/**" />
配置视图解析器
<!-- 配置视图解析器(对jsp默认解析的视图解析器) -->
<!-- 例如页面目录是 /WEB-INF/view/index.jsp, 如下配置后 Controller 中直接返回 return "index"; -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix:前缀 -->
<property name="prefix" >
<value>/WEB-INF/view/</value>
</property>
<!-- suffix:后缀 -->
<property name="suffix" >
<value>.jsp</value>
</property>
</bean>