spring mvc基础环境thymeleaf模板引用(一)
spring mvc基础环境配置
1.引入必要的依赖 .
2.配置springmvc
- web.xml配置 默认的 DispatcherServlet
- classPath根目录创建对应的spring-mvc配置文件
3.写一个,测试类
1. pom.xml 文件引入必要的依赖:
(spring-webmvc引用即可,它可以再找上级依赖)
<!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- Spring5和Thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency>
2.配置 spring-mvc.xml 配置文件 (可以在resources建立,编译后到根目录)
<!--扫描组件--> <context:component-scan base-package="com.cc8w.controller"></context:component-scan> <!-- 开启mvc的注解驱动,此时即便配置了 view-controller,controller层的控制器方法的请求映射也不会失效 --> <mvc:annotation-driven/> <!--配置处理器映射器--> <!--spring 3.1版本之前--> <!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>--> <!--spring 3.1版本之后--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>--> <!--配置处理器适配器--> <!--spring 3.1版本之前--> <!--<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>--> <!--spring 3.1版本之后--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>--> <!--配置视图解析器--> <!--bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean--> <!--配置视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean>
web.xml配置默认的 Servlet
<!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
3.试一下
package com.cc8w.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("index") public class IndexController { @RequestMapping("index01") public String index01() { return "index01"; } }

浙公网安备 33010602011771号