web下的WEB-INF里面的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_4_0.xsd" version="4.0">  
  <!--1.前端处理器配置-->  
  <!-- 设置web应用的上下文参数 -->  
  <context-param> 
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:applicationContext.xml</param-value> 
  </context-param>  
  <!--使用spring提供的监听器加载上下文的配置文件-->  
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener>  
  <!--配置spring mvc 的 前端控制器,拦截所有的 请求-->  
  <servlet> 
    <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>  
    <url-pattern>/</url-pattern> 
  </servlet-mapping>  
  <!-- 添加一個 編碼過濾器 解決亂碼 -->  
  <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>forceRequestEncoding</param-name>  
      <param-value>true</param-value> 
    </init-param>  
    <init-param> 
      <param-name>forceResponseEncoding</param-name>  
      <param-value>true</param-value> 
    </init-param> 
  </filter>  
  <filter-mapping> 
    <filter-name>CharacterEncodingFilter</filter-name>  
    <url-pattern>/*</url-pattern> 
  </filter-mapping> 
</web-app>

src跟目录下要有
applicationContext.xml和spring-mvc.xml 跟上面的web.xml里的内路径相对
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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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">  
  <!--配置springmvc的文件,包含 网站跳转!的逻辑相关的控制和配置 -->  
  <!--注解扫描 -->  
  <context:component-scan base-package="com.xiexin"/>  
  <!--配置视图解析器 -->  
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"/>  
    <property name="suffix" value=".jsp"/> 
  </bean>  
  <!-- 配置springmvc不能处理的请求交给tomcat -->  
  <!-- <mvc:default-servlet-handler/> -->
  <!--配置注解驱动,适用于更高级的注解的功能 -->  
  <mvc:annotation-driven/>  
  <!-- 对静态资源文件的访问 -->  
  <!-- <mvc:resources mapping="/img/**" location="/WEB-INF/img/" /> -->  
  <!-- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> -->  
  <!-- 静态资源配置 -->  
  <mvc:resources mapping="/res/**" location="/WEB-INF/res/"/>  
  <!-- 上传文件 -->  
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="defaultEncoding" value="utf-8"/>  
    <!-- 最大内存大小 -->  
    <property name="maxInMemorySize" value="10240"/>  
    <!-- 最大文件大小,-1为不限制大小-->  
    <!--// value"204800" 1024*200即200k-->  
    <property name="maxUploadSize" value="20480000"/> 
  </bean> 
</beans>

applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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>

整体如下

如果觉得我的文章对你有用,请随意赞赏
END
本文作者:
文章标题:ssm01练习
本文地址:https://c924.cn/index.php/archives/51/
版权说明:若无注明,本文皆天乐Blog原创,转载请保留文章出处。