【发布时间】:2015-03-24 08:31:29
【问题描述】:
我想创建一个完全不使用任何 XML 的 spring 应用程序(没有 web.xml 没有 context.xml 或任何东西)。到目前为止,它似乎工作得很好,只是我的视图解析器有一些问题,我无法自己解决。
这是我的 WebApplicationInitializer
public class AppConfig implements WebApplicationInitializer {
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("fi.cogniti.service.config");
return context;
}
@Override
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(
context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
// Enabling spring security
// servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain"))
// .addMappingForUrlPatterns(null, false, "/*");
}
}
还有我的spring配置
@Configuration
@EnableWebMvc
@ComponentScan("fi.cogniti.service")
public class SpringConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
最后是我的控制器
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping
public String entry() {
return "index";
}
}
index.jsp 位于src/main/webapp/pages/index.jsp。
所以,如果在我的控制器中我使用注释@ResponseBody,那么控制器会给我响应“索引”,因此我知道我的配置至少在某种程度上有效,但是,如果我希望删除注释它将返回index.jsp 的内容,我只得到一个 404 错误。
有什么建议吗?
【问题讨论】:
标签: java spring jsp spring-mvc jakarta-ee