注意spring boot 版本,版本低的要加.mvc
配置与代码:
application.properties
server.port=8000
#spring mvc
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
pom.xml
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<groupId>cn.springboot</groupId>
<artifactId>SpringBoot</artifactId>
<version>1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBoot</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<!-- servlet 依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<!-- 添加 JSTL 支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
测试controller
@Controller
public class TestController {
@RequestMapping(value = "/index")
public String index() {
System.err.println(1111);
return "index";
}
@RequestMapping("/")
@ResponseBody
public String test() {
return "welcome spring boot";
}
}
启动类
@SpringBootApplication
public class SampleController extends SpringBootServletInitializer {
/*
* @Override protected SpringApplicationBuilder
* configure(SpringApplicationBuilder application) { return
* application.sources(SampleController.class); }
*/
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
然后就报错了,错误如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
!!!!!!
视图不对 啊
解决办法:原来上面的application.properties配置文件有问题
去掉MVC改成
server.port=8000
#spring mvc
spring.view.prefix=/WEB-INF/
spring.view.suffix=.jsp
运行成功