Spring Boot的Controller控制層和頁(yè)面
一.項(xiàng)目實(shí)例
1.項(xiàng)目結(jié)構(gòu)


2.項(xiàng)目代碼
1).ActionController.Java:
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定義的值,則取默認(rèn)值為Hello Shanhy
@Value("${application.hello:Hello Shanhy}")
private String hello;
/**
* 默認(rèn)頁(yè)<br/>
* @RequestMapping("/") 和 @RequestMapping 是有區(qū)別的
* 如果不寫參數(shù),則為全局默認(rèn)頁(yè)。
* 如果加了參數(shù)“/”,則只認(rèn)為是根頁(yè)面。
*/
@RequestMapping(value = {"/","/index"})
public String index(Map<String, Object> model){
// 直接返回字符串,框架默認(rèn)會(huì)去 spring.view.prefix 目錄下的 (index拼接spring.view.suffix)頁(yè)面
// 本例為 /WEB-INF/jsp/index.jsp
model.put("time", new Date());
model.put("message", this.hello);
return "index";
}
/**
* 響應(yīng)到JSP頁(yè)面page1
*/
@RequestMapping("/page1")
public ModelAndView page1(){
// 頁(yè)面位置 /WEB-INF/jsp/page/page1.jsp
//page/page1:頁(yè)面路徑地址/頁(yè)面名稱
ModelAndView mav = new ModelAndView("page/page1");
mav.addObject("content", hello);
return mav;
}
/**
* 響應(yīng)到JSP頁(yè)面page1(可以直接使用Model封裝內(nèi)容,直接返回頁(yè)面字符串)
*/
@RequestMapping("/page2")
public String page2(Model model){
// 頁(yè)面位置 /WEB-INF/jsp/page/page1.jsp
model.addAttribute("content", hello + "(第二種)");
return "page/page1";
}
}
2).application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp application.hello=Hello TOM
3).index.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>Spring Boot Sample</title>
</head>
<body>
Time: ${time}
<br>
Message: ${message}
</body>
</html>
4).page1.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>Spring Boot Sample</title>
</head>
<body>
<h1>${content }</h1>: ${message}
</body>
</html>
5).pom.xml:
<?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.運(yùn)行啟動(dòng)項(xiàng)目

訪問(wèn)web地址:http://localhost:8080/action/ ,如下所示:

二.代碼解析說(shuō)明
1.關(guān)于在Spring4.x中,@RestController和@Controller的區(qū)別
@RestController注解相當(dāng)于@ResponseBody + @Controller合在一起的作用。所以,以后定義controller的時(shí)候,可以直接使用@Controller,如果需要返回json可以直接在方法中添加@ResponseBody即可。
a).如果只是使用@RestController注解Controller,則Controller中的方法無(wú)法返回jsp頁(yè)面,配置的視圖解析器InternalResourceViewResolver則不起作用,返回的內(nèi)容就是Return 里的內(nèi)容(String/JSON)。
例如:本來(lái)應(yīng)該到success.jsp頁(yè)面的,則其顯示success.
public String test(HttpServletRequest request, HttpServletResponse response){
return "success";
}
b).如果使用@RestController注解Controller,需要返回到指定頁(yè)面,則需要配置視圖解析器InternalResourceViewResolver,可以利用ModelAndView返回試圖。
@RequestMapping(value = "/test")
public String test(HttpServletRequest request, HttpServletResponse response){
return newModelAndView("success");
}
c).如果使用@Controller注解Controller,如果需要返回JSON,XML或自定義mediaType內(nèi)容到頁(yè)面,則需要在對(duì)應(yīng)的方法上加上@ResponseBody注解。
@ResponseBody
@RequestMapping(value = "/test")
public String test(HttpServletRequest request, HttpServletResponse response){
return "success";
}
2.spring-boot 支持多種模版引擎包括:
a,F(xiàn)reeMarker
b,Groovy
c,Thymeleaf (Spring 官網(wǎng)使用這個(gè))
d,Velocity
e,JSP (貌似Spring Boot官方不推薦,STS創(chuàng)建的項(xiàng)目會(huì)在src/main/resources 下有個(gè)templates 目錄,這里就是讓我們放模版文件的,然后并沒(méi)有生成諸如 SpringMVC 中的webapp目錄)
以上所述是小編給大家介紹的Spring Boot的Controller控制層和頁(yè)面,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于Scanner中nextInt()、nextLine()等方法總結(jié)與問(wèn)題解決
這篇文章主要介紹了關(guān)于Scanner中nextInt()、nextLine()等方法總結(jié)與問(wèn)題解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-11-11
Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來(lái))
這篇文章主要介紹了Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來(lái)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
springboot實(shí)現(xiàn)小程序支付的項(xiàng)目實(shí)踐
本文主要介紹了springboot實(shí)現(xiàn)小程序支付的項(xiàng)目實(shí)踐,?可以通過(guò)調(diào)用微信支付?API?實(shí)現(xiàn)支付功能,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
簡(jiǎn)單了解spring cloud 網(wǎng)關(guān)服務(wù)
這篇文章主要介紹了簡(jiǎn)單了解spring cloud 網(wǎng)關(guān)服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

