How can I pass an attribute into my struts2 java action that tells me whether the action was called from one URL path / action mapping vs another?
I figured I could put something in the struts.xml for two action mappings but use the same java action and just pass in a flag into the action.
You'll want to use the <param/> tag. I do this frequently for actions that handle both adding and editing an entity, as the fields, validations, and whatnot are virtually identical. Here's an example of that.
struts.xml
<action name="users/add" class="AddEditUserAction">
<param name="edit">false</param>
<result name="input">/WEB-INF/jsp/addEditUser.jsp</result>
</action>
<action name="users/{username}/edit" class="AddEditUserAction">
<param name="edit">true</param>
<result name="input">/WEB-INF/jsp/addEditUser.jsp</result>
</action>
The Action
public class AddEditUserAction {
private boolean isEdit;
// this is called by the struts.xml to set the value
public void setEdit(final boolean edit) {
isEdit = edit;
}
}
In order for this to work, you need the static parameters interceptor in your stack (it's included by default).
I question the design.
I'd handle it by specifying a method in the action configuration for one or both mappings.
The method(s) would set a flag in the action and call the "guts" of the action, which would query the flag value, and proceed accordingly.
Related
I have an interceptor that is working fine except the query string is not being passed on to the action from the interceptor.
<action name="EditIroCase" class="iro.action.IroCaseAction">
<interceptor-ref name="authIro"/>
<result name="success">iro/IroCaseFORM.jsp</result>
</action>
URL: /EditIroCase.action?id=123
Action is an existing one that I am adding the interceptor to. Implementing ParametersAware in Action to get ID from URL and that works fine.
Tried a couple different things, but can't seem to make it work. Used lots of interceptors, just never needed to maintain a param across its execution.
Interceptors work slightly different rather than actions. Because they belong to the action. So you can't add an action to the interceptor, but interceptor to the action.
You should know if you are using xml configuration or annotations, or another configuration provider, interceptors are stored in the collection such as List or Set, doesn't matter, but all of them in one place. Using <interceptor-ref> tag inside the <action> overrides the interceptor's config. By default actions are configured using <default-interceptor-ref> tag, which is defined in the struts-default.xml from the core package, and it points to defaultStack. This stack is designed to serve all your needs, but if you use a custom interceptor then you should explicitly add reference to defaultStack or create a custom stack which you would reference.
<action name="EditIroCase" class="iro.action.IroCaseAction">
<interceptor-ref name="authIro"/>
<interceptor-ref name="defaultStack"/>
<result name="success">iro/IroCaseFORM.jsp</result>
</action>
Now parameters should be populated.
I have a case like this:
JSP -> ACTION 1 -> Redirect-> Action 2 -> JSP
While redirecting we loose all the parameters. But there is one parameter that I'd like to pass from one action to another. The parameter is always the same for all actions.
I know that it is possible to write the following code and it works :
<action name="myAction" class="myActionClass" method="doThis">
<result name="success" type="redirect">doThat.action?myParam=${myParam}</result>
</action>
The only thing is that I'd like to pass the parameter automatically from one action to another when I have a redirect without writing anything in my action tag, but I don't know how to code it.
Is it possible with struts 2 to do that?
Someone suggested to put it in global-results, I tried, but still haven't succeeded.
You can set the property in to the session as an attribute and the same attribute can be taken back from the session irrespective of the action class you are using.
session.setAttribute(name,value);
session.getAttribute(name)
I have 2 Action classes : LoadingFormAction.java and FormValidationAction.java. In the first one, I create the functions which help in uploading data from an Excel file, and in the second I need to validate the loaded data according to the other fields values. LoadingFormAction.java control the view Form.jsp and FormValidationAction.java the view FormValidation.jsp. My question is : How to evaluate the attributes of the first action and use them in the second Action?? Is it obligatory to pass it as a session parameter? Thank you a lot.
<action name="LoadingFormActionName" class="com.test.LoadingFormAction">
<result name="success" type="redirectAction">
<param name="actionName">FormValidationActionName</param>
<param name="yourProperty">${yourProperty}</param>
</result>
</action>
Where yourProperty is the parameter of your LoadingFormAction class which you want to pass
to FormValidationAction class. You need to declare getter/setter for this variable in both the classes. Also, LoadingFormActionNameand FormValidationActionName are the corresponding action names.
Problem Statement
Jsp
<s:a href="newUser.action" > New User SignIn</s:a>
Struts.xml
<action name="*User" method="{1}" class="action.NewUser">
<result name="populate">/jsp/registerUser.jsp</result>
<result name="success">/jsp/success.jsp</result>
</action>
Action Class has the method
populate(){
}
I am wanting to use the Dynamic Method Invocation facility of STRUTS2. In general the framework substitutes the * word into the method attribute of struts.xml file.
Is there a way in which I can use a different method name. In my case the framework is attaching the method name new() to the struts.xml file but I have a method called populate() in my action class.
You can try to use Preparable interface and Prepare interceptor for this. If your Action class implements Preparable and provides one method called prepare(), it will be always called before executing any action methods.
There is no way to call populate method on newUser.action. You have to call only populateUser.action to call populate method.
In my TestClass action I am setting an action error using addActionError method. I have an action defined in the struts.xml as following
<action name="TestAction" class="TestClass">
<result name="input">/jsp/test.jsp</result>
<result name="error" type="httpheader">
<param name="error">409</param>
<param name="errorMessage">${SOME-EXPRESSION}</param>
</result>
</action>
The intent is to have the error message display whatever was added using addActionError. According to the org.apache.struts2.dispatcher.HttpHeaderResult documentation, I should be able to use Ognl expressions within errorMessage parameter.
So, is it possible to put something in place of ${SOME-EXPRESSION} that will reference actionerror in this scenario.(I tried ${actionerror} but it didn't work)
I know that I can have a workaround by declaring my own field (for example "errorText") in the action class and using that insteda of addActionError referencing it using ${errorText} inside the param tags. But before I go that route, want to make sure that's the only way.
Action errors are stored in a list, so you'll have to show something like ${actionErrors[0]}. But keep in mind this way it will only show your first added error, not all those you have included using addActionError.