我正在尝试创建一个简单的Web应用程序,它具有使用Spring MVC的登录和欢迎页面。代码如下:
的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>
弹簧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"
}
}
项目名称为Authentication。 / WEB-INF / jsp /.
下有Login.jsp和Welcome.jsp但是,当我尝试运行该项目时,我收到HTTP状态404错误并发出以下警告:
org.springframework.web.servlet.PageNotFound noHandlerFound 警告:在名为'spring'的dispatcherservlet中找不到带有uri [/ Authentication /]的http请求的映射
为什么即使映射看起来很好,我也会收到此警告?
答案 0 :(得分:-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>