为什么不会赢得Spring MVC app?

时间:2014-04-28 16:09:44

标签: java spring jsp tomcat 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错误。

修改:目标(显然)转到该网址并查看我的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;
    }
}

1 个答案:

答案 0 :(得分:2)

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