实现Spring MVC 3.0控制器

时间:2009-11-11 22:04:15

标签: spring-mvc

尝试使用Spring MVC 3.0实现干净的URL(没有.form,.do等)(实际上它是Spring参考的基本示例)。 它不起作用的问题: 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-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: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

4 个答案:

答案 0 :(得分:3)

您的web.xml将Spring DispatcherServlet映射到您的Web应用程序部署位置之上的url pattern / ct / *。

因此,如果您的Web应用程序正在部署到/ ct,请尝试转到http://localhost:8080/ct/ct/helloWorld

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

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

并且您的原始网址应该按预期工作。

答案 1 :(得分:2)

好的,现在的解决方案是:

<强>的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-servlet.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" />

答案 2 :(得分:0)

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

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

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

/path/to/my对于您的环境而言会有所不同。

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

答案 3 :(得分:0)

RequestMapping中的路径应该是完整路径,而不仅仅是pathInfo(web.xml中匹配字符串之后的内容)。所以你的RequestMapping应该指定@RequestMapping(“/ ct / HelloWorld”)

或者,您的问题可能是您没有以root应用程序的身份运行应用程序并且实际上是在路径/ ct上运行。在这种情况下,您的web.xml不匹配任何内容,因为在比较url-patterns时不使用指定应用程序上下文的/ ct来确定servlet上下文。在这种情况下,您需要访问url / ct / ct / HelloWorld才能将请求发送到您的servlet。但是你仍然需要@RequestMapping(“/ ct / HelloWorld”)才能将控制器映射到该请求(但它并不关心指定应用程序的初始/ ct)。

就个人而言,我实际上认为基于后缀进行映射是明确的,但使用通用后缀。我将* .html映射到我的servlet和控制器,它们响应那些请求发回html。同样,我对* json和xml请求使用* .json和* .xml。我在负载均衡器处理静态html和其他静态内容,因此tomcat永远不会看到它们。