从Spring控制器调用servlet

时间:2013-03-21 16:38:06

标签: java spring java-ee tomcat servlets

我有一个Spring控制器( ReportController )和一个常规servlet( ReportServlet )。

我有一个提交部分信息的表单, ReportController 应该在将信息发送到 ReportServlet 进行处理之前完成这些信息。当我在ReportController方法中执行此操作时:

return "/ReportServlet"; 

它尝试解析为/views/ReportServlet(而不是直接/ReportServlet)。

如何从Spring Controller调用我的servlet?

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app ...>

    <display-name>MyProject</display-name>

    <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>*.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/loginform.html</url-pattern>
    </servlet-mapping>

    ...

    <servlet>
        <servlet-name>ReportServlet</servlet-name>
        <servlet-class>com.mypackage.ReportServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ReportServlet</servlet-name>
        <url-pattern>/ReportServlet</url-pattern>
    </servlet-mapping> 
   ...
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>/loginform.html</welcome-file>
    </welcome-file-list>

</web-app>

和我的dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
      ...
>

    <!-- Enable annotation driven controllers, validation etc... -->

    <mvc:annotation-driven />
    <context:component-scan base-package="com.p2p.reportsportal.controllers" />
    <context:property-placeholder location="classpath*:config.properties"/>
    <bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

...

1 个答案:

答案 0 :(得分:3)

试试这个:

return "forward:/ReportServlet";