gpt4 book ai didi

java - Spring MVC 404 未找到

转载 作者:行者123 更新时间:2023-12-02 12:52:35 25 4
gpt4 key购买 nike

我已经开始在基于 Maven 的项目中使用 Spring MVC。我的项目目录结构是 enter image description here

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 未找到错误。你能告诉我我在整个设置中犯了什么错误吗?

最佳答案

对于每个 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"); %>

然后你可以将“signin”映射到spring的indexcontroller。其中将显示您的“登录”页面

关于java - Spring MVC 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44559634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com