首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Maven项目中的404错误

Spring Maven项目中的404错误
EN

Stack Overflow用户
提问于 2018-06-03 16:48:49
回答 1查看 71关注 0票数 0

我是Spring的新手,正在尝试用Maven开发一个使用spring注解的小应用程序。但我得到的是**“请求的资源不可用”。**我可以理解服务器无法定位请求的资源。但是我不能解决它,所以请在这方面帮助我。

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

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2018-06-03 16:54:59

我假定JSP直接位于WEB-INF文件夹下。如果是这样的话,创建一个子目录'view‘并将JSP移到那里。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50664717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档