2016-09-29 33 views
-1

我试图做一个简单的Web应用程序,其中有一个登录和一个欢迎使用Spring MVC的页面。代码如下:警告:没有映射发现与uri [/鉴定/]在名称为'春'的dispatcherservlet http请求

的web.xml

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

    <display-name>Spring MVC Application</display-name> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
    </servlet> 

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

</web-app> 

为spring-servlet.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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="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">  

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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix" value="/WEB-INF/jsp/"></property> 
<property name="suffix" value=".jsp"></property> 
</bean> 

</beans> 

Controller.java

@Controller 
@RequestMapping("/Authentication") 
public class TestController{ 
    @RequestMapping(value="/") 
    public String Login(){ 
     return "Login"; 
    } 

    @RequestMapping(value="Authenticate", method=RequestMethod.GET) 
    public String Authenticate(){ 
     //Authenticates and returns "Welcome" 
    } 
} 

项目名称为身份验证。/WEB-INF/jsp /下有Login.jsp和Welcome.jsp。

然而,当我试图运行项目,我收到HTTP状态404错误和以下警告:

org.springframework.web.servlet.PageNotFound noHandlerFound 警告:没有找到映射为HTTP请求与uri [/ Authentication /]在名为'spring'的dispatcherservlet中

即使映射看起来很好,我为什么会收到此警告?

+1

[欢迎,编号338(http://stackoverflow.com/search?q=is%3Aq+%22No+mapping+found+for+http+request+与+ URI%22 +%22英寸+的DispatcherServlet +与+名称%22)。 – BalusC

+0

您使用哪个网址访问您的应用程序?哪个是TestController类的包? – amicoderozer

回答

-1

您没有指定spring-servlet的位置。这些添加到您的web.xml文件:

<listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring-servlet.xml 
    </param-value> 
</context-param> 
+0

他们的servlet被命名为'spring'。 Spring假定上下文文件存在于该servlet上下文的'/ WEB-INF/ -servlet.xml'中。你为什么在这里引入一个'ContextLoaderListener'? –

+0

从文档:ContextLoaderListener支持通过ContextLoaderListener(WebApplicationContext)构造函数注入根Web应用程序上下文,从而允许在Servlet 3.0+环境中进行编程式配置。另外@BalusC你可以参考[这里](http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring-frame) – sirandy

+0

这不解释为什么需要。在这种情况下,DispatcherServlet的上下文已经足够了。 –

相关问题