spring boot + Thymeleaf开发web项目 视图解析

很多人,都知道spring boot怎么进行接口开发,今天我就来讲讲怎么实现页面跳转,完成一个web项目。我就借助Thymeleaf模板引擎来举例:

导入相关jar包(pom)

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.1.RELEASE</version>
	<relativePath />
</parent>
<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
</properties>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-configuration-processor</artifactId>
		<optional>true</optional>
	</dependency>
	<!-- thymeleaf模板引擎 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
</dependencies>

你用spring-boot-starter-parent是2.0以上时,对应的thymeleaf是3.0以上的版本,相对较新,不要改。如果你用的是spring-boot-starter-parent 1.5,对应的thymeleaf是2.0以上的版本,建议用thymeleaf3.0的。

包选择为:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>  <!-- 如果spring是5:thymeleaf-spring5  -->
    <version>3.0.9.RELEASE</version>
</dependency>

静态资源文件映射规则

根据:WebMvcAutoConfiguration→addResourceHandlers→ResourceProperties
可知道:spring boot 默认的静态资源路径如下:

1、classpath:/
2、classpath:/META-INF/resources/
3、classpath:/resources/
4、classpath:/static/
5、classpath:/public/

也就是说可以直接访问以上路径中的文件。
如:
     
访问地址:http://127.0.0.1:8080/css/login.css (不需要加resources、static、public...)

thymeleaf默认的视图解析

可以看出,初始化的默认页面路径在classpath:/templates/ 下,视图后缀为.html。也可以根据自己的喜号在application.yml文件中 指定spring.thymeleaf.** =** 来指定值

实现请求URL

我就不改动了,采用默认值

/**
 * @描述 	用户相关页面请求
 * @注意		此类只能用@Controller 不能使用@RestController
 * @author  BianP
 */
@Controller  
@RequestMapping("/user")
public class UserWebController {
	/**
	 * @explain 用户登录《GET》
	 * @return  String
	 * @author  BianP
	 */
	@RequestMapping(value="/toLogin", method = RequestMethod.GET)
	public String toLogin(Model model){
		model.addAttribute("welcome", "欢迎登录");
		return "login";
	}
}

目录结构

请求结果:

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3eya265e9u68g

转载于:https://my.oschina.net/bianxin/blog/1816920

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Spring BootThymeleaf实现拦截器的步骤: 1.创建一个拦截器类,该类需要实现HandlerInterceptor接口。在该类中,您可以实现三个方法:preHandle,postHandle和afterCompletion。这些方法分别在处理程序执行之前,之后和完成之后调用。 ```java import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前进行调用(Controller方法调用之前) System.out.println("请求处理之前调用……"); return true; // 如果返回false,则请求中断 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) System.out.println("请求处理之后调用……"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在视图被渲染之后调用(主要用于资源清理工作) System.out.println("视图渲染之后调用……"); } } ``` 2.将拦截器注册到应用程序中。在Spring Boot应用程序中,您可以通过使用WebMvcConfigurerAdapter类来注册拦截器。在该类中,您可以重addInterceptors方法,并将您的拦截器添加到拦截器链中。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebAppConfiguration extends WebMvcConfigurerAdapter { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor); } } ``` 3.使用Thymeleaf进行视图渲染。在您的控制器方法中,您可以返回一个包含视图名称和模型对象的ModelAndView对象。在视图中,您可以使用Thymeleaf模板来渲染HTML内容。在下面的示例中,我们将使用layout.html作为页面布局,并使用th:replace指令将内容注入到该布局中。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>My Page</title> </head> <body> <div th:replace="layout :: content"> <h1 th:text="${message}">Hello, World!</h1> </div> </body> </html> ``` 4.创建一个包含布局的Thymeleaf模板。在该模板中,您可以定义页面的基本结构和样式,并使用th:fragment指令定义页面的各个部分。在下面的示例中,我们将使用layout.html作为页面布局,并使用th:fragment指令定义页面的内容部分。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${pageTitle}">My Site</title> </head> <body> <div th:fragment="content"></div> </body> </html> ``` 以上就是使用Spring BootThymeleaf实现拦截器的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值