Spring Boot integration view technology, application global profile

Spring Boot integration view technology

Spring Boot 整合jsp
Spring Boot 整合Freemarker
Spring Boot 整合 Thymeleaf  (重点讲解,官方推荐)

Spring Boot integration jsp

step:

  1. New maven project of the jar Spring Boot project
    Here Insert Picture Description
    Here Insert Picture Description
  2. Open file pom.xml added jsp dependent
    Here Insert Picture Description
    code is as follows:
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <dependencies>
        <!-- spring boot web启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- jasper:jsp引擎 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>

  1. Write controller Controller (without access to the database)
    Here Insert Picture Description
    code is as follows:
@Controller
public class UserController {
	/**
	 * 获取用户信息,到jsp页面进行展示
	*/
	@RequestMapping("/userList")
	public String getUsersAll(Model model) {
		//访问业务层-->数据访问层mapper-->mybatis数据库获取所有用户信息
		//模拟,定义固定的用户信息
		List<User> list=new ArrayList<User>();
		list.add(new User("007", "小张", 22));
		list.add(new User("009","小康",32));
		list.add(new User("012","小健",18));
		model.addAttribute("list", list);
		//配置springmvc的视图解析器,前缀:/WEB-INF/   后缀: .jsp
		return "index";
	}
}

  1. Create a Spring Boot global configuration file application.properties
    src / main / Resources -> create -> application.properties
    the Spring two global recognition the Boot default configuration file: application.properties and application.yml
    Here Insert Picture Description
    Code:
#配置jsp的访问的前缀和后缀 (视图解析器)
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

  1. View layer JSP
    the src / main -> the webapp -> the WEB-INF -> the index.jsp
    Here Insert Picture Description
    Code:
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户显示页面</title>
</head>
<body>
<table border="1" width="60%" align="center">
    <tr>
        <td>用户编号</td>
        <td>用户名称</td>
        <td>年龄</td>
    </tr>
    <c:forEach items="${list}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.age}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

  1. Startup class
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
  1. Run your browser and enter localhost: 8080 / userList
    Here Insert Picture Description

Spring Boot 整合freemarker

  1. Create a maven project's jar of spring boot project (the same as step omitted)
  2. Open pom.xml, its dependencies added freemarker
    Here Insert Picture Description
    Code:
<dependencies>
     <!--spring boot web 启动器坐标 -->
     <dependency>
     	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     
     <!-- freemarker 启动器坐标 -->
     <dependency>
     	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-freemarker</artifactId>
     </dependency>
  </dependencies>

  1. Write controller Controller
    Here Insert Picture Description
  2. View layer FreeMarker
    FreeMarker page templates must be placed under the directory src / main / resources, and extension of the page: FTL
    Here Insert Picture Description
    Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户显示页面</title>
</head>
<body>
   <table border="1" width="60%" align="center">
       <tr>
       	  <td>用户编号</td>
       	  <td>用户名称</td>
       	  <td>年龄</td>
       </tr>
	   <!--freemarker获取request传过来的数据  <#数据类型  key类型 as 遍历元素名称>-->
	   <#list list as user>
	       <tr>
	       	  <td>${user.id}</td>
	       	  <td>${user.username}</td>
	       	  <td>${user.age}</td>
	       </tr>
	   </#list>
   </table>
</body>
<html>

  1. Create a Spring Boot global configuration file application.properties
    Here Insert Picture Description
    Code:
# 模板编码。
spring.freemarker.charset= UTF-8
# 后缀,在构建URL时附加到查看名称。
spring.freemarker.suffix=.ftl
# 逗号分隔的模板路径列表。src/main/resources==classpath
spring.freemarker.template-loader-path=classpath:/templates/
server.port=8081
  1. Startup class
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

run
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/joker-dj/p/12657530.html