SpringBoot处理请求并以HTML形式返回结果 - Go语言中文社区

SpringBoot处理请求并以HTML形式返回结果


一、 首先添加pom依赖

 <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

二、在resources建立一个templates目录(也可以创建子目录pages)和index.htm静态文件
在这里插入图片描述

三、打开application.properties,添加如下配置:
spring.thymeleaf.prefix=classpath:/templates/pages/

Contorller处理请求,返回对应的HTML:

//以request属性传值,对应key,value
@Controller
@RequestMapping(“hello”)
public class indexController {
@GetMapping("/index")
public String test(HttpServletRequest request){
request.setAttribute(“hello”, “Welcome to the first one SpringBoot HTML”);
return “index”;
}

//以HashMap方式传值
@Controller
@RequestMapping(“hello”)
public class indexController {
@GetMapping("/index")
public String test(HashMap<String, Object> map){
map.put(“hello”, “Welcome to the first one SpringBoot HTML”);
return “index”;
}

HTML展示代码
在这里插入图片描述

红线部分是接收Controller传值的结果

访问http://localhost:8080/hello/index

在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_43433164/article/details/83993823
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:47:53
  • 阅读 ( 1983 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢