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

我有一个提交部分信息的表单,在将信息发送到ReportServlet进行处理之前,ReportController应该先完成信息。当我在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>

...

最佳答案

尝试这个:

return "forward:/ReportServlet";

07-24 20:48