一尘不染

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

spring-mvc

这是我的应用程序项目结构:

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;
    }
}

阅读 344

收藏
2020-06-01

共1个答案

一尘不染

在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>
2020-06-01