[英]HTTP Status 404 on jsp spring mvc
I'm trying to develop a spring MVC project which has a index.jsp file as it's view. 我正在尝试开发一个Spring MVC项目,该项目的视图具有index.jsp文件。 I searched and found I should do this by the help of two below configuration classes:
我搜索并发现应该通过以下两个配置类来执行此操作:
public class MainInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
public static HashMap<String, String> response_code = new HashMap<String, String>();
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { MainConfiguration.class,
WebSocketConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
Security.addProvider(new BouncyCastleProvider());
servletContext.addListener(new MainContextListener());
}
}
and this is MainConfiguration class: 这是MainConfiguration类:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "-----")
public class MainConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/Content/**")
.addResourceLocations("/Content/");
registry.addResourceHandler("/Scripts/**")
.addResourceLocations("/Scripts/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
I've configured my project to run on tomcat web-server inside intellij. 我已经将项目配置为在intellij的tomcat Web服务器上运行。 but when index.jsp is directly in path : webapp/index.jsp it will be opened in browser after running the program but when I move it to a subfolder it doesn't.
但是当index.jsp直接位于path:webapp / index.jsp中时,它将在运行程序后在浏览器中打开,但是当我将其移至子文件夹时则不会。
Shouldn't this part of code give the address to spring so it find the url and don't give a 404 error? 这部分代码不应该给spring提供地址,以便它找到url而不给出404错误吗?
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
You need to annotate your MainConfiguration.java with @Configuration.
您需要使用
@Configuration.
注释MainConfiguration.java @Configuration.
Also, you require to register an instance of the DispatcherServlet
in the servlet context. 另外,您需要在Servlet上下文中注册
DispatcherServlet
的实例。 Actually, you can refer - spring mvc with jsp example for detailed configurations. 实际上,您可以参考- 带jsp示例的spring mvc以获取详细配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.