奇怪的错误:在URL映射Spring MVC上找不到资源 [英] strange error: resource not found on url mapping spring mvc

查看:101
本文介绍了奇怪的错误:在URL映射Spring MVC上找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切似乎都很好,但是当我启动应用程序时,出现一个奇怪的资源错误错误. 这是我的控制器类

Everything seems alright but when I launch my application, I get a strange error of resource not found. This is my controller class

@Controller
public class HomeController {

        @Autowired
        private IMusicStoreService musicStoreService;

    @RequestMapping(value="/")
    public ModelAndView test(HttpServletResponse response) throws IOException{

        return new ModelAndView("home","musicDetForm",new MusicDetails());
    }

        @RequestMapping(value="AddSong",method = RequestMethod.POST)
        @ResponseBody
        public String addSong(@ModelAttribute("musicDetForm") MusicDetails musicDetails){
           return musicStoreService.addSong(musicDetails);
        }

        @RequestMapping(value = "SongList/",method = RequestMethod.GET)
        @ResponseBody
        public List<MusicDetails> getSongList(){
            return musicStoreService.getSongList();
        }
}

这是我pom.xml文件中的代码部分

This is a code section from my pom.xml file

<properties>
    <failOnMissingWebXml>false</failOnMissingWebXml>
        <java.version>1.6</java.version>
        <spring.version>3.1.0.RELEASE</spring.version>
        <cglib.version>2.2.2</cglib.version>
</properties>

这是我的web.xml文件

This is my web.xml file

<display-name>MusicStore</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>aish.vaishno.musicstore</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

当我使用sts在tomcat上启动我的应用程序时,我的构建没有显示任何错误,但出现此错误

When I launch my application on tomcat using sts, no error is shown on my build but I get this error

HTTP Status 404 - /MusicStore/

尽管我的映射看起来还可以,但为什么仍然出现上述错误?

Why am I getting the above error despite that my mapping seems okay.

推荐答案

您是否将MusicStore存储在WEB-INF/中? 如果将其设置为web.xml中的索引,则可以访问

Are you storing the MusicStore inside the WEB-INF/? It is okay to access if you set it as the index in web.xml

但是,一旦发送请求,响应通常就会发送到您提供的URL,并且应该是"JSP"文件,而不是servlet.

However,once you send request ,the response normally send to the URL you provided, and should be a 'JSP' file,but not servlet.

在您的情况下,代码混乱了很多,我没有看到任何View或JSP或网页跟随您的MusicStore控制器,请确保您可以直接打印它,但这不是一个好习惯.

In your case, the code are messed up quite a bit,i didn't see any View or JSP or web page to follow your MusicStore controller, sure you can directly print it but that's not a good practice.

如果您可以提供更多的代码内容,则可能会更有帮助. 现在的解决方案是,当您第一次加载servlet或检查映射时,是否可以访问索引页面.

If you could provide more of the code content,it may be more helpful. The solution is now see if you could access index page when you first load your servlet, or check the mapping.

这篇关于奇怪的错误:在URL映射Spring MVC上找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆