我使用注释配置了Spring Boot
我有以下文件
1)用于配置弹簧启动的AppStarter类
@Configuration @EnableAutoConfiguration @PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = true) @ComponentScan(basePackages = "com.sample.config") public class AppStarter extends SpringBootServletInitializer{
@Value("${server.contextPath}") private String contextPath; @Value("${server.port:8080}") private String port; @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(Integer.valueOf(port)); factory.setContextPath(contextPath); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); return factory; } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(AppStarter.class); } public static void main(String[] args) { SpringApplication.run(AppStarter.class, args); } }
@Value("${server.contextPath}") private String contextPath; @Value("${server.port:8080}") private String port; @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(Integer.valueOf(port)); factory.setContextPath(contextPath); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); return factory; } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(AppStarter.class); } public static void main(String[] args) { SpringApplication.run(AppStarter.class, args); } }
2)WebConfig类
3)app.properties
@Configuration
@ComponentScan(basePackages = {"com.sample.rest"})
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter
{
@Bean
public static PropertySourcesPlaceholderConfigurer properties()
{
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{new ClassPathResource("app.properties")};
configurer.setLocations(resources);
configurer.setIgnoreUnresolvablePlaceholders(true);
return configurer;
}
@Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
4)UserController类
core_pool_size = 100
max_pool_size = 600
queue_capacity = 160
server.port=8080
spring.view.prefix: /WEB-INF/pages/
spring.view.suffix: .jsp
5)的pom.xml
@Controller
public class UserController extends AbstractController {
@RequestMapping(value = "/user/add")
public String user()
{
return "adduser";
}
}
6)webdu / WEB_INF / pages文件夹中的adduser.jsp
当我尝试通过localhost访问我的jsp时:8080 / sample / user / add我得到:
org.springframework.web.servlet.PageNotFound |在DispatcherServlet中找不到带有URI [/sample/WEB-INF/pages/adduser.jsp]的HTTP请求的映射,其名称为' dispatcherServlet'
任何人都可以就此问题提供任何帮助吗?
答案 0 :(得分:1)
spring.view.prefix: /WEB-INF/jsp/
webapp/WEB_INF/pages
/jsp
vs,/pages
您需要更改其中一个,以便它们匹配!
第二:你需要请求url(这是在你的控制器上写的,而不是jsp的路径!
所以使用:
localhost:8080/<yourAppName>/user/add
代替/sample/WEB-INF/pages/adduser.jsp
答案 1 :(得分:0)
使用localhost:8080 / samples / user / add,您需要将控制器代码更改为此,
@Controller
@RequestMapping("/samples")
public class UserController extends AbstractController {
@RequestMapping(value = "/user/add")
public String user()
{
return "adduser";
}
}
答案 2 :(得分:0)
您似乎已配置了嵌入式Tomcat容器。嵌入Tomcat时不支持JSP页面。 http://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#boot-features-jsp-limitations
答案 3 :(得分:0)
试试这个:
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}