springboot hello world jsp
1.0.0
第5天 - 春季靴子 - 與JSP的Hello World示例
在本教程中,您將知道如何以最簡單的方式在Spring Boot中使用JSP創建Hello World示例。


文件>導入>現有Maven項目>單擊
Next>選擇項目根目錄>單擊Finish
pom.xml文件中添加額外的依賴項< dependencies >
< 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 >
<!-- JSTL for JSP -->
< dependency >
< groupId >javax.servlet</ groupId >
< artifactId >jstl</ artifactId >
</ dependency >
<!-- Need this to compile JSP -->
< dependency >
< groupId >org.apache.tomcat.embed</ groupId >
< artifactId >tomcat-embed-jasper</ artifactId >
< scope >provided</ scope >
</ dependency >
</ dependencies >src/main/resources/application.properties文件中的項目配置屬性 # jsp view binding configuration
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# project context and port configuration
server.port=8080
server.context-path=/springboot-helloworld
src/main/java/package/HomeController.java文件中添加控制器類 import org . springframework . stereotype . Controller ;
import org . springframework . ui . Model ;
import org . springframework . web . bind . annotation . RequestMapping ;
import org . springframework . web . bind . annotation . RequestMethod ;
@ Controller
public class HomeController {
@ RequestMapping ( value = "/" , method = RequestMethod . GET )
public String hello ( Model model ){
model . addAttribute ( "title" , "Spring Boot - Hello World Example Jsp" );
return "index" ;
}
}webapp/WEB-INF/jsp/index.jsp文件中添加JSP視圖<%@ page language = " java " contentType = " text/html; charset=ISO-8859-1 " pageEncoding = " ISO-8859-1 " %>
<%@ taglib prefix = " spring " uri = " http://www.springframework.org/tags " %>
<%@ taglib prefix = " c " uri = " http://java.sun.com/jsp/jstl/core " %>
<!DOCTYPE html>
< html >
< head >
< title > ${ title } </ title >
</ head >
< body >
< h1 > ${ title } </ h1 >
</ body >
</ html >
右鍵單擊“項目>運行為> Spring Boot應用程序
訪問http:// localhost:8080/springboot-holowerorld/
rutvik patel [email protected]
根據GPL V3.0許可分發。有關更多信息,請參見LICENSE 。