SpringBoot整合jsp

1、创建项目

 

2、修改pom文件,添加坐标。将jdk版本改为1.7,添加启动器

 

<!-- 修改jdk版本 -->
<properties>
  <java.version>1.7 </java.version>
</properties>
<!-- springboot启动器 -->
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

<!-- jstl -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
  </dependency>

<!-- jasper -->
  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
  </dependency>
</dependencies>

3、创建配置文件application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/  //前缀
spring.mvc.view.suffic= .jsp        //后缀

 

 4、创建controller和User类

package com.zhu.controller;

@Controller			//做视图跳转,不可用RestController
public class UserController {
	/*
	 * 处理请求,产生数据
	 */
	@RequestMapping("/showUser")
	public String showUser(Model model) {
		ArrayList<User> list = new ArrayList<>();
		list.add(new User("aa",1,1));
		list.add(new User("bb",2,2));
		list.add(new User("cc",3,3));
		list.add(new User("dd",4,4));
		
		//将list传到jsp当中,需要一个Model对象,因此参数中多了一个Model型参数
		model.addAttribute("list",list);
		//跳转视图
		return "userList";
		
	}
}
package com.zhu.pojo;

public class User {
	private String username;
	private Integer userid;
	private Integer userage;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public User() {
		super();
	}
	public User(String username, Integer userid, Integer userage) {
		super();
		this.username = username;
		this.userid = userid;
		this.userage = userage;
	}
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	public Integer getUserage() {
		return userage;
	}
	public void setUserage(Integer userage) {
		this.userage = userage;
	}
}

  

5、建立jsp jsp文件要在src/main中新建文件夹webapp,再新建文件夹jsp,然后再创建jsp文件

遇到错误,错误原因:<c:forEach item="${list} " var = "user">中,${}后很多了个逗号,

导致异常:Property [userid] not found on type [java.lang.String]] with root cause出现,

<%@ 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>Insert title here</title>
</head>
<body>
	<table border="1" align = "center" width = "50%">
		<tr>
			<th>ID</th>
			<th>Age</th>
			<th>Name</th>
		</tr>
		<c:forEach item="${list}" var = "user">
			<tr>
				<td>${user.userid}</td>
				<td>${user.username}</td>
				<td>${user.username}</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

6、创建启动类

package com.zhu;

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

  

 

posted @ 2019-05-09 17:19  朱健栋  阅读(419)  评论(0编辑  收藏  举报