整合持久层技术
- 整合jsp
- 整合frameworker
- 整合thymeleaf
整合jsp技术
pom文件
<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>2.2.0.RELEASE</version>
</parent>
<groupId>com.pshdhx</groupId>
<artifactId>01-springboot-view-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
<version>9.0.8</version>
</dependency>
</dependencies>
</project>
配置文件application.properities
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
实体类User.java
package com.pshdhx.controller;
public class User {
private String id;
private String userName;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String id, String userName, int age) {
super();
this.id = id;
this.userName = userName;
this.age = age;
}
}
控制层JSPController.java
package com.pshdhx.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
* @author ASUS
*
*/
@Controller
public class JSPController {
/*
* 处理请求,产生数据
*/
@RequestMapping("/showUser")
public String showUser(Model model){
List<User> list = new ArrayList<>();
list.add(new User("1","张三",20));
list.add(new User("2","李四",22));
list.add(new User("3","王五",24));
//需要一个 Model 对象
model.addAttribute("list", list);
//跳转视图
return "userList";
}
}
视图层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"%>
<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.id }</td>
<td>${user.userName }</td>
<td>${user.age }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
主启动类App.java
package com.pshdhx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
目录结构
访问地址:
http://localhost:8080/showUser