简单的弹簧mvc输出程序有什么问题?

时间:2014-01-09 09:25:51

标签: java spring jsp spring-mvc

我有 [web.xml]

<web-app 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_2_5.xsd"
          version="2.5">    


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



    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>

[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"
       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-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">



<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /home=HomeController
        </value>
    </property>
</bean> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="1"></property>
</bean>

<bean name="HomeController" class="com.spring.mvcc.HomeController"></bean>

</beans>

[HomeController.java]

@Controller
public class HomeController {

    @RequestMapping({"/","/home"})
    public String showHomePage(Map<String, Object> model){
        model.put("spittles","works");

        return "home";     
    }
}

[针对home.jsp]

<html>
<body>
   <h2>HI!</h2>
</body>
</html>

当我尝试通过浏览器运行时(我将 Tomcat 端口更改为 9090 ): localhost:9090 // MyProject / home - 结果:什么都没发生。只是一个空白页面。

这有什么问题?

2 个答案:

答案 0 :(得分:0)

Home.jsp是视图,你应该访问localhost:9090 // MyProject / home

答案 1 :(得分:0)

我认为问题出在你的配置上。 首先,您正在混合XML配置和注释配置,IMO通常不是一件好事(可能会引起读者和Spring的混淆)。其次,你没有告诉Spring在任何地方实际寻找注释配置。

我将做的是以下内容,

<强> [调度员的servlet]

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="1"></property>
</bean>

<context:component-scan base-package="your.package.here" />
<mvc:annotation-driven />    

请注意我添加了两个名称空间(mvc和context),并删除了SimpleUrlHandlerMapping和HomeController bean。

但是魔法发生在最后两行,他们基本上“激活”了你的注释。

有一些方法可以在没有注释的情况下执行此操作,扩展MultiActionController可能是一种方式,但看起来你想要注释(并且基于Spring文档这些天它似乎是首选方式去。)

编辑:此外,默认情况下,DefaultAnnotationHandlerMapping从3.2开始不推荐使用。但是随着添加的命名空间,它似乎无用。我会删除它。

相关问题