zoukankan      html  css  js  c++  java
  • 淘淘商城之springmvc前端控制器

    一、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_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>springwebmvc</display-name>
        
        <!-- springmvc前端控制器 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 
                contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不
                配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) 
            -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!--
                     第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析;  
                     第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不
                              让DispatcherServlet进行解析 使用此种方式可以实现 RESTful风格的url;
                     第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 
                    仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错
            -->
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
        
        
        <!-- <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list> -->
    </web-app>

    二、springmvc.xml

    <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" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
        
        <!-- 配置第一种Handler,ItemsController1需要实现Controller接口-->
        <!-- <bean id="itemsController1" name="/queryItems.action" class="com.chuanye.ssm.controller.ItemsController1" /> -->
        <!-- 配置第二种Handler,ItemsController2需要实现HttpRequestHandler接口 -->
        <!-- <bean id="itemsController2" class="com.chuanye.ssm.controller.ItemsController2" /> -->
        
        <!-- 对于注解的Handler可以单个配置; 实际开发中建议使用组件扫描-->
        <bean class="com.chuanye.ssm.controller.ItemsController3" />
        <!-- 可以扫描controller、service等; 这里让扫描controller,指定controller的包 -->
        <!-- <context:component-scan base-package="com.chuanye.ssm.controller" /> -->
        
        <!-- 《非注解映射器1》 -->
        <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    -->
        <!-- 《非注解映射器2》 -->
        <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    对itemsController1进行url映射,url是/queryItems1.action
                    <prop key="/queryItems1.action">itemsController1</prop>
                    <prop key="/queryItems2.action">itemsController1</prop>
                    <prop key="/queryItems3.action">itemsController2</prop>
                </props>
            </property>
        </bean> -->
        
        
        <!-- 《注解映射器》 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
        <!-- 《注解适配器》 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
        <!-- 
            使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
            mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换
            解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的
            RequestMappingHandlerMapping和RequestMappingHandlerAdapter
            实际开发时使用mvc:annotation-driven
         -->
        <mvc:annotation-driven></mvc:annotation-driven>
        
        <!-- 《非注解适配器1》 -->
        <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> -->
        <!-- 《非注解适配器2》 -->
        <!-- <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> -->
        
        <!-- 《视图解析器》 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
            <!-- 配置jsp路径的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 配置jsp路径的后缀 -->
            <property name="suffix" value=".jsp"/>
        </bean>
        
    </beans>
  • 相关阅读:
    [BZOJ5338][TJOI2018]xor(可持久化Trie)
    [BZOJ4592][SHOI2015]脑洞治疗仪(线段树)
    [BZOJ4571][SCOI2016]美味(贪心+主席树)
    [BZOJ4570][SCOI2016]妖怪(凸包)
    [BZOJ4569][SCOI2016]萌萌哒(倍增+并查集)
    [BZOJ4567][SCOI2016]背单词(Trie+贪心)
    [BZOJ4565][HAOI2016]字符合并(区间状压DP)
    [BZOJ4561][JLOI2016]圆的异或并(扫描线)
    [BZOJ2650]积木
    [清橙A1210]光棱坦克
  • 原文地址:https://www.cnblogs.com/soldierback/p/10614302.html
Copyright © 2011-2022 走看看