前段控制器

基于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"
    id="jt-manage" version="2.5">
    <display-name>jt-manage</display-name>

    <!-- 配置前段控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化springmvc的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring/applicationContext*.xml</param-value>
        </init-param>
        <!-- 当web服务器启动时加载该servlet,实例化servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 请求路径
        后缀型的路径:  *.do  只拦截.do结尾的路径
        前缀型路径:/service/*  只拦截请求一service开头的
        全路径:/service/*.do
        /   :前缀型路径  表示拦截全部测请求和静态资源(html/css/js/图片...),放行动态资源(.jsp)
        /*  :任何请求和资源都拦截
     -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 乱码处理问题 主要解决的是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>

applicationContext-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: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/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.xsd">

    <!-- 开启mvc注解 -->
    <mvc:annotation-driven />

    <!-- 放行静态资源 -->
    <mvc:default-servlet-handler/>

    <!--配置视图解析器  内部资源视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 切记引用绝对路径 否者会出现路径跳转问题 -->
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

全注解形式配置的视图解析器

@Configuration//下面写的时候可以省略
@ComponentScan(value="com.jt",
includeFilters={@Filter(classes={Controller.class,ControllerAdvice.class},
type=FilterType.ANNOTATION)},//这个type不指定时会出现无效的参数异常,要指定上边的为注解类
useDefaultFilters=false)
@EnableWebMvc//启用mvc默认配置()
public class AppServletConfig extends WebMvcConfigurerAdapter {

    /*配置视图解析器*/
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {

        registry.jsp("/WEB-INF/pages/", ".html");
    }

猜你喜欢

转载自blog.csdn.net/qq1083273166/article/details/82589992