Why won't Spring MVC app display?

2019-07-20 14:17发布

Here is my app project structure:

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

My 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>

My 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>

Here is the structure of the packaged WAR (after I bundle it up with Ant):

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

When I deploy this to Tomcat and go to http://localhost:8080/myapp/hello.htm I get a 404 Not Found error from Tomcat.

Edit: The objective is (obviously) to go to that URL and see my Hello.jsp page.

Where am I going wrong?

Update: my 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;
    }
}

1条回答
别忘想泡老子
2楼-- · 2019-07-20 14:51

In web.xml file you need to change * into *.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>
查看更多
登录 后发表回答