尝试访问MyController时出现Spring错误404

时间:2013-04-04 14:39:45

标签: spring spring-mvc controller

我是Spring的新手。我想在一个Web应用程序的一部分中集成Spring。 它必须与以下网址一起使用:

http://localhost:9080/myfolder/myspring

我的 web.xml 包括:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>    
    /WEB-INF/config/myspring-context.xml
  </param-value>
</context-param>


    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/config/applicationContext.xml
    </param-value>
  </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>       

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/springviews</url-pattern>
    </servlet-mapping>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

        <!-- registers all of Spring's standard post-processors for annotation-based configuration -->
        <context:annotation-config />

        <tx:annotation-driven/>
        <tx:jta-transaction-manager/>

       <bean id="properties" class="springcop.pojo.TestObject">

        </bean>

    </beans>

我的 myspring-context.xml 是:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="springcop"/>

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven/>

 <!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/springviews/" />
    <property name="suffix" value=".jsp" />
</bean>



</beans>

这是 MyController

package springcop;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {


    public MyController () {
         System.out.println("--->GestioneController");

    }


     @RequestMapping(value = "/test", method = RequestMethod.GET)
      public String test() {
         System.out.println("--->test");
         return "test";
     }

}

向上奔跑没有错误。 Spring似乎工作正如我在我的日志“---&gt; GestioneController中打印出来的那样,它位于我的COntroller的构造函数中。

无论如何,当我在浏览器中打开时

http://localhost:9080/myfolder/myspring/test

在MyController中执行测试方法我得到404错误。

我该怎么做才能让它发挥作用? 感谢。

1 个答案:

答案 0 :(得分:0)

假设您的网络应用名称是问题中列出的myfolder,则网址将为...

http://localhost:9080/myfolder/springviews/test

问题是您将调度程序servlet映射到springviews。您需要在web.xml中按如下方式映射调度程序servlet ...

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

编辑:此外,您已将spring配置放在context-param中,但是您没有添加侦听器来获取它。将以下内容添加到您的web.xml ....

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
相关问题