SpringBoot整合視圖層

目錄

 

一.SpringBoot整合JSP

二.SpringBoot整合FreeMarker

三.SpringBoot整合ThymeLeaf


一.SpringBoot整合JSP

①創建工程設置配置文件

<project 
	xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <groupId>com.bjsxt</groupId>
  <artifactId>08-spring-boot-view-jsp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <!-- jdk1.7 -->
  <properties>
  	<java.version>1.7</java.version>
  </properties>
  
  <dependencies>
  <!-- springBoot的啓動器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
	<!-- jstl標籤庫 -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
	<!-- jasper——SpringBoot對JSP的處理引擎 -->
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency>
</dependencies>
</project>

②創建 springBoot 的全局配置文件 application.properties

設置視圖的前綴和後綴:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

③創建 實體和Controller

public class Users {
	private Integer userid;
	private String username;
	private Integer userage;
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getUserage() {
		return userage;
	}
	public void setUserage(Integer userage) {
		this.userage = userage;
	}
	public Users(Integer userid, String username, Integer userage) {
		super();
		this.userid = userid;
		this.username = username;
		this.userage = userage;
	}
	public Users() {
		super();
	}
	
}
/**
 * SpringBoot整合jsp
 *
 *
 */
@Controller
public class UserController {
	/*
	 * 處理請求,產生數據
	 */
	@RequestMapping("/showUser")
	public String showUser(Model model){
		List<Users> list = new ArrayList<>();
		list.add(new Users(1,"張三",20));
		list.add(new Users(2,"李四",22));
		list.add(new Users(3,"王五",24));
		
		//需要一個Model對象
		model.addAttribute("list", list);
		//跳轉視圖
		return "userList";
	}
}

④創建 jsp

<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center" width="50%">
		<tr>
			<th>ID</th>
			<th>Name</th>
			<th>Age</th>
		</tr>
		<c:forEach items="${list }" var="user">
			<tr>
				<td>${user.userid }</td>
				<td>${user.username }</td>
				<td>${user.userage }</td>	
			</tr>
		</c:forEach>
	</table>
</body>
</html>

⑤創建啓動類

/**
 * SpringBoot啓動類
 *
 *
 */
@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

運行測試:http://localhost:8080/showUser

二.SpringBoot整合FreeMarker

①創建項目配置文件

<project 
	xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <groupId>com.bjsxt</groupId>
  <artifactId>09-spring-boot-view-freemarker</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
  	<java.version>1.7</java.version>
  </properties>
  
  <dependencies>
  <!-- springBoot的啓動器 -->
    <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>
</project>

②編寫視圖

注意:springBoot 要求模板形式的視圖層技術的文件必須要放到 src/main/resources 目錄下必須要一個名稱爲 templates

<html>
	<head>
		<title>展示用戶數據</title>
		<meta charset="utf-9"></meta>
	</head>	
	<body>
		<table border="1" align="center" width="50%">	
			<tr>		
				<th>ID</th>
				<th>Name</th>
				<th>Age</th>
			</tr>
			
			<#list list as user >
				<tr>
					<td>${user.userid}</td>
					<td>${user.username}</td>
					<td>${user.userage}</td>
				</tr>
			</#list>	
		</table>
	</body>
</html>

③創建User和Controller 同上(SpringBoot整合JSP)

④創建啓動器 同上

運行測試結果同上

三.SpringBoot整合ThymeLeaf

①環境搭建

<project 
	xmlns="http://maven.apache.org/POM/4.0.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <groupId>com.bjsxt</groupId>
  <artifactId>11-spring-boot-view-thymeleaf-RUL</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
  <java.version>1.7</java.version>
  <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  </properties>
  
  <dependencies>
  	<!-- springBoot的啓動器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- thymeleaf的啓動器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>
</project>

②創建存放視圖的目錄

目錄位置:src/main/resources/templates
templates:該目錄是安全的。意味着該目錄下的內容是不允許外界直接訪問的

Thymelaef 是通過他特定語法對 html 的標記做渲染

③編寫Controller

/**
* Thymeleaf 入門案例
*
*
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model){
    model.addAttribute("msg", "Thymeleaf 第一個案例");
    return "index";
    }
}

④創建視圖

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Thymeleaf 入門</title>
    </head>
    <body>
        <span th:text="Hello"></span>
        <hr/>
        <span th:text="${msg}"></span>
    </body>
</html>

⑤創建啓動類 同上

運行測試:http://localhost:8080/show

⑥解決異常

方式一:

嚴格遵守html語法

 

方式二:

修改pom文件,設置Thymeleaf.jar:更新爲 3.0 以上的版本,thymeleaf-layout-dialect.jar:更新爲 2.0 以上的版本

  <properties>
	  <java.version>1.7</java.version>
	  <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
	  <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  </properties>

重新運行結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章