我刚刚开始学习spring boot,并创建了一个小型的spring mvc应用程序。但是当我尝试访问视图时,我收到白标错误,但是如果我只是使用responseBody注释返回字符串,则应用程序正常工作。我还将百里香加入了依赖。
demo2
|-- src/main/java
| |-- example
| | `-- Demo2Application
| |-- controller
| | `-- Controller
| `-- model
| `-- Person
`-- src/main/resources
`-- templates
`-- personView.html
这是我的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>org.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用程序配置主要方法:
@Configuration
@EnableAutoConfiguration
@ComponentScan({"com", "example", "controller"})
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
控制器类:
@Controller
public class PersonController {
@RequestMapping("/person")
public String getPerson(Model model) {
Person person = new Person();
person.setFirstname("John");
person.setLastName("Carter");
person.setAge(25);
model.addAttribute(person);
return "personView";
}
@RequestMapping("/test")
public @ResponseBody String test() {
return "hello world";
}
@ResponseBody
@RequestMapping("/")
String entry() {
return "Welcome to Spring Boot Application";
}
}
查看页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
FirstName : <span th:text="${person.firstName}"></span>
LastName : <span th:text="${person.lastName}"></span>
Age : <span th:text="${person.age}"></span>
</body>
</html>
错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Sep 04 22:45:31 EDT 2016
There was an unexpected error (type=Not Found, status=404).
No message available
日志:
2016-09-04 22:43:10.330 INFO 11356 --- [ main] com.example.Demo2Application : Starting Demo2Application on Sais-MacBook-Pro.local with PID 11356 (/Users/sainishankbojja/Documents/workspace-sts-3.8.1.RELEASE/demo2/target/classes started by sainishankbojja in /Users/sainishankbojja/Documents/workspace-sts-3.8.1.RELEASE/demo2)
2016-09-04 22:43:10.334 INFO 11356 --- [ main] com.example.Demo2Application : No active profile set, falling back to default profiles: default
2016-09-04 22:43:10.370 INFO 11356 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@a9cd3b1: startup date [Sun Sep 04 22:43:10 EDT 2016]; root of context hierarchy
2016-09-04 22:43:11.353 INFO 11356 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-09-04 22:43:11.365 INFO 11356 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-09-04 22:43:11.366 INFO 11356 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-09-04 22:43:11.430 INFO 11356 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-09-04 22:43:11.430 INFO 11356 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1063 ms
2016-09-04 22:43:11.558 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-04 22:43:11.561 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-09-04 22:43:11.562 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-09-04 22:43:11.562 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-09-04 22:43:11.562 INFO 11356 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-09-04 22:43:11.786 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@a9cd3b1: startup date [Sun Sep 04 22:43:10 EDT 2016]; root of context hierarchy
2016-09-04 22:43:11.834 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String controller.PersonController.test()
2016-09-04 22:43:11.835 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String controller.PersonController.entry()
2016-09-04 22:43:11.835 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/person]}" onto public java.lang.String controller.PersonController.getPerson(org.springframework.ui.Model)
2016-09-04 22:43:11.837 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-09-04 22:43:11.837 INFO 11356 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-09-04 22:43:11.860 INFO 11356 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-04 22:43:11.860 INFO 11356 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-04 22:43:11.885 INFO 11356 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-04 22:43:11.990 INFO 11356 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-09-04 22:43:12.035 INFO 11356 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-09-04 22:43:12.038 INFO 11356 --- [ main] com.example.Demo2Application : Started Demo2Application in 12.204 seconds (JVM running for 12.492)
2016-09-04 22:43:15.931 INFO 11356 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-09-04 22:43:15.931 INFO 11356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-09-04 22:43:15.941 INFO 11356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms