一.项目实例
1.项目结构
2.项目代码
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定义的值,则取默认值为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:
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.运行启动项目
访问web地址:http://localhost:8080/action/ ,如下所示:
二.代码解析说明
1.关于在Spring4.x中,@RestController和@Controller的区别
@RestController注解相当于@ResponseBody
+
@Controller合在一起的作用。所以,以后定义controller的时候,可以直接使用@Controller,如果需要返回json可以直接在方法中添加@ResponseBody即可。
a).如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver则不起作用,返回的内容就是Return
里的内容(String/JSON)。
例如:本来应该到success.jsp页面的,则其显示success.
b).如果使用@RestController注解Controller,需要返回到指定页面,则需要配置视图解析器InternalResourceViewResolver,可以利用ModelAndView返回试图。
@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目录)