springboot默认并不支持jsp模板,所以需要配置。
下面是一个可以运行的例子:
首先配置属性文件:
spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8server.port
=8080spring.mvc.view.prefix:
/WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello world
然后配置启动类:
package sample.jsp; 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 Application extends SpringBootServletInitializer {
@Override</br> </span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> SpringApplicationBuilder configure(SpringApplicationBuilder application) {</br> </span><span style="color: #0000ff;">return</span> application.sources(Application.<span style="color: #0000ff;">class</span><span style="color: #000000;">);</br> }</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {</br> SpringApplication.run(Application.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, args);</br> }</br></br>
}
最后写一下控制层:
package sample.jsp; import java.util.Date;
import java.util.Map; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class WelcomeController {
@Value(</span>"${application.message}"<span style="color: #000000;">)</br> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String message;</br></br> @GetMapping(</span>"/welcome"<span style="color: #000000;">)</br> </span><span style="color: #0000ff;">public</span> String welcome(Map<String, Object><span style="color: #000000;"> model) {</br> model.put(</span>"time", <span style="color: #0000ff;">new</span><span style="color: #000000;"> Date().getTime());</br> model.put(</span>"message", <span style="color: #0000ff;">this</span><span style="color: #000000;">.message);</br> </span><span style="color: #0000ff;">return</span> "welcome"<span style="color: #000000;">;</br> }</br></br> @GetMapping(</span>"/test"<span style="color: #000000;">)</br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String test(){</br> </span><span style="color: #0000ff;">return</span> "test"<span style="color: #000000;">;</br> }</br></br>
}
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>spring-boot-sample-web-jsp</groupId> <artifactId>ming</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Spring Boot Web JSP Sample</name> <description>Spring Boot Web JSP Sample</description> <url>http://projects.spring.io/spring-boot/</url> <organization> <name>Pivotal Software, Inc.</name> <url>http://www.spring.io</url> </organization> <properties> <main.basedir>${basedir}/../..</main.basedir> <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot> <springboot.version>1.5.4.RELEASE</springboot.version> </properties> <dependencies> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${springboot.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Provided --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>${springboot.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>8.5.6</version> <scope>provided</scope> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${springboot.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> </plugins> </build> </project>