IntelliJ IDEA 2017版 spring-boot加載jsp配置詳解(詳細圖文實例)


一、創建項目

(File--->New-->Project)

2、項目配置內容

3、選擇配置項目的Group包名,Artifact項目名稱

4、選擇項目類型為web類型

5、創建成功,點擊最后一步finish

二、代碼編寫

1、搭建pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.springboot</groupId>
 7     <artifactId>jsp</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>jsp</name>
12     <url>http://maven.apache.org</url>
13     <description>Demo project for Spring Boot</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.5.9.RELEASE</version>
19         <relativePath/> <!-- lookup parent from repository -->
20     </parent>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25         <java.version>1.8</java.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39 
40         <!-- servlet 依賴. -->
41         <dependency>
42             <groupId>javax.servlet</groupId>
43             <artifactId>javax.servlet-api</artifactId>
44             <scope>provided</scope>
45         </dependency>
46 
47         <!--
48                 JSTL(JSP Standard Tag Library,JSP標准標簽庫)是一個不斷完善的開放源代碼的JSP標簽庫,是由apache的jakarta小組來維護的。
49          -->
50         <dependency>
51             <groupId>javax.servlet</groupId>
52             <artifactId>jstl</artifactId>
53         </dependency>
54 
55         <!-- tomcat 的支持.-->
56         <dependency>
57             <groupId>org.springframework.boot</groupId>
58             <artifactId>spring-boot-starter-tomcat</artifactId>
59             <scope>provided</scope>
60         </dependency>
61 
62         <dependency>
63             <groupId>org.apache.tomcat.embed</groupId>
64             <artifactId>tomcat-embed-jasper</artifactId>
65             <!--<scope>provided</scope>-->
66         </dependency>
67 
68         <dependency>
69             <groupId>org.eclipse.jdt.core.compiler</groupId>
70             <artifactId>ecj</artifactId>
71             <version>4.6.1</version>
72             <scope>provided</scope>
73         </dependency>
74 
75     </dependencies>
76 
77     <build>
78         <plugins>
79             <plugin>
80                 <groupId>org.springframework.boot</groupId>
81                 <artifactId>spring-boot-maven-plugin</artifactId>
82             </plugin>
83         </plugins>
84     </build>
85 
86 
87 </project>
View Code

2、application配置

1 #頁面默認前綴目錄
2 spring.mvc.view.prefix=/WEB-INF/jsp/
3 # 響應默認后綴
4 spring.mvc.view.suffix=.jsp
View Code

3、Application類編輯

 1 package com.springboot;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class JspApplication {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(JspApplication.class, args);
11     }
12 }
View Code

4、Controller測試類編輯

 1 package com.springboot.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 import java.util.Map;
 7 
 8 /**
 9  * Created by liuya on 2018-01-28.
10  */
11 
12 @Controller
13 public class JSPController {
14 
15     @RequestMapping("/index")
16     public String index(Map<String, Object> map) {
17         map.put("name","Andy");
18         return "mytest";
19     }
20 
21 
22 }
View Code

三、界面配置

由於IntellJ下的項目不自帶webapp,所以需要手動自行添加

方法如圖

1、選中項目--->file-->Project Structure

2、如圖選中module ,選中jsp,然后選擇2的加號

3、加號彈出框中選擇web

4、就會在目錄下生成一個web,然后我們對web進行配置

5、web配置到項目中,選中web然后找到右邊的小加號

6、找到自己的項目,然后選擇你自己建立的web文件夾(一般有個默認路徑,但是我們不用,選擇后邊的省略號)

7、點擊后彈出如圖,然后選擇自己的web,如圖

8、點擊ok,然后就發現文件夾變為了帶點的文件夾,然后在文件夾下建立jsp子文件夾,然后在其中加入測試代碼

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: liuya
 4   Date: 2018-01-28
 5   Time: 下午 06:12
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>mytestdemo</title>
12 </head>
13 <body>
14 <center>
15     <h2>Hello ${name} </h2>
16 </center>
17 </body>
18 </html>
View Code

9、項目的各個目錄層級

10、訪問地址:http://127.0.0.1:8080/index,測試如圖返回為測試成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM