坚持为新手推出Spring MVC教程

时间:2010-06-02 21:05:57

标签: java spring-mvc

我对新手的Spring MVC教程感兴趣: http://sites.google.com/site/springmvcnetbeans/step-by-step/

我是1.12“尝试申请”。当我放入我的地址栏hello.htm它不起作用,但hello.jsp工作。但我想有点隐藏这个JSP扩展。

我精确地遵循了教程。 我可能做错了什么?

skaffman建议发布web.xml和servlet映射xml文件,所以这里是。我只添加了教程所说的内容(但是使用另一个软件包名称,其余是由NetBeans生成的)

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springsample</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springsample</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

springsample-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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

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

</beans>

2 个答案:

答案 0 :(得分:1)

我可以看到几个不同之处。本教程中的文件名为springapp-servlet.xml,而不是springsample-servlet.xml(参见步骤1.8),它只包含一个bean,即hello.htm bean。整个事情中没有提到springsample-servlet.xml

答案 1 :(得分:0)

本教程告诉您将文件放在webapp根目录web / hello.jsp中,但是你已经告诉spring在/WEB-INF/jsp/hello.jsp中查找它你是否将文件移动到该文件夹​​中?您可以在/springsample/hello.jsp找到它的事实表明您没有。