springMVC DispatcherServlet映射的url无法访问到JSP资源

Kenny100120 发布于 2017/11/05 11:33
阅读 223
收藏 0

博主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错误,望好心人帮忙解答一下疑惑

加载中
0
doglixia
doglixia
兄弟webinf下的资源是不能被外部访问的,你只能通过内部的请求转发。
Kenny100120
Kenny100120
不是吧 ? 我 copy的spring in action 里面的代码。我吧项目包发你,能帮忙具体解决一下么
0
doglixia
doglixia
webinf下的资源不能直接访问,只能在服务其中转发去访问,大兄弟!
0
苏别v
苏别v

不要把页面放到WEB-INF下面

Kenny100120
Kenny100120
那应该放在哪里,resource 里面?能够具体解释一下么?
返回顶部
顶部