我是Spring Boot Application开发的新手。我正在使用spring boot 1.4.3版本和Spring 4.3.5.RELEASE版本进行简单的spring启动应用程序。我成功配置了Mysql数据库配置。在运行Spring Boot主类时也会创建表。
下面是Spring Boot项目目录结构:
SpringBootApp[boot]
src/main/java
-config
-controller
-dto
-entity
-repository
-service
src/main/resources
-application.properties
src/test/java
src/main/webapp
/recources/css
/resources/images
/resources/js
/WEB-INF/view/jsp/main.jsp
我创建了DBConfig.java
文件,该文件具有与数据源创建,jpa事务管理等数据库相关的功能。
以下是WebMVC配置文件:
@EnableWebMvc
@Configuration
@Import(DBConfig.class)
@ComponentScan(basePackages = { "com.tms" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
以下是App Context初始化文件:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { DBConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
我还在application.properties文件中添加了以下代码行来定位jsp页面并停用百万美元模板。
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
spring.thymeleaf.check-template-location=false
下面是Spring Controller java文件:
@RequestMapping("/")
public class IndexController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("title", "Hello world!");
return "main";
}
}
在jsp示例页面下面:
/WEB-INF/jsp/main.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring Boot</title>
</head>
<body>
<h2>Test Page</h2>
<table>
<tr>
<td>Title</td>
<td>${title}</td>
</tr>
</table>
</body>
</html>
Spring Boot Application成功上手。
以下是STS控制台输出:
2017-01-12 04:22:57.501[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.tool.hbm2ddl.SchemaUpdate [0;39m [2m:[0;39m HHH000228: Running hbm2ddl schema update
[2m2017-01-12 04:22:57.677[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'jpaUnit'
[2m2017-01-12 04:22:58.392[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m Mapped "{[/book]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.tms.dto.BookDto>> com.tms.controller.BookController.getAllBooks()
[2m2017-01-12 04:22:58.400[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 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)
[2m2017-01-12 04:22:58.401[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerMapping[0;39m [2m:[0;39m 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)
[2m2017-01-12 04:22:58.466[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[2m2017-01-12 04:22:58.476[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.w.s.handler.SimpleUrlHandlerMapping [0;39m [2m:[0;39m Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
[2m2017-01-12 04:22:58.596[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.w.s.m.m.a.RequestMappingHandlerAdapter[0;39m [2m:[0;39m Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33d512c1: startup date [Thu Jan 12 04:22:49 IST 2017]; root of context hierarchy
[2m2017-01-12 04:23:00.111[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.j.e.a.AnnotationMBeanExporter [0;39m [2m:[0;39m Registering beans for JMX exposure on startup
[2m2017-01-12 04:23:00.265[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36ms.b.c.e.t.TomcatEmbeddedServletContainer[0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http)
[2m2017-01-12 04:23:00.276[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.tms.config.App [0;39m [2m:[0;39m Started App in 12.241 seconds (JVM running for 13.221)
[2m2017-01-12 04:23:01.999[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring FrameworkServlet 'dispatcherServlet'
[2m2017-01-12 04:23:02.000[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization started
[2m2017-01-12 04:23:02.040[0;39m [32m INFO[0;39m [35m4968[0;39m [2m---[0;39m [2m[nio-8080-exec-1][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m FrameworkServlet 'dispatcherServlet': initialization completed in 40 ms
当我在浏览器中输入网址http://localhost:8080/时,会显示以下消息:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 12 04:24:27 IST 2017
There was an unexpected error (type=Not Found, status=404).
此外,我在浏览器中尝试http://localhost:8080/SpringBootApp/网址将显示以下消息:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 12 04:33:26 IST 2017
There was an unexpected error (type=Not Found, status=404).
/SpringBootApp/
STS控制台中没有打印任何错误消息。
I would be thankful if anybody have solution/ to suggest other approach for the same.
答案 0 :(得分:0)
您的观点位于/ view / jsp /中,但您的视图解析程序仅显示/ view /。在application.properties和代码中都有配置似乎也是错误的。
我个人今天会将Thymeleaf用于服务器端渲染。
答案 1 :(得分:0)
更改
viewResolver.setPrefix("/WEB-INF/view/");
到
viewResolver.setPrefix("/WEB-INF/view/jsp/");