使用注释驱动配置时Spring MVC 404错误

时间:2014-04-03 21:55:53

标签: spring spring-mvc

我收到以下错误:

  

请求的资源(/ SpringWebApp /)不可用。

我的Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <display-name>Spring Web MVC Application</display-name>

    <!-- Processes application requests -->

    <servlet>
        <servlet-name>MVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
     </init-param>

        <load-on-startup>1</load-on-startup>        

    </servlet>

    <servlet-mapping>
        <servlet-name>MVC</servlet-name>
            <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

MyFirstController:

package com.assyst.SpringWebApp;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;



@Controller

    public class MyFirstController 
    {


        @RequestMapping(value="/")
        protected ModelAndView ServletContainer(HttpServletRequest request,HttpServletResponse response) throws Exception 
                {

                        ModelAndView model = new ModelAndView("HomePage");
                        model.addObject("msg", "welcome to spring mvc hello world");
                        return model;


                }
    }

servlet的context.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

      <annotation-driven />
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
   <!--<beans:bean name="/" class="com.assyst.SpringWebApp.MyFirstController"></beans:bean> -->

     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
     <beans:bean name="response" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

      <context:component-scan base-package="com.assyst.SpringWebApp" />



</beans:beans>

我用来访问它的网址是:http://localhost:8080/SpringWebApp/ 我的应用服务器是jboss 7.

我在做什么错了?

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

你们是对的/ SpringWebApp Context是我把它切换到SpringMVCWorking2的问题而且它有效。它发现的Spring-osgi-annotation.jar文件问题只是在eclipse中发生的零星问题我删除了错误消息然后重新启动了项目并且它已经消失了。

感谢您的帮助