SpringBoot整合JSP

雖然說作為視圖技術(shù),JSP不是SpringBoot中提倡的,但是在實(shí)際開發(fā)中很多人還是離不開JSP技術(shù)的,因此在這里我們簡單的整合使用一下。

1.創(chuàng)建項(xiàng)目
2.修改pom文件,添加坐標(biāo)
<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.1.3.RELEASE</version>
  </parent>
  <groupId>com.neuedu</groupId>
  <artifactId>08-spring-boot-view-jsp</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
      <!-- SpringBoot啟動(dòng)器坐標(biāo) -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <!-- JSTL坐標(biāo) -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
      </dependency>
      <!-- jasper坐標(biāo):支持JSP -->
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-jasper</artifactId>
          <!-- 打包的時(shí)候,不用把該jar包打包進(jìn)去 -->
          <scope>provided</scope>
  </dependency>
  </dependencies>
</project>
3.創(chuàng)建SpringBoot的全局配置文件,application.properties

配置視圖解析器,修改application.properties如下:


spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
4.創(chuàng)建Controller
package com.neuedu.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.neuedu.po.User;

/**
* SpringBoot 整合 jsp
* @author 清水三千尺
*
*/
@Controller
public class UserController {
  
  /*
   * 處理請(qǐng)求,產(chǎn)生數(shù)據(jù)(模擬數(shù)據(jù))
   */
  @RequestMapping("/showUser")
  public String showUser(Model model) {
      //準(zhǔn)備模擬數(shù)據(jù)
      List<User> list = new ArrayList<User>();
      list.add(new User(1, "趙四", 45));
      list.add(new User(2, "劉能", 39));
      list.add(new User(3, "廣坤", 43));
      
      //需要一個(gè)Model對(duì)象
      model.addAttribute("list",list);
      //跳轉(zhuǎn)視圖
      return "userList";
  }

}
5.創(chuàng)建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>
6.創(chuàng)建啟動(dòng)類
package com.neuedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBoot 啟動(dòng)類
* @author 清水三千尺
*
*/
@SpringBootApplication
public class App {
  public static void main(String[] args) throws Exception {
      SpringApplication.run(App.class, args);
  }
}
7.測試

訪問路徑:http://localhost:8080/showUser

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容