gpt4 book ai didi

java - DispatcherServlet 未重定向到适当的 View

转载 作者:行者123 更新时间:2023-12-01 08:55:17 26 4
gpt4 key购买 nike

在请求适当的 View 时,我不断收到 404 not found HTTP 响应。

这些是我的配置文件。

web.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>ssytem-ecommerce-prototype-view</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:appContext.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/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

</web-app>

mvc-config.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">

<annotation-driven></annotation-driven>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="ma.pack.net.*"></context:component-scan>

<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".html" />
</beans:bean>

</beans:beans>

HomeController.java

    package ma.pack.net;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "entry";
}
}

entry.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<b>Hello, This is the Front Page</b>
</body>
</html>

我不知道我在哪里犯了错误,我尝试更改 Controller 映射的名称、页面名称、页面扩展名,但我一直得到相同的输出。

最佳答案

依靠“默认”Servlet 来提供资源

这允许将 DispatcherServlet 映射到“/ ”(从而覆盖容器默认 Servlet 的映射),同时仍然允许容器默认 Servlet 处理静态资源请求。它配置了 DefaultServletHttpRequestHandler URL 映射为“/** ”,且相对于其他 URL 映射的优先级最低。

此处理程序会将所有请求转发到默认 Servlet。因此,重要的是它在所有其他 URL HandlerMapping 的顺序中保持在最后。如果您使用 <mvc:annotation-driven> 就会出现这种情况;如果您要设置自己的自定义 HandlerMapping 实例,请务必将其 order 属性设置为低于 DefaultServletHttpRequestHandler 的值,即 Integer.MAX_VALUE .

要使用默认设置启用该功能,请使用:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

}

或者在 XML 中:

<mvc:default-servlet-handler/>

覆盖“/”Servlet 映射的注意事项是,默认 Servlet 的 RequestDispatcher 必须按名称而不是按路径检索。 DefaultServletHttpRequestHandler 将尝试在启动时自动检测容器的默认 Servlet,使用大多数主要 Servlet 容器(包括 Tomcat、Jetty、GlassFish、JBoss、Resin、WebLogic 和 WebSphere)的已知名称列表。如果默认 Servlet 已自定义配置为不同的名称,或者如果正在使用默认 Servlet 名称未知的不同 Servlet 容器,则必须显式提供默认 Servlet 的名称,如下例所示:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable("myCustomDefaultServlet");
}

}

或者在 XML 中:

<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/> 

您的mvc-config.xml :

<mvc:default-servlet-handler />

关于java - DispatcherServlet 未重定向到适当的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42070886/

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