我的控制器
@Controller //@RequestMapping("/") //@ComponentScan("com.spring") //@EnableAutoConfiguration public class HomeController { @Value("${framework.welcomeMessage}") private String message; @RequestMapping("/hello") String home(ModelMap model) { System.out.println("hittin the controller..."); model.addAttribute("welcomeMessage", "vsdfgfgd"); return "Hello World!"; } @RequestMapping(value = "/indexPage", method = RequestMethod.GET) String index(ModelMap model) { System.out.println("hittin the index controller..."); model.addAttribute("welcomeMessage", message); return "welcome"; } @RequestMapping(value = "/indexPageWithModel", method = RequestMethod.GET) ModelAndView indexModel(ModelMap model) { System.out.println("hittin the indexPageWithModel controller..."); model.addAttribute("welcomeMessage", message); return new ModelAndView("welcome", model); } }
我在/ WEB-INF / jsp中的JSP(welcome.jsp) (父文件夹为WebContent)
<%@ 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>Welcome to Spring Boot</title> </head> <body> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> Message: ${message} </body> </html>
我的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> <groupId>SpringBootPlay</groupId> <artifactId>SpringBootPlay</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-log</artifactId> <version>0.17</version> </dependency> </dependencies> <properties> <java.version>1.8</java.version> <start-class>com.spring.play.BootLoader</start-class> <main.basedir>${basedir}/../..</main.basedir> <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot> </properties> <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>
我的应用程式初始化器
@EnableAutoConfiguration @SpringBootApplication @ComponentScan({ "com.spring.controller" }) @PropertySources(value = { @PropertySource("classpath:/application.properties") }) public class BootLoader extends SpringBootServletInitializer { final static Logger logger = Logger.getLogger(BootLoader.class); @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(BootLoader.class); } public static void main(String[] args) { SpringApplication.run(BootLoader.class, args); } }
我什至增加了thymeleaf对pom的依赖性。仍然没有用。每当我打localhost:8080/hello or /indexPage or /indexPageWithModel它总是说
thymeleaf
localhost:8080/hello or /indexPage or /indexPageWithModel
白标错误页面
此应用程序没有针对/ error的显式映射,因此您将其视为后备。
2016年9月21日星期三EDT 2016年发生意外错误(类型=未找到,状态= 404)。] /WEB-INF/jsp/welcome.jsp
我的application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp framework.welcomeMessage=Welcome to Dashboard
请帮我。谢谢!
我自己想通了。
当我将动态Web项目转换为Maven项目时,它没有以这种方式创建文件夹结构
src/main/java
和
src/main/resources
src/main/webapp
我手动创建了自己,并将jsp文件从WebContent/WEB-INF/jsp移至src/main/webapp/WEB- INF/jsp并修改Java build path了项目属性。
WebContent/WEB-INF/jsp
src/main/webapp/WEB- INF/jsp
Java build path
然后,我重新启动embedded tomcat并再次尝试。有效。
embedded tomcat