springMVC DispatcherServlet映射的url无法访问到JSP资源
博主JavaWeb新手,想搭建一个博客,使用IDEA+MAVEN+Tomcat+Springmvc,搭建成功后部署war包,通过DispatcherServlet映射的url无法访问到JSP资源,不知道该怎么解决,求好心人帮忙解答疑惑,不胜感激。项目结构和代码如下;
项目结构:
各文件代码如下:
KBlogWebAppInitializer.java:
package KBlog.Config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class KBlogWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/KBlog"}; } }
RootConfig.java package KBlog.Config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @ComponentScan(basePackages = {"KBlog"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)}) public class RootConfig { }
WebConfig.java package KBlog.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan("KBlog.Web") public class WebConfig extends WebMvcConfigurationSupport { @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ configurer.enable(); } }
MainController.java package KBlog.Web.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class MainController { @RequestMapping(value = "/welcome",method = RequestMethod.GET) public String index(){ return "blog"; } }
blog.jsp <html> <head> <title>Title</title> </head> <body> welcome to my blog home,thanks! </body> </html>
部署完成后访问localhost:8000/KBlog/没有问题,能够读取index.jsp页面的数据
但是访问localhost:8000/KBlog/welcome ;显示404错误,望好心人帮忙解答一下疑惑
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那应该放在哪里,resource 里面?能够具体解释一下么?
不要把页面放到WEB-INF下面
webinf下的资源不能直接访问,只能在服务其中转发去访问,大兄弟!
不是吧 ? 我 copy的spring in action 里面的代码。我吧项目包发你,能帮忙具体解决一下么
兄弟webinf下的资源是不能被外部访问的,你只能通过内部的请求转发。