Struts2 parameters between actions - java

I have to pass some parameter from an action to another action,for example to keep trace of an event.
What is the best way to do that?
I would not use session parameters. Thanks

Assuming you are serverside within one action and wishing to invoke another action with some parameters.
You can use the s:action tag to invoke another action, possibly with additional/other parameters than the original action:
<s:action name="myAction" ignoreContextParams="true" executeResult="true">
<s:param name="foo" value="bar"/>
</s:action>
You can also use a standard struts-xml result type with a parameter:
<result name="success" type="redirect" >
<param name="location">foo.jsp?foo=${bar}</param>
<param name="parse">true</param>
<param name="encode">true</param>
</result>
If you want a client side redirect you have to send an url back to the client with the proper parameters, and maybe use some javascript to go there.
<s:url action="myAction" >
<s:param name="foo" value="bar"/>
</s:url>

Use url tag in the struts core tags, sample is given below:
<s:url var="idurl" action="EditEnterprise">
<s:param name="enterpriseId">
<s:property value="enterpriseId" />
</s:param>
</s:url>

<td>
<s:url id="url" action="Logging">
<s:param name="m_userNameInAction"><s:property value="m_userNameInForm"/></s:param>
</s:url>
<s:a href="%{url}">English</s:a>
</td>

Actually you are going to pass your one action parameter value from one action to another action.
simply include bean variable with same name. which parameter you are going to receive on action(receiver action).
<action name="ForwardAction" class="...">
<result name="success" type="chain">ReceiverAction</result>
</action>
ForwardAction parameter will be forwarded to ReceiverAction. you can use it.
but include same bean name in both actions.
if you are going to receive userid in receiveaction means.,
This should be in both actions.,
private int userid;
public void setUserid(int id){
this.userid = userid;
}
public int getUserid(){
return userid;
}

actually, the scope and servletConfig interceptor can be utilized in struts2, to automatic pop the action context parameters, (request/session, etc)

Related

Pass values from html to Action Class Struts

I am completely new to Struts and now I am facing some issues.
Here is my html code :-
<div id = "processingMode">
<div id = "appButton">
<input type="hidden" value="Cancel" name="flagValue" />
Cancel
</div>
</div>
And I want to retrieve hidden field values in the action class (or please let me know how to pass values from html to action class).
Can anyone help me to achieve this.
In order to pass the hidden parameter value using struts
you first need to add the struts library and import the struts tag library
Try this below code
<s:url action="actionclass">
<s:param name="id" value="parametervalue" />
</s:url>
Hi Krupa
To send hidden parameters from view page to action class, you have to use <s:hidden></s:hidden> struts tag. You can pass the parameters through the URL too. But that is not a best practice to do so when it comes to security.
so try to use struts tag to achieve desired task. find the following sample app code to get a better understanding :-
View Page
<s:form action="testAction">
<s:hidden name="EmpName" value="DummyName" />
<s:submit value="Submit" />
</s:form>
Action class
public class Employee extends ActionSupport {
private String EmpName;
// getters and setters
public String execute() {
System.out.println("Employee Name :- "+this.getEmpName());
return "success";
}
}
struts.xml
<action name="testAction" class="Employee">
<result name="success"><path of your view page></result>
</action>
Simply,
Cancel
In Action class, create a parameter q and its set and get methods

How to call different method from execute in struts?

In my Struts app, I have an action, called Foo.
<action name="Foo" class="some.path.here.foo">
<result name="SUCCESS" type="tiles">/foo.tiles</result>
</action>
Normally it calls execute(), but I want to call another method called change(). How can I do so?
My Idea was this:
<form name="Foo" action="Foo" >
<s:textfield name="Mail" placeholder="Mail" />
<select name="someselect">
<s:iterator value="someblabla">
<option value="<s:property value="somevalue"/>" label="<s:property value="Description"/>"><s:property value="Name"/></option>
</s:iterator>
</select>
<s:submit method="change" value="Go!"></s:submit>
</form>
But when I want to do this, I get
HTTP Status 404 - No result defined for action some.path.is.here.Foo and result input
Can you help me out here?
execute is the default method in an action. If you want to change it i think you can modify the description of your action with adding the "method" attribute. Like that :
<action name="Foo" class="some.path.here.foo" method="change">
<result name="SUCCESS" type="tiles">/foo.tiles</result>
</action>
Hope this help
You are getting error because, struts expects 'execute' method unless you specify explicitly. If your action method name is different, you have to specify it explicity using the parameter 'method'.
So your code should be
<action name="Foo" class="some.path.here.foo" method="change">
<result name="SUCCESS" type="tiles">/foo.tiles</result>
</action>
Struts 2 also supports wildcard methods and dynamic method invocation. DMI is less secure, and not preferred. See the docs here

Saving Form Fields into more than one Table

I have a form in my application .That form asks for data which is meant for two different tables of my Database.
Example
i have two tables Job and Customer,I ask for customers information like name ,email,phone number and i also ask for an starting time.This starting time field is meant for the Job table.
I was thinking to have two forms with two different actions and both actions being mapped to different action classes.But then i thought this would not be a good idea as i will have two different buttons to submit .
How can i map one action to two different action classes .
Here is what i was thinking
<s:form action="action1">
<s:textfield name="field1" label="name" />
<s:textfield name="field2" label="email" />
<s:submit />
</s:form>
<s:form action="action2">
<s:textfield name="field1" label="startingtime" />
<s:submit />
</s:form>
and action1 and action2 will be mapped to two different action classes in the controller(xml file).
what i want is
<s:form action="anaction">
<s:textfield name="field1" label="name" />
<s:textfield name="field2" label="email" />
<s:textfield name="field1" label="startingtime" />
<s:submit />
</s:form>
and this action be mapped to two different classes in the xml file
EDIT:I know the following is wrong,how do i achive something like this in the correct way
<action name="anaction" class="com.codinghazard.actions.Actionclass" class="com.codinghazard.actions.anotherActionclass">
<result name="success">pages/success.jsp</result>
<result name="error">pages/failure.jsp</result>
</action>
i want a single form to post data to two different tables .How do i achieve that?
I have googled and i was not able to find an answer due the fact that i was not able to search correctly (what to search for).
I am using struts2 framework.Please dont downvote.I am a begginer and this is my first framework.
You may achieve this using Chain Result type. In this after execution of one action, another action will be executed sequentially.
<action name="anaction" class="com.codinghazard.actions.Actionclass">
<result name="success" type="chain">anotherAction</result>
<result name="error">pages/failure.jsp</result>
</action>
<action name="anotherAction" class="com.codinghazard.actions.anotherActionclass">
<result name="success">pages/success.jsp</result>
<result name="error">pages/failure.jsp</result>
</action>
You should refer this link
[a link]http://viralpatel.net/blogs/struts-2-action-chaining-example/

How to pass a value to an action in Struts 2 when you don't have a form?

I've written a submit button like this:
<s:submit type="button" value="Delete" action="%{notesDeleteUrl}" theme="simple"/>
And I've defined the url like this.
<s:url value="notesDeleteAction.action" id="notesDeleteUrl" >
<s:param name="noteId"><s:propertyvalue="iNote" /> </s:param>
</s:url>
So basically, I have no < s:form > tag on my JSP but I need to call an action with the submit button while passing a value to it. And I get this error.
There is no Action mapped for namespace [/] and action name [notesDeleteAction?noteId=48] associated with context path [/abc].
So I understand that it's unable to resolve the action because of the added parameter, but how else can I send this value to the action?
Your error is nothing to do with the parameter. Struts doesn't know what to do with the URL /notesDeleteAction
You'll need to include the action in your struts.xml file:
<package name="yourpackage" namespace="/" extends="struts-default">
<action name="notesDeleteAction" class="foo.YourClass">
<result>somepage.jsp</result>
</action>
</package>
There are 2 ways to get the parameter in your class, foo.YourClass
One way is:
Map parameters = ActionContext.getContext().getParameters();
The other way is for you class to implement org.apache.struts2.interceptor.ParameterAware

Append a query string with action in Struts 2

I am appending the parameters with action but I am getting an exception of on my Struts 2 page.
PWC6212: equal symbol expected
Below is my action with appended parameters code which is to be submitted.
action="MyAction.action?id=<%=request.getParameter("id")%>&name=<%=request.getParameter("name")%>&age=<%=request.getParameter("age")%>&num=<%=request.getParameter("num")%>"
Is the above is the syntax problem? If not then how can we set the parameters as a query string with action?
You should not use Scriptlets (<%= %>)
And, if action is an attribute of a Struts tag (like <s:form>), you can't use scriptlets, you should use OGNL.
Please refer to this question: Struts 2 s:select tag dynamic id for more details
Assumed the action attribute is used with the <form tag. Then the construction
<form name="MyAction.action" action="upload?id=<%=request.getParameter("id")%>&name=<%=request.getParameter("name")%>&age=<%=request.getParameter("age")%>&num=<%=request.getParameter("num")%>" method="POST">
should work with the current context. But in your case given error message (Exception Name: org.apache.jasper.JasperException: equal symbol expected is occurred when <s:form tag is used. So, you cannot use this url in the action attribute. This attribute should contain the plain action name that would be used to find your action.
"How we set parameters as a querystring?"
Actually we do it with <s:param tag. For example, when using hyperlinks
<s:a action="MyAction">
<s:param name="id" value="%{id}"/>
<s:param name="name" value="%{name}"/>
</s:a>
But this construction doesn't work with <s:form tag, unless you applying a special syntax as described in this answer and you definitely want to get these parameters if you do in the action
String quesyString = request.getQueryString();
and this string should not be empty.
However, this usecase is rarely applied, If you don't have a reason to get parameters in such way then as alternative you always can use <s:hidden fields to contain the values of the parameters. For example
<s:form action="MyAction" method="POST">
<s:hidden name="id" value="%{id}"/>
<s:hidden name="name" value="%{name}"/>
</s:form>
These values are passed as a parameters and initialize the action attributes after params interceptor worked. You could also get this parameters directly from the request as
Map<String, String[]> params = (Map<String, String[]>)request.getParameterMap();
However, the more convenient way to do this in your action is to implement ParameterAware.
Just like #AndreaLigios mentioned, you should use Struts2 specified EL, check out the Document here.
If you are using <s:url/>, check out Document please for more information.
Your code should look something like this:
<s:url value="MyAction.action">
<s:param name="id" value="%{#parameters.id}" />
<s:param name="name" value="%{#parameters.name}" />
<s:param name="age" value="%{#parameters.age}" />
<s:param name="num" value="%{#parameters.num}" />
</s:url>

Categories