Struts 2 迁移到 spring mvc [英] Struts 2 migration to spring mvc

查看:47
本文介绍了Struts 2 迁移到 spring mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于将 Struts 2 应用程序迁移到 spring mvc 框架的早期阶段.我已经在项目中添加了几个 spring 模块,包括 spring 核心、spring 安全性,现在我将尝试摆脱 struts,转而使用 spring mvc.

I'm in the very early stages of migrating a Struts 2 application to the spring mvc framework. I've already added several spring modules to the project including spring core, spring security and now I'll be trying to move off of struts in favor of spring mvc.

不过我遇到了困难 - 我正在尝试将我的一些 struts 操作重新映射到 bean.这是一个例子:

I'm running into difficulties though - I'm trying to re-map some of my struts actions to beans. Here's an example :

我在我的 struts.xml 文件中配置了一个操作:

I have an action configured in my struts.xml file :

<package name="default" extends="struts-default">
    <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>
    ...
    <action name="tools" class="com.carfax.pb.dashboard.processing.action.RerunEventsAction" method="getAllRerunEvents">
        <result name="success">/WEB-INF/jsp/tools/home.jsp</result>
    </action>
    ...
</package>

所以基本上我有一个 tools.home jsp 页面,它是上面定义的操作的视图.我为这个视图创建了一个控制器类(基本上只是从动作中取出实现并将它移到一个常规的控制器类中):

So basically I have a tools.home jsp page which is the view for the action defined above it. I've created a controller class for this view (basically just took the implementation out of the action and moved it into a groovy controller class) :

@Controller
@RequestMapping("/tools")
class RerunEventsController {

...

    public String getAllRerunEvents() {

        ...
        return ActionSupport.SUCCESS;
    }    


...
}

现在我想弄清楚如何将这两个连接起来,这就是我遇到困难的地方.

Now I'm trying to figure out how to wire up these two and that's where I'm having difficulties.

  1. 我不知道如何让 struts 推迟在我的 struts.xml 中定义的映射(我仍然希望支持其余的 struts 操作,因为我将把这些操作一个一个地移到控制器中.

  1. I don't know how to make struts defer from the mapping defined in my struts.xml (I still want the remaining struts actions to be supported as I'll be moving the actions into controllers one by one.

我不知道如何正确配置从命名空间到控制器的映射以供查看.

I don't know how to correctly configure the mapping from namespace to controller to view.

这是我尝试过的 -

web.xml:

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

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/PBDashboard/*</url-pattern>
</servlet-mapping>

servlet.xml:

servlet.xml:

//Spring 应该自动找到我的控制器,因为它存在于这个包中

// Spring should automatically find my controller as it exists inside this package

<context:component-scan base-package="com.carfax.pb.dashboard.processing.action" />

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

我的 web.xml 有一个 Struts2 过滤器和过滤器映射设置如下:

My web.xml has both a Struts2 filter and filter mapping set up as follows :

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

当我导航到 appname/tools 时,我收到一个 struts 错误,提示没有为命名空间/和操作名称工具映射的操作"

When I navigate to appname/tools I get a struts error saying that "There is no Action mapped for namespace / and action name tools"

这显然是正确的,但我觉得我已经建立了一个 spring mvc 应该选择的映射.

This is obviously correct but I feel like I've set up a mapping that spring mvc should pick up.

谁能告诉我一些有关如何正确执行此操作/更好的信息并指出我的错误的信息?

Could anyone point me to some information regarding how to correctly do this / be even nicer and point out my mistakes?

推荐答案

如果我是对的,你的 web.xml 也应该有 Struts 过滤器,在/* 上.然后,您可以将 DispatcherServlet 映射为默认 servlet (/),并使用以下内容排除 struts.xml 中的迁移操作:

If I'm correct your web.xml should also have the Struts filter, on /*. Then you could map your DispatcherServlet as the default servlet (/), and exclude the migrated actions in your struts.xml using something like:

<constant name="struts.action.excludePattern" value="/tools"/>

Struts 然后应该处理每个请求,除了/tools,并将/tools URL 留给 Spring.

Struts should then handle every request, except for /tools, and leave the /tools URL up to Spring.

这篇关于Struts 2 迁移到 spring mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆