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/
Related
I am trying to develop an application in struts2 where I have multiple forms and may or may not have same action. To avoid CSRF I also added token with these forms, but only one token at a time working. If I clicked on another submit button then I am redirecting to error page which is configured for CSRF. I am stuck here why this is happening, am not able to use multiple token on same page or is there any solution for this.
Please find struts2.xml code which I have configured.
<action name="expUsers" class="com.org.action.ExpUser">
<interceptor-ref name="CSRFStack"/>
<result name="invalid.token" type="tiles">csrfError</result>
<result name="success" type="stream">
<param name="contentDisposition">contentDisposition</param>
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
</result>
<result name="failed" type="chain">csrfError</result>
</action>
I have the following dummy form example below which will let you understand what i am doing.
<form id="form-1">
<s:token/>
....
</form>
<form id="form-2">
<s:token/>
....
</form>
<form id="form-3">
<s:token/>
....
</form>
... so on multiple forms with token.
Thanks to All for giving me your suggestion. I have found a working solution so i am giving answer to my own question.
When we generate <s:token/> then the hidden field will be generate like this:-
<input type="hidden" name="struts.token.name" value="token">
<input type="hidden" name="token" value="1IPDKJ3QWM8X4JXAV0RKC0A9XVQ4I83E">
which is default behavior. Now if i want to generate different token for each struts2 token tag then i have to specify the unique name to token like this :-
<s:token name="unique-1"/>
Then it will generate hidden field like this:-
<input type="hidden" name="struts.token.name" value="unique-1">
<input type="hidden" name="unique-1" value="1IPDKJ3QWM8X4JXAV0RKC0A9XVQ4I83E">
Now i have two different token for two different forms in same page and this tricks also working perfectly without any issue at the time of writing this answer.
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
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
I have a homepage with a modal struts 2 jquery dialog which calls a struts2 action containing a struts form.
Homepage dialog code
<s:url id="newItemURL" var="newItemURL" action="addNewItem" />
<sj:dialog id="newItem" href="%{newItemURL}" title="Create New Item" width="700" position="top" autoOpen="false"
loadingText="Loading..." />
<sj:a id="addNewItem" openDialog="newItem" button="true" buttonIcon="ui-icon-refresh">New Item</sj:a>
addNewItem Action Result JSP
This form submits and produces a text result if successful.
<div id="newItemForm">
<s:form action="addNewItem" id="addNewItem">
<fieldset>
<legend>Create a new item</legend>
<label for="description">Description: </label>
<s:textarea id="description" name="item.description" label="Description:" cols="20" rows="5"/><br />
<sj:submit id="submitNewItem" targets="resultNewItem" value="Submit" indicator="indicator" button="true" replaceTarget="true" />
</fieldset>
</s:form>
</div>
<div id="resultNewItem"></div>
The addNewItem form works fine standalone with <sj:head />, with AJAX result appearing in the resultNewItem div. <sj:head /> has to be removed in the addNewItem JSP to avoid conflict with the homepage.
The issue is when addNewItem Action is included as part of the dialog upon submitting the form the homepage action is called and as a result I end up with another homepage inside the dialog.
How can I solve this?
Edit:
Struts config
<action name="homepage"
class="com.actions.Homepage">
<result name="success" type="tiles">Homepage</result>
</action>
<action name="AddNewItem" class='com.actions.AddNewItem'>
<result name="success">/WEB-INF/jsp/addNewRisk/addNewRisk.jsp</result>
</action>
<action name="smoAddNewRiskINSERT" class="com.actions.AddNewItem" method="addNewRisk">
<result name="success">/WEB-INF/jsp/addNewRisk/success.jsp</result>
<result name="error">/WEB-INF/jsp/addNewRisk/error.jsp</result>
</action>
What I want to achieve
User clicks button to load dialog, addNewItem form is created (AddNewItem action), when user submits form it is sent via AJAX submit button and the result is displayed within the dialog.
First of all, give your elements unique id-s. As pointed out in Quincy comment. Second, in your struts.xml file change action name to addNewItem instead of AddNewItem.
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)