1. 程式人生 > >spring-boot使用JSP的Demo

spring-boot使用JSP的Demo

注意要點: 1  使用idea的時候,Pom引入tomcat-embed-jasper的時候不能有<scope>provided</scope> 2  @RestController是Controller和ResponseBody的結合,不會跳轉JSP會返回字串 3  啟動類Application要在根目錄(參考專案結構圖)

專案結構



*******************Controller *******************
package
com.example.controller;
import org.springframework.stereotype. Controller; import org.springframework.web.bind.annotation. RequestMapping; import java.util.Map;
@Controller public class ExampleController { @RequestMapping(value = "/index") public
String index() { return "index"; } @RequestMapping( "/name") public String helloJsp(String username,Map map){ map.put( "name", username); return "name"; } }

*******************Application 啟動類*******************

package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure. SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication public class Application extends SpringBootServletInitializer {
public static void main(String[] args) { SpringApplication. run(Application. class, args); }
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application. class); }
}

*******************application.properties配置檔案 *******************
#jsp 支援 spring.mvc.view.prefix= /WEB-INF/jsp/ spring.mvc.view.suffix= .jsp
server.port= 8082

*******************index.jsp放在main/webapp/WEB-INF/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 >
< form action= "name" method= "post" > 使用者名稱: < input type= "text" name= "username" />< br > < input type= "submit" value= "提交" />< br >
</ form > </ form >
</ body > </ html >

*******************name.jsp放在main/webapp/WEB-INF/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 > hello ${ name } !!! </ body > </ html >

*******************POM檔案 *******************
<? 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.0http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0 </ modelVersion >
< groupId >com.hao </ groupId > < artifactId >SpringBootJsp5 </ artifactId > < version >1.0-SNAPSHOT </ version >
< parent > < groupId >org.springframework.boot </ groupId > < artifactId >spring-boot-starter-parent </ artifactId > < version >2.0.2.RELEASE </ version > < relativePath /> <!-- lookup parent from repository --> </ parent >
< 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 >
< dependency > < groupId >org.springframework.boot </ groupId > < artifactId >spring-boot-starter-web </ artifactId > </ 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> idea必須刪除--> </ dependency >
</ dependencies >
</ project >


執行main方法後,在瀏覽器輸入http://localhost:8082/index

輸入一個使用者名稱,跳轉name