Java框架_SpringBoot整合JSP

1、創建項目


2、修改pom文件,添加座標

3、創建springBoot的全局配置文件,application.properties


# mav視圖配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp


4、創建Controller

package com.lxp.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;

import com.lxp.pojo.User;

@Controller
public class ShowUserController {

	@RequestMapping("/showUser")
	public String showUser(Model model) {

		List<User> list = new ArrayList<User>();
		list.add(new User(1, "張三", 20));
		list.add(new User(2, "李四", 22));
		list.add(new User(3, "王五", 24));

		model.addAttribute("list", list);
		return "userList";
	}
}
pojo類
package com.lxp.pojo;

public class User {
	private Integer userId;
	private String userName;
	private Integer age;

	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 getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public User(int id, String name, int age) {
		this.userId = id;
		this.userName = name;
		this.age = age;
	}

}

5、創建jsp


<%@page import="org.apache.tomcat.util.descriptor.tld.TaglibXml"%>
<%@ 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.age }</td>	
			</tr>
		</c:forEach>
	</table>
</body>
</html>

6,創建啓動類

@SpringBootApplication
public class App {

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


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