9、SpringBoot 新增JSP支援
阿新 • • 發佈:2018-12-03
SpringBoot 新增JSP支援
專案結構
在pom.xml檔案新增依賴
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>cn.ylx</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-helloworld</name>
<description >springboot-helloworld</description>
<!-- SpringBoot專案要繼承的父類 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
< relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- SpringBoot專案要繼承的父類 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web支援: 1、web mvc; 2、restful; 3、jackjson支援; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- servlet 依賴 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL 標籤庫 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支援 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
JSTL(JSP Standard Tag Library,JSP標準標籤庫)是一個不斷完善的開放原始碼的JSP標籤庫,是由apache的jakarta小組來維護的。
配置application.yml支援jsp
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
編寫測試Controller
package cn.ylx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class HelloController {
@RequestMapping("/index")
public String index(Map<String,Object> map){
map.put("name", "Nancy");
return "index";
}
}
編寫JSP頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<h2>Hello ${name} </h2>
</body>
</html>
測試:能成功取值。
注意
IDEA啟動該專案如果直接啟動啟動項,瀏覽器輸入 http://localhost:8080/index ,會報錯:
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/index.jsp
使用右側的外掛啟動就不會報錯: