Spring Boot實現JSP解析
Spring Boot可以輕鬆建立可以“執行”的獨立的,生產級的基於Spring的應用程式。大多數Spring Boot應用程式需要很少的Spring配置。
功能
建立獨立的Spring應用程式
直接嵌入Tomcat,Jetty或Undertow(不需要部署WAR檔案)
提供好用的POM來簡化你的Maven配置
儘可能自動配置Spring
提供生產就緒功能如指標,執行狀況檢查和外部化配置
絕對沒有程式碼生成,也不需要XML配置.(摘自官方文件)
當我們在做Spring的時候,發現無論是使用xml的,還是java的方式,都有很多配置,稍有不慎就會各種報錯。
而如果使用Spring Boot確實方便不少,至少少了很多配置,我們可以直接去
話不多說,我們直接看個簡單demo。
1.pom檔案:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId >
<artifactId>spring-boot-example</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-example Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId >
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Tomcat Embed -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.properties檔案,主要是配置jsp的路徑,字首字尾,相當於springmvc-servlet.xml的功能。
spring.mvc.view.prefix=/jsp/
spring.mvc.view.suffix=.jsp
#自定義error需要
server.error.whitelabel.enabled = false
3.HelloController:
這裡需要注意,如果要定義自己的error,需要實現ErrorController,因為Spring Boot系統會預設對映 /error。
@Controller
public class HelloController implements ErrorController{
@RequestMapping("/")
public String showIndex() {
return "index";
}
@RequestMapping("/hello")
public String showHello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}
@RequestMapping("/error")
public String showError() {
return getErrorPath();
}
@Override
public String getErrorPath() {
return "error";
}
}
4.error.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
I'm error
</body>
</html>
5.hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
I'm ${name}
</body>
</html>
6.index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="/hello?name=mike">click here</a>
</body>
</html>
7.啟動類WebApplication.java,定義main方法啟動。
當然也可以用maven 啟動:mvn spring-boot:run
或者是打包啟動:
mvn package
java -jar target/spring-boot-example-0.0.1-SNAPSHOT.jar
這裡SpringBootApplication註解已經包含了@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan等註解
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
點選link:根據你傳的引數顯示
至此,基本框架已經搭好,可以看出我們基本沒有配置,就快速的搭建起了環境,而且如果要與其他框架整合如mybatis等,也有相關的jar包可以引入。