[英]Spring MVC 404 Not Found
我已经开始在基于Maven的项目中使用Spring MVC。 我的项目目录结构是
web.xml的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<module-name>Admin Application</module-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servletContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servetContext的内容是
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.web.controller"/>
<bean name="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/config.properties</value>
</list>
</property>
</bean>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
在LoginController中,我定义了一个将对初始HttpRequest处理的方法
@Controller
public class LoginController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String signIn() {
return "sign-in";
}
}
当我使用http:// localhost:8080 / AdminApplication /请求登录页面时
它给我404 not found错误。 您能告诉我我在这里为整个设置所做的错误吗?
对于每个Web应用程序,您应该有一个索引页直接位于webapp文件夹下,这是访问应用程序时加载的第一页。
将此包含在您的web.xml中
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
重定向页面(jsp)可以像这样:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("signin"); %>
然后,您可以将“登录”映射到spring的indexcontroller。 哪个页面将显示“登录”页面
我在Parent POM.xml中犯了一个错误,在那里我定义了为spring-mvc提供的作用域,以便它不与lib文件夹一起打包。 这就是为什么找不到调度程序类的原因。 我已经检查了日志并解决了问题。 感谢你所做的一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.