我正在为Spring MVC web-app
的servlet配置而苦苦挣扎。
- 我正在使用Intellij IDEA IDE
- 在Maven的pom.xml中引入了版本1.1.8.RELEASE的
spring-boot-starter-parent
作为父级,spring-boot-starter-web
和spring-webmvc
作为依赖项 - 使用以下bean定义了一个Config类:
`
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"sblog", "sblog.controller", "sblog.repository", "sblog.service"})
@EnableJpaRepositories("sblog.repository")
@EnableTransactionManagement
@EnableWebMvc
public class Config {
...
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public ServletRegistrationBean dispatcherRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet(), "/*", "/index");
registration.setLoadOnStartup(1);
System.err.println(registration.getServletName());
System.err.println(registration.getUrlMappings());
return registration;
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/resources/");
resolver.setSuffix(".jsp");
return resolver;
}
- 定义了类应用程序:
`
public class Application {
public static void main(String[] args) {
SpringApplication.run(Config.class, args);
}
}
`-增加了一个简单的控制器:
`
@Controller
public class ApplicationController {
@RequestMapping(method = GET, value = "/")
public String index(Model model) {
System.err.println("Index");
return "index";
}
- 并定义了以下项目结构:
`
sblog/
├── pom.xml
├── sblog.iml
├── src
│ ├── main
│ │ └── java
│ │ └── sblog
│ │ ├── Application.java
│ │ ├── Config.java
│ │ ├── controller
│ │ │ ├── ApplicationController.java
│ │ │ ├── PageController.java
│ │ │ ├── PostController.java
│ │ │ └── SessionController.java
│ │ ├── orm
│ │ │ ├── Author.java
│ │ │ └── Post.java
│ │ ├── repository
│ │ │ ├── AuthorRepository.java
│ │ │ └── PostRepository.java
│ │ ├── service
│ │ │ └── PostService.java
│ │ └── WebMVCApplicationInitializer.java
│ └── resources
│ └── index.jsp
`
但每次我访问根目录时,我都会得到o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/resources/index.jsp] in DispatcherServlet with name 'dispatcherServletRegistration'
提前感谢您,如有任何帮助,我们将不胜感激!:)
首先,我建议阅读一下Spring Boot可以为您做些什么,您正在努力不使用Spring Boot。
我建议删除除@Configuration
、@EnableAutoConfiguration
和@ComponentScan
之外的所有注释。其他一切都将由Spring Boot添加/提供。我还建议将Config
和Application
类合并为一个类。留给您以下内容(是,没有@Bean
方法)。。
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Config.class, args);
}
}
您只需要添加spring-boot-starter-web
和spring-boot-starter-data-jpa
作为依赖项,其他必要的依赖项(spring-webmvc
等)将自动添加。当然,您仍然需要添加hibernate作为依赖项。
Spring Boot将检测Spring Web并为您配置DispatcherServlet
和其他bean。
接下来,src/resources
目录是未知的,因此它将无法作为资源使用,因此也无法找到。我还建议不要使用JSP作为视图层。参考指南中解释了JSP和Spring Boot的一些问题。如果您真的不必显式配置InternalResourceViewResolver
,而是向src/main/resources
添加一个application.properties
文件,并将以下内容添加到属性中。
spring.view.prefix=
spring.view.suffix=.jsp
对应于CCD_ 20的前缀和后缀属性。有关属性列表,请查看参考指南。
我建议不要使用src/main/resources/resources
文件夹来存储JSP页面,因为该目录也是公共可读的(在Spring Boot中默认情况下)。例如,我建议使用views
目录。这将导致以下属性
spring.view.prefix=/views/
spring.view.suffix=.jsp
最后要注意的是,Spring Boot驱动的应用程序不会检测到WebApplicationInitializer
,因此如果您在其中执行某些操作,则不会对Spring Boot应用程序执行此操作(除非是特定的Spring Boot WebApplicationInitializer
)。
首先,我使用了相同的结构,只是我的"resources"文件夹在"main"文件夹中。其次,我使用了如下配置。
@Configuration
@EnableAutoConfiguration
public class CoreConfig {
@Bean
public WebMvcConfigurerAdapter initAdapter(){
return new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/resources/");
}
};
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setSuffix(".jsp");
return resolver;
}
}
如果你想了解更多细节,请查看我在GitHub上的示例项目。