SpringMVC之context-dispatcher.xml,了解基本的控制器

简介: SpringMVC之context-dispatcher.xml,了解基本的控制器

本篇来完成SpringMVC的三大XML的最后一个context-dispatcher.xml,了解spring如何控制视图、如何扫描注解、数据转换、静态资源加载等关键配置项。


另外附上前两个xml的配置链接:


SpringMVC之application-context.xml

SpringMVC之web.xml


①、xml内容

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
     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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd"
       >
       <!-- 视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- 将jsp页面放置在web-info中可以保护这些页面不被浏览器直接访问 -->
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 扫描web包,应用Spring的注解 @Controller -->
    <!-- 具体的包可以使spring在加载时不扫描没有必要的包 -->
    <context:component-scan base-package="com.honzh.spring.controller" />
    <!-- 扫描业务层service实现类 -->
    <context:component-scan base-package="com.**.service" />
    <mvc:annotation-driven conversion-service="conversionService" />
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <!-- 这里使用string to date可以将dao在jsp到controller转换的时候直接将string格式的日期转换为date类型 -->
                <bean class="com.honzh.common.plugin.StringToDateConverter" />
            </list>
        </property>
    </bean>
    <mvc:resources location="/assets/" mapping="/assets/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/components/" mapping="/components/**"/>
    <!-- 以上和下面这句话等效 -->
<!--    <mvc:default-servlet-handler/> -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8">
        <property name="maxUploadSize" value="1024000000"></property>
    </bean>
</beans>  


②、重点内容介绍


1、viewResolver


      <!-- 视图解析器 -->

   <bean id="viewResolver"

       class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <property name="viewClass"

           value="org.springframework.web.servlet.view.JstlView" />

       <!-- 将jsp页面放置在web-info中可以保护这些页面不被浏览器直接访问 -->

       <property name="prefix" value="/WEB-INF/pages/" />

       <property name="suffix" value=".jsp"></property>

   </bean>

1

2

3

4

5

6

7

8

9

将jsp页面放置在web-info中可以保护这些页面不被浏览器直接访问。


通过prefix、suffix我们在controller中的跳转地址就可以简化了,比如说原来的“/WEB-INF/pages/login.jsp”就可以简化为“/login”。

2、context:component-scan


<!-- 扫描web包,应用Spring的注解 @Controller -->
    <!-- 具体的包可以使spring在加载时不扫描没有必要的包 -->
    <context:component-scan base-package="com.honzh.spring.controller" />
    <!-- 扫描业务层service实现类 -->
    <context:component-scan base-package="com.**.service" />


通过context:component-scan,spring就会扫描base-package下面带有Spring注解的类(@Controller、@Service),这样一来,当你url进行请求的时候,就会找到对应的controller控制器,在controller需要的时候,也会找到对应的service。


当然了,base-package的配置方式就有两种,一种是全路径,一种是带*的省略路径,不过一定要具体到对应的包下面,否则spring会扫描不必要的包路径以及类。


3、mvc:annotation-driven


<mvc:annotation-driven conversion-service="conversionService" />
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <!-- 这里使用string to date可以将dao在jsp到controller转换的时候直接将string格式的日期转换为date类型 -->
                <bean class="com.honzh.common.plugin.StringToDateConverter" />
            </list>
        </property>
    </bean>


转换器converter是可以将一种类型转换为另外一种类型的,例如上述配置中string到date类型的转换,StringToDateConverter 类如下,其转换的日期格式为”yyyy-MM-dd”,如果页面上输入的日期格式不是”yyyy-MM-dd”,那么尤其是在form表单提交到model时,就会抛出错误。另外,一般model的bean中对于日期类,其类型为Date,而前端jsp传入的类型为string,有了该converter,就可以轻松将string转换为bean了,就不需要特殊的处理了。

package com.honzh.common.plugin;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class StringToDateConverter implements Converter<String,Date>{
    public Date convert(String source) {
        DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateTimeFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

4、mvc:resources和mvc:default-servlet-handler


还记得我在介绍web.xml时说过“/”的静态资源问题了吗?那么当url-pattern中配置为“/”,默认的DispatcherServlet会处理所有的请求,包含静态资源,而事实显然不需要这样。


image.png


webContent的目录如上图所示,对于assets和css文件夹,显然我们不希望servlet再进行拦截,否则,这些资源将无法呈现,那么如何解决这个问题呢?


   <mvc:resources location="/assets/" mapping="/assets/**"/>

   <mvc:resources location="/css/" mapping="/css/**"/>

   <mvc:resources location="/components/" mapping="/components/**"/>


   <!-- 以上和下面这句话等效 -->

<!--    <mvc:default-servlet-handler/> -->


以上两种方案供解决,二者选其一。至于更细节的内容,王二我能力有限,就略过了。


5、multipartResolver


   <bean id="multipartResolver"

       class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8">

       <property name="maxUploadSize" value="1024000000"></property>

   </bean>


这显然是控制文件上传的,我这里先不做详细的介绍,稍候我会用一整篇的文章来进行说明。


有关SpringMVC的三大XML(web.xml、application-context.xml、context-dispatcher.xml)暂时就先介绍到这里,后续遇到新的问题,会重新把问题添加到系列文章中,为后来者铺路,感觉很欣慰啊。


相关文章
|
前端开发 Java 数据格式
SpringMVC之context-dispatcher.xml,了解基本的控制器
版权声明:欢迎转载,请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/50895295 本篇来完成SpringMVC的三大XML的最后一个context-dispatcher.xml,了解spring如何控制视图、如何扫描注解、数据转换、静态资源加载等关键配置项。
789 0
|
5月前
|
设计模式 前端开发 JavaScript
Spring MVC(一)【什么是Spring MVC】
Spring MVC(一)【什么是Spring MVC】
|
2月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
16 0
|
7月前
|
前端开发 Java Go
Spring MVC 和 Spring Boot 的区别
Spring MVC 和 Spring Boot 的区别
114 0
|
10月前
|
Java Spring
springmvc中spring提供的中文乱码解决问题
可以解决浏览器的乱码问题
44 0
|
5月前
|
设计模式 前端开发 Java
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
48 0
|
10月前
|
JSON 前端开发 Java
"《图书管理系统》利用SpringMvc$Spring$MyBatis (实操九)(一) "
"《图书管理系统》利用SpringMvc$Spring$MyBatis (实操九)(一) "
58 0
|
5月前
|
设计模式 前端开发 Java
8:Spring MVC-Java Spring
8:Spring MVC-Java Spring
74 0
|
9月前
|
前端开发 Java 应用服务中间件
SpringMVC(基于Spring 的Web 层MVC 框架)--SpingMVC 执行流程--@RequestMapping的使用
SpringMVC(基于Spring 的Web 层MVC 框架)--SpingMVC 执行流程--@RequestMapping的使用
99 0
|
10月前
|
SQL JSON 前端开发
SpringMvc+Spring+MyBatis+Maven+Ajax+Json注解开发 利用Maven的依赖导入不使用架包模式 (实操十二)
SpringMvc+Spring+MyBatis+Maven+Ajax+Json注解开发 利用Maven的依赖导入不使用架包模式 (实操十二)
46 0