代码之家  ›  专栏  ›  技术社区  ›  x__dos

实现SpringMVC 3.0控制器

  •  0
  • x__dos  · 技术社区  · 16 年前

    尝试使用SpringMVC 3.0实现干净的URL(没有.form、.do等)(实际上它是SpringReference中的一个基本示例)。 问题是它不起作用: http://localhost:8080/ct/helloWorld 给出404页。

    以下是我的资料来源,请帮助查找错误。

    helloworldcontroller.java语言

    package controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller()
    public class HelloWorldController {
        @RequestMapping("/helloWorld")
        public ModelAndView helloWorld() {
            ModelAndView mav = new ModelAndView();
            mav.setViewName("helloWorld");
            mav.addObject("message", "Hello World!");
            return mav;
        }
    }
    

    CT-Servel.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:component-scan base-package="controllers" />
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/" />
            <property name="suffix" value=".jsp" />
        </bean>
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
    </beans>
    

    web.xml文件

    <web-app id="WebApp_ID" version="2.4" 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">
        <display-name>
        ct</display-name>
    
    
        <servlet>
        <servlet-name>ct</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    
    <servlet-mapping>
        <servlet-name>ct</servlet-name>
        <url-pattern>/ct/*</url-pattern>
    </servlet-mapping>
    
    
    </web-app>
    

    控制台输出

    12.11.2009 0:49:25 org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:ct' did not find a matching property.
    12.11.2009 0:49:25 org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:\Program Files\PC Connectivity Solution\;C:\PROGRA~1\RATIONAL\RATION~1\NUTCROOT\bin;C:\PROGRA~1\RATIONAL\RATION~1\NUTCROOT\bin\x11;C:\PROGRA~1\RATIONAL\RATION~1\NUTCROOT\mksnt;C:\Program Files\Borland\Delphi7\Bin;C:\Program Files\Borland\Delphi7\Projects\Bpl\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\QuickTime\QTSystem\;e:\my downloads\develop tools\nant-0.86-beta1\bin\;C:\Program Files\Rational\common;C:\Program Files\Rational\ClearQuest;C:\Program Files\Rational\Rose\TopLink\;C:\Program Files\Rational\Rational Test;C:\Program Files\CodeGear\Delphi Prism\bin
    12.11.2009 0:49:25 org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    12.11.2009 0:49:25 org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 769 ms
    12.11.2009 0:49:25 org.apache.catalina.core.StandardService start
    INFO: Starting service Catalina
    12.11.2009 0:49:25 org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.20
    12.11.2009 0:49:26 org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet 'ct'
    12.11.2009 0:49:26 org.springframework.web.servlet.FrameworkServlet initServletBean
    INFO: FrameworkServlet 'ct': initialization started
    12.11.2009 0:49:26 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing WebApplicationContext for namespace 'ct-servlet': startup date [Thu Nov 12 00:49:26 MSK 2009]; root of context hierarchy
    12.11.2009 0:49:26 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/ct-servlet.xml]
    12.11.2009 0:49:27 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@197507c: defining beans [helloWorldController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,viewResolver,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0]; root of factory hierarchy
    12.11.2009 0:49:27 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/helloWorld] onto handler [controllers.HelloWorldController@1c2fff0]
    12.11.2009 0:49:27 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/helloWorld.*] onto handler [controllers.HelloWorldController@1c2fff0]
    12.11.2009 0:49:27 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
    INFO: Mapped URL path [/helloWorld/] onto handler [controllers.HelloWorldController@1c2fff0]
    12.11.2009 0:49:28 org.springframework.web.servlet.FrameworkServlet initServletBean
    INFO: FrameworkServlet 'ct': initialization completed in 2641 ms
    12.11.2009 0:49:28 org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    12.11.2009 0:49:28 org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    12.11.2009 0:49:28 org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/79  config=null
    12.11.2009 0:49:28 org.apache.catalina.startup.Catalina start
    INFO: Server startup in 3451 ms
    

    java project structure http://www.freeimagehosting.net/uploads/566ee12a13.png

    4 回复  |  直到 16 年前
        1
  •  3
  •   gavinjames    16 年前

    web.xml正在将spring dispatcherservlet映射到Web应用程序部署位置顶部的url模式/ct/*上。

    因此,如果将Web应用程序部署到/ct,请尝试 http://localhost:8080/ct/ct/helloWorld

    很可能您希望SpringDispatcherServlet映射到Web应用程序的所有URL。将servlet映射更改为:

    <servlet-mapping>
        <servlet-name>ct</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    您的原始URL应该按预期工作。

        2
  •  2
  •   x__dos    16 年前

    好的,目前的解决方案是:

    Web.XML

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

    helloworldcontroller.java语言

    @Controller
    public class HelloWorldController {
    
        @RequestMapping("/helloWorld")
        public String list() {
            return "helloWorld";
        } 
      }
    

    CT-Servel.xml

    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
        3
  •  0
  •   ideasculptor    16 年前

    请求映射中的路径应该是完整的路径,而不仅仅是pathinfo(web.xml中匹配字符串后的内容)。所以您的请求映射应该指定@requestmapping(“/ct/helloworld”)。

    或者,您的问题可能是您没有将应用程序作为根应用程序运行,而是实际在path/ct上运行。在这种情况下,web.xml不匹配任何内容,因为在比较URL模式以确定servlet上下文时,不会使用指定应用程序上下文的/ct。在这种情况下,您需要访问url/ct/ct/helloworld,以便将请求发送到servlet。但您仍然需要@requestmapping(“/ct/helloworld”),以便将控制器映射到该请求(但它不关心指定应用程序的初始/ct)。

    就我个人而言,我实际上认为基于后缀进行映射是很清楚的,但是要使用通用后缀。我将*.html映射到我的servlet和控制器,它们响应这些请求,并返回html。同样,对于JSON和XML请求,我使用了*.json和*.xml。我在负载均衡器中处理静态HTML和其他静态内容,所以Tomcat永远不会看到它们。

        4
  •  0
  •   toolkit    16 年前

    您是否创建了helloworld.jsp页面?

    我试用了您的代码(注意:使用2.5.6),在浏览器上看到404,在服务器日志中显示以下错误消息:

    File "/path/to/my/WEB-INF/helloWorld.jsp" not found
    

    在哪里? /path/to/my 会对你的环境有所不同。

    如果没有helloworld.jsp,视图解析程序将失败。添加这个JSP,一切都很好。