Action mapping in struts - mapping

I found in a struts project this action mapping:
<action name="action" class="MyClass" method="add">
<result name="success">/jsp/test.jsp</result>
</action>
And in MyClass there is no method name ="add" but there is a method "onAdd"
I wanna know if struts know the name of the method in this case or its an error ?
because I found this in much actionmapping in this project;
Thanks for your help;

Server will throw exception there is no method MyClass.add().
For more details you can look struts2 documentation Action Method

Because when you are mapping action into the staruts.xml then you have to specify the particular class and method and if you are not specifying that method then by Default it will call Execute() method. so now try this and that will call the particular class.
<action name="actionname" class="package.class" method="methodname">
<result name="success">/folder/xyz.jsp</result>
</action>
that way it will work.

Related

about the struts2 redirectAction result in struts.xml

Can Struts2 redirectAction result transfer a reference type like Person.
my code
<action name="saveAdAction" class="saveAdAction">
<result name="success" type="redirectAction">
<param name="actionName">getAdAction</param>
<param name="namespace">/</param>
<param name="person">${person}</param>
</result>
</action>
<action name="getAdAction" class="getAdAction">
<result name="success">/ad/ad.jsp</result>
<result name="input">/index.jsp</result>
</action>
the person is an bean in saveAdAction and getAdAction.
when i use saveAdAction the webpage will jump into index.jsp. why?
No, a redirect can only transfer request parameters, just like any regular servlet redirect. So you either have to pass each attribute of person as request parameters, or put the person in the session with a key that you can look up in your next action.
No, This is not possible, with redirectAction a new value stack will be created and new instance of Request and Response will be created.
So all your previous data will be lost.There are few ways by which you can transfer data and here are few
Save data in session and retrieve it in other action.
Pass through Query string which is not possible in your case.
Use message-store-interceptor
You can choose any of the above option

Struts2 Lifecycle of Action object

I'm newbie to Struts2 framework and i didn't really have much time to read any Struts2 books. I just learn it from YouTube. Anyway here's my question.
Supposedly my struts.xml is as per below:
<struts>
<package name="p1" namespace="/" extends="struts-default">
<action name="login" class="org.tutorial.struts2.action.LoginAction">
<result name="success" type="redirect" >searchTutorialForm</result>
<result name="error">/login.jsp</result>
</action>
<action name="searchTutorialForm">
<result>/searchForm.jsp</result>
</action>
</package>
</struts>
Lets talk about package p1.
If the URL is say [http://localhost:8080/Struts2/login], the org.tutorial.struts2.action.LoginAction gets called and upon success it gets redirected to the searchTutorialForm action tag which calls the searchForm.jsp.
So the URL the client would see is [http://localhost:8080/Struts2/searchTutorialForm]
(purpose is not for the client to see [http://localhost:8080/Struts2/searchForm.jsp])
Now there are some member variables in LoginAction which is being displayed in searchForm.jsp using tag. However using this approach they are not displayed since i think the object LoginAction is no longer in the ValueStack (after the redirect, i think??).
Of course if i do not use the above approach but instead as per below:
<struts>
<package name="p1" namespace="/" extends="struts-default">
<action name="login" class="org.tutorial.struts2.action.LoginAction">
<result name="success">/searchForm.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
then the member variable in the LoginAction object is displayed in success.jsp using the tag (but then user would see the URL [http://localhost:8080/Struts2/searchForm.jsp])
Basically my intention is for the user not to see any specific internal file or calling like .jsp or .action.
IMPORTANT NOTE:
There is no action class in action tag searchTutorialForm - basically a dummy action.
Question:
1. How do i display the member variable in LoginAction object using the first approach?
2. What is the life cycle of the LoginAction object in Value Stack?
Thanks.
Actions are created per-request.
A redirect causes a new request.
Therefor objects in previous actions are no longer available.
The questions:
You put it in session or include them as parameters in the redirect.
Actions go away at the end of the request.

Dynamic Method invocation in Struts2

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.

Pass attribute into struts2 action via struts.xml

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.

Referencing actionerror from httpheader result in struts.xml

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.

Resources