• sring mvc web.xml以及servlet配置


    1. web.mxl配置

    <?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="WebApp_ID" version="2.5">
        
         <!-- 在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?
        下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter  
         这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。  
          其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。-->  
          
        <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>  
        
        <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet,,所以可以配置多个DispatcherServlet-->  
        <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。--> 
        <!--加载配置文件-->
         <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/spring-servlet.xml,
                    classpath:spring/spring-context.xml
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup><!--是启动顺序,让这个Servlet随Servletp容器一起启动。-->  
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>        
        </servlet-mapping>
            <!--如果你的DispatcherServlet拦截"/",为了实现REST风格,拦截了所有的请求,那么同时对*.js,*.jpg等静态文件的访问也就被拦截了。-->  
        <!--方案一:激活Tomcat的defaultServlet来处理静态文件-->  
        <!--要写在DispatcherServlet的前面, 让 defaultServlet先拦截请求,这样请求就不会进入Spring了,我想性能是最好的吧。-->  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.css</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.swf</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.gif</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.jpg</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.png</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.js</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.html</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.xml</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.json</url-pattern>  
        </servlet-mapping>  
        <servlet-mapping>  
            <servlet-name>default</servlet-name>  
            <url-pattern>*.map</url-pattern>  
        </servlet-mapping>
     
    </web-app>

    2.spring-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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/aop 
            http://www.springframework.org/schema/aop/spring-aop.xsd ">
        
        <!-- 扫描的基本包路径 -->
        <context:component-scan base-package="com.hshbic.cloudapp" />
        
        <!-- 开启切面注解 -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        
        <!-- 解析给前台传java对象 -->
       <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
        
        <mvc:resources location="/js/" mapping="/js/**"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        
    </beans>

    3. spring-context.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:tx="http://www.springframework.org/schema/tx" 
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd 
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util.xsd">
            
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                <!--资源文件默认扫描的跟目录是 src/main/resources-->
                    <value>classpath:server.properties</value>
                </list>
            </property>
        </bean>
    
    </beans>
  • 相关阅读:
    2019 SDN上机第3次作业
    SDN第二次作业
    软件定义网络第一次作业
    软件工程实践第五次作业
    软件工程实践2019第四次作业------原型设计
    2019 SDN上机第7次作业
    2019 SDN上机第6次作业
    SDN课程阅读作业(2)
    软件工程实践个人作业——软件评测
    2019 SDN上机第5次作业
  • 原文地址:https://www.cnblogs.com/peak911/p/9155398.html
Copyright © 2020-2023  润新知