1. 程式人生 > >springboot項目添加jsp支持

springboot項目添加jsp支持

val urn templates java map odi span 微軟雅黑 spa

.創建springboot項目

使用 http://start.spring.io/ 快速創建一個springboot項目下載並導入

技術分享

二.添加依賴

在pom.xml中添加支持jsp的依賴如下:

<!-- web  依賴 -->

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

</dependency>

 <!-- servlet 依賴 -->
 <dependency>
           <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
 </dependency>
<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>
 </dependency>

三.編寫controller

@Controller
public class LoginController {
    
    @RequestMapping(value="/login")
    public String hello(){
        return "login";
    }
    

}

註意此時註解要用@Controller,不能使用@RestController

四.配置支持jsp的配置文件application.properties

技術分享

##jsp  ##
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix
=.jsp

五.編寫jsp頁面

技術分享

在項目工程下src/main目錄下添加webapp/WEB-INF/jsp文件夾,裏面添加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
!!! </body> </html>

六.啟動項目啟動類訪問controller路徑

技術分享

說明:

1,FreeMarker
2,Groovy
3,Thymeleaf (
spring 官網使用這個)
4,Velocity
5,Spring Boot官方不推薦使用JSP ,STS創建的項目會在sec/main/resources 下有個templates 目錄,這裏就是讓我們放模版文件的,然後並沒有生成諸如SpringMVC 中的webapp目錄)

springboot項目添加jsp支持