一.項目實例
1.項目結構
2.項目代碼
1).actioncontroller.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.example.controller; import java.util.date; import java.util.map; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; @controller @requestmapping ( "/action" ) public class actioncontroller { // 從 application.properties 中讀取配置,如取不到application.properties定義的值,則取默認值為hello shanhy @value ( "${application.hello:hello shanhy}" ) private string hello; /** * 默認頁<br/> * @requestmapping("/") 和 @requestmapping 是有區別的 * 如果不寫參數,則為全局默認頁。 * 如果加了參數“/”,則只認為是根頁面。 */ @requestmapping (value = { "/" , "/index" }) public string index(map<string, object> model){ // 直接返回字符串,框架默認會去 spring.view.prefix 目錄下的 (index拼接spring.view.suffix)頁面 // 本例為 /web-inf/jsp/index.jsp model.put( "time" , new date()); model.put( "message" , this .hello); return "index" ; } /** * 響應到jsp頁面page1 */ @requestmapping ( "/page1" ) public modelandview page1(){ // 頁面位置 /web-inf/jsp/page/page1.jsp //page/page1:頁面路徑地址/頁面名稱 modelandview mav = new modelandview( "page/page1" ); mav.addobject( "content" , hello); return mav; } /** * 響應到jsp頁面page1(可以直接使用model封裝內容,直接返回頁面字符串) */ @requestmapping ( "/page2" ) public string page2(model model){ // 頁面位置 /web-inf/jsp/page/page1.jsp model.addattribute( "content" , hello + "(第二種)" ); return "page/page1" ; } } |
2).application.properties:
1
2
3
|
spring.mvc.view.prefix=/web-inf/jsp/ spring.mvc.view.suffix=.jsp application.hello=hello tom |
3).index.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ 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>spring boot sample</title> </head> <body> time: ${time} <br> message: ${message} </body> </html> |
4).page1.jsp:
1
2
3
4
5
6
7
8
9
10
11
|
<%@ 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>spring boot sample</title> </head> <body> <h1>${content }</h1>: ${message} </body> </html> |
5).pom.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.example</groupid> <artifactid>spring-boot-simple</artifactid> <version> 0.0 . 1 -snapshot</version> <!-- <packaging>jar</packaging> --> <packaging>war</packaging> <name>spring-boot-simple</name> <description>demo project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 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> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
3.運行啟動項目
訪問web地址: ,如下所示:
二.代碼解析說明
1.關于在spring4.x中,@restcontroller和@controller的區別
@restcontroller注解相當于@responsebody + @controller合在一起的作用。所以,以后定義controller的時候,可以直接使用@controller,如果需要返回json可以直接在方法中添加@responsebody即可。
a).如果只是使用@restcontroller注解controller,則controller中的方法無法返回jsp頁面,配置的視圖解析器internalresourceviewresolver則不起作用,返回的內容就是return 里的內容(string/json)。
例如:本來應該到success.jsp頁面的,則其顯示success.
1
2
3
|
public string test(httpservletrequest request, httpservletresponse response){ return "success" ; } |
b).如果使用@restcontroller注解controller,需要返回到指定頁面,則需要配置視圖解析器internalresourceviewresolver,可以利用modelandview返回試圖。
1
2
3
4
5
6
7
8
9
10
|
@requestmapping (value = "/test" ) public string test(httpservletrequest request, httpservletresponse response){ return newmodelandview( "success" ); } c).如果使用 @controller 注解controller,如果需要返回json,xml或自定義mediatype內容到頁面,則需要在對應的方法上加上 @responsebody 注解。 @responsebody @requestmapping (value = "/test" ) public string test(httpservletrequest request, httpservletresponse response){ return "success" ; } |
2.spring-boot 支持多種模版引擎包括:
a,freemarker
b,groovy
c,thymeleaf (spring 官網使用這個)
d,velocity
e,jsp (貌似spring boot官方不推薦,sts創建的項目會在src/main/resources 下有個templates 目錄,這里就是讓我們放模版文件的,然后并沒有生成諸如 springmvc 中的webapp目錄)
以上所述是小編給大家介紹的spring boot的controller控制層和頁面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!