美文网首页
Spring MVC

Spring MVC

作者: CupricNitrate | 来源:发表于2019-07-29 16:43 被阅读0次

    web.xml

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id="WebApp_ID" version="3.1">
     ...
    
      <!-- 这个内容是为了解决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>
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <!-- 解决中文乱码的过滤器部分编写结束 -->
    
      <!-- 这个 servlet 能够让项目应用到 Spring MVC,非常重要,一定要写 -->
      <servlet>
        <!-- 为 servlet 起一个名字,可自己定义 -->
        <servlet-name>springmvc</servlet-name>
        <!-- 固定写法,不可修改 -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <!-- 制定 Spring MVC 配置文件的路径 -->
          <param-name>contextConfigLocation</param-name>
          <!-- 将路径指定为了 resources 中的 spring-mvc.xml -->
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
      </servlet>
    
      <servlet-mapping>
        <!-- 为 servlet-mapping 起一个名字,与上面的保持一致 -->
        <servlet-name>springmvc</servlet-name>
        <!-- 将路径映射定义为/就表示所有的请求都会交给 Spring MVC 处理 -->
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    

    在resources中的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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           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.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- 指定扫描路径,在该包路径中的注解能够被 Spring 扫描到,从而实现对应功能 -->
        <context:component-scan base-package="online.shixun.project.controller" />
    
        <!-- 指定视图解析的一些内容 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 将默认的视图文件存放路径定义在 WEB-INF/views/ 这里了 -->
            <property name="prefix" value="WEB-INF/views/" />
            <!-- 将默认的视图文件定义成了 .jsp -->
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- 构建一个默认的 Serlvet 来额外的处理一切静态资源 -->
        <mvc:annotation-driven />
        <mvc:default-servlet-handler />
    
    </beans>
    

    注意这个:


    SpringMvc.png

    我这里的路径是“/”所以视图路径这样设置

            <property name="prefix" value="WEB-INF/views/" />
    

    否则要在前面加你的设置的路径

    相关文章

      网友评论

          本文标题:Spring MVC

          本文链接:https://www.haomeiwen.com/subject/zujjrctx.html