2013-04-04 72 views
0

我新的春天。我正在尝试将Spring集成到Web应用程序的一部分中。 它有像URL的工作:春错误404试图访问myController的

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上下文。 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"; 
    } 

} 

向上是没有错误runniong。春天似乎工作,因为我已经在我的日志“---> GestioneController这是在我的控制器的构造函数。

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

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

执行测试打印出来在方法myController的,我得到404错误。

什么是我应该做的,使其工作? 感谢。

回答

0

假设你的web应用的名称是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> 
+0

谢谢@hyness我会明天测试它,让你知道。 RequestMapping中的value =“/ test”是否正确? – Gyonder 2013-04-04 16:04:17

+0

你好@hyness,我已经修改如你所说但404错误,这在日志中:PageNotFoundW¯¯org.springframework.web.servlet.DispatcherServlet noHandlerFound未发现与URI [/ MyFolder文件/ springviews /测试]在DispatcherServlet的HTTP请求映射与名称'调度员' – Gyonder 2013-04-05 07:15:06

+0

我已经更新了我的答案,如果这仍然没有回答你的问题,那么你需要发布完整的文件不仅包括部分以及任何来自日志的错误消息。如果确实解决了您的问题,请将答案标记为已接受。 – hyness 2013-04-09 14:04:49