gpt4 book ai didi

java - 为什么 Spring MVC 应用程序不显示?

转载 作者:行者123 更新时间:2023-11-30 09:08:49 25 4
gpt4 key购买 nike

这是我的应用项目结构:

myapp/
src/main/java/
com.me.myapp.controllers.HelloController
src/main/config/
mvc-dispatcher-servlet.xml
web.xml
pages/
Hello.jsp
lib/
<JARs>
build.xml

我的mvc-dispatcher-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean name="/hello.htm"
class="com.me.myapp.controllers.HelloController" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

我的web.xml:

<web-app 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"
version="2.4">
<display-name>Partner Demo</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

这是打包后的 WAR 的结构(我将其与 Ant 捆绑后):

myapp.war
META-INF/
WEB-INF/
classes/
lib/
pages/
Hello.jsp
mvc-dispatcher-servlet.xml
web.xml

当我将其部署到 Tomcat 并转到 http://localhost:8080/myapp/hello.htm 时,我从 Tomcat 收到 404 Not Found 错误。

编辑:目标(显然)是转到该 URL 并查看我的 Hello.jsp 页面。

我哪里错了?

更新:我的HelloController:

public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView model = new ModelAndView("Hello");
model.addObject("msg", "This should be visible.");

return model;
}
}

最佳答案

在web.xml文件中你需要把*改成*.htm

<web-app 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"
version="2.4">
<display-name>Partner Demo</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>

关于java - 为什么 Spring MVC 应用程序不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23345997/

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