Java DispatcherServlet不';t重定向到适当的视图
在请求适当的视图时,我不断得到404notfoundHTTP响应 这些是我的配置文件 web.xmlJava DispatcherServlet不';t重定向到适当的视图,java,spring-mvc,web.xml,Java,Spring Mvc,Web.xml,在请求适当的视图时,我不断得到404notfoundHTTP响应 这些是我的配置文件 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:sch
<?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>
<?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>
<!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>
entry.html
<?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>
<?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>
<!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>
在此处插入标题
你好,这是头版
我不知道我在哪里犯了错误,我试图更改控制器映射的名称、页面名称、页面扩展名,但我一直得到相同的输出。我通过修改一些配置文件解决了我的问题 只要我想操作html而不是jsp文件,这就意味着我们在寻找静态内容,所以我所要做的就是将我的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="/static/**" location="/WEB-INF/static/"></resources>
</beans:beans>
现在,为了生成适当的视图,viewsolver没有干扰。使用“默认”Servlet来提供资源
这允许将DispatcherServlet映射到“/
”(从而覆盖容器的默认Servlet的映射),同时仍然允许容器的默认Servlet处理静态资源请求。它使用“/**
”的URL映射和相对于其他URL映射的最低优先级配置DefaultServletHttpRequestHandler
此处理程序将所有请求转发到默认Servlet。因此,它在所有其他URL HandlerMappings的顺序中保持最后一个是很重要的。如果您使用
,则会出现这种情况;如果要设置自己的自定义HandlerMapping实例,请确保将其order属性设置为低于DefaultServletHttpRequestHandler的值,即Integer.MAX\u值
要使用默认设置启用该功能,请使用:
@配置
@EnableWebMvc
公共类WebConfig扩展了WebMVCConfigureAdapter{
@凌驾
公共无效配置DefaultServletHandling(DefaultServletHandlerConfigurer-configurer){
configurer.enable();
}
}
或在XML中:
重写“/
”Servlet映射的注意事项是,必须按名称而不是路径检索默认Servlet的RequestDispatcher。DefaultServletHttpRequestHandler将尝试在启动时自动检测容器的默认Servlet,使用大多数主要Servlet容器(包括Tomcat、Jetty、GlassFish、JBoss、Resin、WebLogic和WebSphere)的已知名称列表。如果默认Servlet已使用不同的名称进行自定义配置,或者如果在默认Servlet名称未知的情况下使用不同的Servlet容器,则必须显式提供默认Servlet的名称,如以下示例所示:
@配置
@EnableWebMvc
公共类WebConfig扩展了WebMVCConfigureAdapter{
@凌驾
公共无效配置DefaultServletHandling(DefaultServletHandlerConfigurer-configurer){
启用(“myCustomDefaultServlet”);
}
}
或在XML中:
您的mvc config.xml
:
entry.html文件是否在/WEB-INF/view/目录中?您是否启用了日志记录,如果启用,您是否可以提供日志记录?您确定应用程序的部署没有任何问题吗?您的war文件名是什么?您在浏览器中访问的地址是什么?war文件名:ssytem-ecommerce-prototype-view-0.0.1-SNAPSHOT.war,我确认它在WEB-INF/view/目录中。地址看起来像是这个InternalViewResolver应该在DispatcherServlet在请求和适当的控制器之间进行正确的映射之后进行映射,在本例中是HomeControllerI我不知道另一个servlet的确切含义,根据我的理解,调度器将选择HomeController,并将视图的名称提供给InternalViewResolver,以便使用前缀和后缀进行更新。