1 json 的使用
在 Spring Boot 中,只需我们在类添加 @RestController ,默认类中的方法都会以 json 的格式返回
@RestController
public class HelloWorldController {
@RequestMapping("/test")
public Map<String,String> index() {
Map<String,String> map = new HashMap<String,String>();
map.put("name", "小明");
map.put("age","12");
return map;
}
}
@RestController 注解包含了 @Controller 和 @ResponseBody 注解,可以理解为 @Controller的增强版。若使用页面开发,只需使用@Controller注解即可。
2 模版引擎,Controller
Spring Boot 支持多种模版引擎,包括 FreeMarker,Groovy,Thymeleaf,Velocity,JSP 。其中Spring 官方推荐使用 Thymeleaf ,而不推荐使用 JSP(不会生成 webapp 目录)。
本文使用 JSP 进行举例。
我们创建一个控制器
package edu.szu.test;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JSPTestController {
// 从 application.properties 中读取配置,如取不到默认值为Hello Spring Boot
@Value("${application.hello:Hello Spring Boot}")
private String hello = "Hello Spring Boot";
/**
* 我们需要注意,@RequestMapping("/") 和 @RequestMapping 是有区别的
* 如果不写参数,则为全局默认页,输入404页面,也会自动访问到这个页面。
* 如果加了参数“/”,则只认为是根页面。
* @param map
* @return
*/
@RequestMapping(value = {"/","/index1"})
public String index1(Map<String, String> map){
// 直接返回字符串,框架默认会去 spring.view.prefix 目录下的 (index拼接spring.view.suffix)页面
// 本例为 /WEB-INF/jsp/index1.jsp
map.put("name","小明");
map.put("message",hello);
return "index1";
}
}
然后添加依赖
<!-- 使用jsp -->
<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>
我们之前说过,Spring Boot 不推荐使用 JSP,若想使用 JSP 需要配置 application.properties。在 src/main/resources/application.properties 添加如下内容:
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Spring Boot Test
在 src/main/webapp/WEB-INF/jsp 创建一个 index1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
name:${name}
<br>
message:${message}
</body>
</html>
结果如下:
3 Servlet
在 Spring Boot 中添加自己的 Servlet 有两种方法,代码注册 Servlet 和注解自动注册。
- 代码注册通过 ServletRegistrationBean,FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 也可以通过实现 ServletContextInitializer 接口直接注册。
- 在 SpringBootApplication 上使用 @ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
通过代码注册 Servlet 示例
/**
* 使用代码注册Servlet(不需要@ServletComponentScan注解)
* @author 30309
*
*/
@SpringBootApplication
public class TestApplication {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new MyFirstServlet(), "/servlet/*");
}
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
public class MyFirstServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
System.out.println("doGet()");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
System.out.println("doPost()");
}
}
在浏览器输入 http://localhost:8080/servlet ,控制台便会打印相应的字符
证明 Servlet 运行成功。
使用注解注册 Servlet 示例
/**
* 直接使用 @ServletComponentScan注解
* @author 30309
*
*/
@ServletComponentScan
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
@WebServlet(urlPatterns="/secondServlet")
public class MySecondServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
System.out.println("doGet2()");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
System.out.println("doPost2()");
}
}
在浏览器输入 http://localhost:8080/secondServlet ,控制台便会打印相应的字符
证明 Servlet 运行成功。