Apache Struts 2 Documentation > Home > Guides > Plugin Developers Guide > Struts 1 Plugin |
The Struts 1 plugin allows you to use existing Struts 1 Actions and ActionForms in Struts 2 applications.
This plugin provides a generic Struts 2 Action class to wrap an existing Struts 1 Action, org.apache.struts2.s1.Struts1Action. The wrapper class provides the expected calls to the legacy Struts 1 Action and ActionForm, converting incoming and outgoing objects into the expected forms. It works by using the scoped model driven technique that uses a single Object as the model for every page, very similar to how the Struts 1 ActionForm works. The plugin also provides several interceptors to emulate Struts 1 logic:
To use existing Struts 1 Actions and ActionForms in a Struts 2 application, create a normal Struts 2 configuration package in struts.xml, but have it extend struts1-default. The struts1-default package contains several interceptors and a default interceptor stack that work the plugin into the Struts 2 request process.
In the most simple case, where you have a Struts 1 Action that you want to use in Struts 2, configure a Struts 2 action using the wrapper:
<action name="myAction" class="org.apache.struts2.s1.Struts1Action"> <param name="className">com.mycompany.myapp.MyAction</param> <result>myAction.jsp</result> </action>
Most likely, you will have an ActionForm that your Struts 1 Action expects. To use an ActionForm, the setup is a little more complicated because you'll need an interceptor that manages the creation and scope (to support request and session scoping like Struts 1) of the ActionForm.
<action name="myFormAction" class="org.apache.struts2.s1.Struts1Action"> <param name="className">com.mycompany.myapp.MyAction</param> <interceptor name="myForm" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"> <param name="className">com.mycompany.myapp.MyForm</param> <param name="name">gangsterForm</param> <param name="scope">session</param> </interceptor> <interceptor-ref name="struts1-default" /> <result>myAction.jsp</result> </action>
This example shows a few Struts 1 Actions, a session-scoped ActionForm, and validation that uses Commons Validator:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="integration" extends="struts1-default" namespace="/integration"> <interceptors> <interceptor name="gangsterForm" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"> <param name="className">org.apache.struts2.showcase.integration.GangsterForm</param> <param name="name">gangsterForm</param> </interceptor> <interceptor name="gangsterValidation" class="org.apache.struts2.s1.ActionFormValidationInterceptor"> <param name="pathnames">/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml</param> </interceptor> <interceptor-stack name="integration"> <interceptor-ref name="static-params"/> <interceptor-ref name="gangsterForm"/> <interceptor-ref name="model-driven"/> <interceptor-ref name="actionForm-reset"/> <interceptor-ref name="basicStack"/> <interceptor-ref name="gangsterValidation"/> <interceptor-ref name="workflow"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="integration" /> <default-action-ref name="editGangster" /> <!-- Diplay entry page that uses Model-Driven technique --> <action name="editGangster" class="org.apache.struts2.s1.Struts1Action"> <param name="className">org.apache.struts2.showcase.integration.EditGangsterAction</param> <result>modelDriven.jsp</result> </action> <!-- Display the result page whose content is populated using the Model-Driven technique --> <action name="saveGangster" class="org.apache.struts2.s1.Struts1Action"> <param name="className">org.apache.struts2.showcase.integration.SaveGangsterAction</param> <param name="validate">true</param> <result name="input">modelDriven.jsp</result> <result>modelDrivenResult.jsp</result> </action> </package> </struts>
This plugin doesn't support any global settings.
This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory. It will need the Struts 1 jar in order to function correctly.