How would I execute a method with an argument in my model based on the URL? Ie, http://server/MyAction_Arg.action maps to MyClass.MyMethod(Arg)? I tried this:
<action name="MyAction_*" method="MyMethod({1})" class="example.MyClass">
<result>page.jsp</result>
</action>
but I get java.lang.NoSuchMethodException at runtime
In struts2 you can accomplish this like this:
server/myaction.action?arg=value
And in the MyClass action class you can declare a variable variable named arg with getter and setter. In the MyMethod() method you have access to the value of arg via the getArg() method.
Related
My need is to have a global result in struts.xml in having the dynamic value for the action to be redirected to.
Like,
<global-results>
<result name="customResult" type="redirectAction">
<param name="actionName">${customValue}</param>
<param name="namespace">/</param>
</result>
</global-results>
This customResult is being returned from one of my interceptor. customValue is the member of the same interceptor with getter and setter.
I am aware that action specific results can have dynamic values in it, provided the dynamic param shuould get its value assigned in the execute method of the particular action. And, that dynamic param should be the member of that action class.
Since here it is needed in global result, I made the dynamic param to be the member of the interceptop where I return that particular global result. But, this ${customValue} is not getting values while redirecting. It simple gets redirected to localhost:9080/myapp/.action.
Please suggest
The action properties will be put into the value stack that is why when you have an expression (like ${customValue}) in configuration, the values will be retrieved with appropriate getters. In order to achieve same behavior inside an interceptor you can put desired values directly into the value stack. The value stack can be retrieved from the invocationContext.
invocation.getInvocationContext().getValueStack()
.set("customValue", "some_action");
I have a method in dart, let's call it showSomething(), which has "named optional parameters"
// the following method is in a model called ctrl.
void showSomething(bool _bool, {var content}){
....
}
In dart, we can use the method like showSomething(true, content:whatever) or simply showSomething(true).
Now in html, I have a tag
<div ng-click="ctrl.showSomething(true, content:something)"></div>
Yeah, just like what you have seen, I use it the same way within dart code, but it doesn't work. Anyone got ideas?
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.
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.
I am trying to populate a parameter named 'member.id' in an action class using regular expression and wildcard action mapping with Struts2. The action mapping is something like this:
<action name = "validationEmailHTML/{sac:[^/]*}/{member.id:[0-9]*}" class = "controller.signUp.ValidationEmailContentController">
<interceptor-ref name="securityStack"/>
<result name="success">/signup/validationemail.jsp</result>
<result name="dbconnectionerror">/error/500.jsp</result>
<result name="unknownerror">/error/500.jsp</result>
</action>
The first parameter 'sac' gets populated correctly but the 'member.id' is being ignored. Am I missing something here or is it because ognl expressions to specify field-names is not an option when regex is used as a pattern-matcher?
It seems that it is impossible to use OGNL expressions for parameter names when the regular expression pattern-matcher is used in struts2. The only possible way is to use simple value members in your action class and write the code to set the value of properties of complex objects inside the class methods.