在上一篇文章中web配置文件中指定了对应的dispatcher-servlet的对应的文件位置,接下来我们就对dispatcher-servlet.xml文件进行配置
在配置过程中会不断的向maven的pom.xml配置文件中引用对应的jar包
SpringMvc的配置文件的作用时包含网站跳转逻辑的控制
在配置写配置信息的时候需要在pom.xml文件中配置对应的引用
<!--spring SpringMVC Web包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
需要配置的信息:
1. 配置需要扫描的Controller的包
2.配置对应的视图解析器
3.配置两个标准代码
<!--两个标准的配置-->
<!--将springmvc不能处理的请求交给tomcat 如静态资源加载问题-->
<mvc:default-servlet-handler/>
<!--能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax 映射请求-->
<mvc:annotation-driven/>
附上对应的配置文件:
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
<!--SpringMCVC的配置文件,包含网站跳转逻辑的控制,配置-->
<context:component-scan base-package=
"org.spring"
use-
default
-filters=
"false"
>
<!--只扫描Controller-->
<context:include-filter type=
"annotation"
expression=
"org.springframework.stereotype.Controller"
></context:include-filter>
</context:component-scan>
<!--配置视图解析器 方便页面的返回-->
<!--<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>-->
<!--<property name=
"prefix"
value=
"/WEB-INF/templates"
></property>-->
<!--<property name=
"suffix"
value=
".html"
></property>-->
<!--</bean>-->
<bean id=
"layoutDialect"
class=
"nz.net.ultraq.thymeleaf.LayoutDialect"
></bean>
<!--thymeleaf视图解析器配置-->
<bean id=
"templateResolver"
class=
"org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"
>
<property name=
"prefix"
value=
"/WEB-INF/templates/"
/>
<property name=
"suffix"
value=
".html"
/>
<property name=
"templateMode"
value=
"HTML5"
/>
<property name=
"cacheable"
value=
"true"
/>
<property name=
"characterEncoding"
value=
"UTF-8"
/>
</bean>
<bean id=
"templateEngine"
class=
"org.thymeleaf.spring4.SpringTemplateEngine"
>
<property name=
"templateResolver"
ref=
"templateResolver"
/>
<property name=
"additionalDialects"
ref=
"layoutDialect"
/>
</bean>
<bean class=
"org.thymeleaf.spring4.view.ThymeleafViewResolver"
>
<property name=
"templateEngine"
ref=
"templateEngine"
/>
<property name=
"characterEncoding"
value=
"UTF-8"
/>
</bean>
<!--两个标准的配置-->
<!--将springmvc不能处理的请求交给tomcat 如静态资源加载问题-->
<mvc:
default
-servlet-handler/>
<!--能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax 映射请求-->
<mvc:annotation-driven/>
</beans>
|
下一篇时Spring配置文件的详解