淘淘商城之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>

转载于:https://www.cnblogs.com/soldierback/p/10614302.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值