EchoDemo's Blogs

springmvc学习笔记(5)-入门程序小结

凉风起天末,君子意如何。

鸿雁几时到,江湖秋水多。

文章憎命达,魑魅喜人过。

应共冤魂语,投诗赠汨罗。

—-唐·杜甫《天末怀李白》

入门程序配置小结

1、前端控制器配置:

(1)第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析。

(2)第二种:/,所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析,使用此种方式可以实现RESTful风格的url。

2、处理器映射器:

(1)非注解处理器映射器(了解)。

(2)注解的处理器映射器(掌握)。

对标记@Controller类中标识有@RequestMapping的方法进行映射。在@RequestMapping里边定义映射的url。使用注解的映射器不用在xml中配置url和Handler的映射关系。

3、处理器适配器:

(1)非注解处理器适配器(了解) 。

(2)注解的处理器适配器(掌握) 。

注解处理器适配器和注解的处理器映射器是配对使用。理解为不能使用非注解映射器进行映射。

<mvc:annotation-driven></mvc:annotation-driven> 

可以代替下面的配置:

<!--注解映射器 -->  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  
<!--注解适配器 -->  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

非注解的完整的配置文件

src/main/resources/springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- 配置Handler -->
    <bean id="itemsController" name="/queryItems.action" class="com.echodemo.ssm.controller.ItemsController"/>

    <!-- 处理器映射器
    将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
    所有的映射器都实现HandlerMapping接口
    -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!-- 配置处理器适配器,所有处理器适配器都实现了HandlerAdapter接口,编写的Handler实现Controller接口 -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!-- 视图解析器
    解析jsp,默认使用jstl,classpath下要有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路径的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置jsp路径的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

或者:

<?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:mvc="http://www.springframework.org/schema/mvc"
       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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- 配置Handler -->
    <bean id="itemsController" name="/queryItems.action" class="com.echodemo.ssm.controller.ItemsController"/>
    <bean id="itemsController1" class="com.echodemo.ssm.controller.ItemsController1"/>

    <!-- 简单的url映射 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!-- 对itemsController进行url映射,key表示访问的路径,itemsController表示Handler的id -->
                <prop key="/queryItems0.action">itemsController</prop>
                <prop key="/queryItems1.action">itemsController1</prop>
            </props>
        </property>
    </bean>

    <!-- 另一个处理器适配器,编写的Handler实现HttpRequestHandler接口 -->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

    <!-- 视图解析器
    解析jsp,默认使用jstl,classpath下要有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路径的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置jsp路径的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

注:以上这两种不同的处理器和适配器的配置方式是可以并存的。


注解的完整配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
       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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- 对于注解的Handler 可以单个配置
    实际开发中建议使用组件扫描
    -->
    <!--<bean class="com.echodemo.ssm.controller.ItemsController2"/>-->
    <!-- 可以扫描controller、service、...
    这里让扫描controller,指定controller的包
     -->
    <context:component-scan base-package="com.echodemo.ssm.controller"></context:component-scan>

    <!-- 注解的映射器 -->
    <!--<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>


    <!-- 视图解析器
    解析jsp,默认使用jstl,classpath下要有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp路径的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置jsp路径的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
🐶 您的支持将鼓励我继续创作 🐶
-------------本文结束感谢您的阅读-------------