Spring Boot 之JSP
激活传统的Servlet Web部署
导入相关jar
<!-- JSP模板引起 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
启动类继承SpringBootServletInitializer,并组装
package com.adagio.chat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class ChatApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ChatApplication.class, args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
builder.sources(ChatApplication.class);
return builder;
}
}
参照类WebMvcProperties 配置JSP视图
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
更改pom文件为warbao
<packaging>war</packaging>
编写测试demo
新建目录:/src/main/webapp/WEB-INF/views
在上面目录下新建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>Insert title here</title>
</head>
<body>
hello,${message }
</body>
</html>
新建Controller类
package com.adagio.chat.jsp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JspDemoController {
@RequestMapping("index")
public String index(Model model){
model.addAttribute("message", "World");
return "index";
}
}
发现index路径有映射但是访问不到,pom添加
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
扩展
WebMvcProperties Spring MVC的配置
InternalResourceViewResolver 模板引擎资源解析
WebMvcAutoConfiguration 自动装配
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。