文章目录
html
在pom.xml文件中添加依赖
<!-- thymeleaf模板引擎依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在main/resources/templates下建一个html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>yyyy</title>
</head>
<body>
<h1 th:text="${message}">333</h1>
<h1>hello mmmm</h1>
</body>
</html>
在main/java/com.springboot.demo建一个控制层Controller/hello.java
@Controller
@RequestMapping("/hh")
public class hello {
@RequestMapping("/aa")
public String hello(){
return "hello";
}
@RequestMapping("/hello1")
public String hello1(Model model){
model.addAttribute("message","http:/www.ityouknow.com");
return "hello";
}
- 这个时候只需要在浏览器上打上localhost:8080/hh/aa或者localhost:8080/hh/hello1即可
jsp
也需要在pom.xml中导入依赖
</dependency>
<!--配置支持jsp(JSP和Thymeleaf都属于View层技术,二者二选其一)如果这两个依赖共存时,idea会优先选择thymeleaf 不再支持jsp的页面的展示-->
<!--<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>-->
<!-- thymeleaf模板引擎依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在main下建/webapp/WEB-INF/index.jsp
这个时候webapp文件夹是灰色的,需要按住ctrl+alt+shift+s键配置一些东西来点亮它
接着在resources下/templates的application.properties添加前缀和后缀
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
在Controller层的hello.java中
@Controller
@RequestMapping("/hh")
public class hello {
@RequestMapping("/bb")
public String hello2(Model model){
model.addAttribute("message","http:/www.ityouknow.com");
return "index";
}
}
- 在浏览器中输入localhost:8080/hh/bb