springboot 整合JSP

Springboot 不建議使用JSP
原因;

  1. 在tomcat上,jsp不能在嵌套的tomcat容器解析即不能在打包成可執行的jar的情況下解析
  2. Jetty 嵌套的容器不支持jsp
  3. Undertow

官方推薦使用模版引擎,thymelef。

下面是JSP總結

1、創建MAVEN工程要用JSP必須要用war包
在這裏插入圖片描述
在這裏插入圖片描述

Pom.xml 引入相關依賴


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.springboot</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>freemarker</artifactId>
  <packaging>war</packaging>
  
  
  <dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 整合jsp需要依賴 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>
	</dependencies>
	
	//這裏是因爲創建的<packaging>war</packaging> Pom.xml 有紅叉叉,加入下面代碼就可以了。
		<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
	
</project>

創建controller層

package thymelef.com.Coutroller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CoutrollerTest {
	@RequestMapping("/index")
	  public String index(Model model){
		  model.addAttribute("name", "fujiashi"); 
		  return"index";
		  
	  }
	

}

創建啓動類

package thymelef.com.Test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
@Controller
@SpringBootApplication(scanBasePackages={"thymelef.com.Coutroller"})
public class JspTest {
  public static void main(String[] args) {
	SpringApplication.run(JspTest.class, args);
}
}


創建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>
${name} 
</body>
</html>

需要建立application.properties
spring.mvc.view.prefix=/jsp/ 前綴
spring.mvc.view.suffix=.jsp  後綴

在這裏插入圖片描述
最後訪問
http://localhost:8080/index

在這裏插入圖片描述

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