Spring Maven项目中的404错误

时间:2018-06-03 08:48:49

标签: spring maven http-status-code-404

enter image description here我是Spring的新手,并尝试使用Maven的spring注释开发一个小应用程序。但是我得到**"请求的资源不可用。" **我可以理解服务器无法找到所请求的资源。但是我无法解决它,所以请帮助我。

以下是我的项目结构和代码: -

SpringRootConfig.java
package com.capp.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
@Configurable
@ComponentScan( basePackages = {"com.capp"})
public class SpringRootConfig {


}

    package com.capp.config;

    import org.springframework.beans.factory.annotation.Configurable;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;

    @SuppressWarnings("deprecation")
    @Configurable
    @ComponentScan( basePackages = {"com.capp"})
    @EnableWebMvc
    public class SpringWebConfig extends WebMvcConfigurerAdapter {


        public void addResourceHandlers(ResourceHandlerRegistry registry) {


        }

        @Bean
        public ViewResolver viewResolver() {

            InternalResourceViewResolver vr =new InternalResourceViewResolver();
            vr.setViewClass(JstlView.class);
            vr.setPrefix("/WEB-INF/view/");
            vr.setSuffix(".jsp");
            return vr;
        }
    }
    ContactAppDispatcherServletIntializer.java
    package com.capp.config;

    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;

    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    public class ContactAppDispatcherServletIntializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] {SpringRootConfig.class};
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            // TODO Auto-generated method stub
            return new Class[] {SpringRootConfig.class};
        }

        @Override
        protected String[] getServletMappings() {
            // TODO Auto-generated method stub
            return new String[] {"/"};
        }

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            // TODO Auto-generated method stub
            super.onStartup(servletContext);
        }




    }
TestController.java    
package com.capp.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Controller
    public class TestController {

        @RequestMapping(value="/test/hello")
        public String helloWorld() {

            return "hello";
        }

    }

当我使用http://localhost:8080/SpringContactApp/test/hello运行应用程序时无法找到WEB-INF文件夹下的hello.jsp。enter image description here

1 个答案:

答案 0 :(得分:0)

我认为JSP直接位于WEB-INF文件夹下。如果是这种情况,请创建子目录“view”并将JSP移动到那里。

在视图解析器中,您将前缀设置为“/ WEB-INF / view /”,您的视图资源必须位于该位置。