pass hidden field in action class - struts2

<s:iterator value="categoryList">
<s:url id="category" action="/editProduct.action"/>
<s:a href="%{category}">
<s:property value="name"/>
</s:a>
<s:hidden name = "categoryId" id = "categoryId" value = "<s:property value='name'/" />
</s:iterator
Problem:
I have a list of categories which are click able. Every category has a unique ID. Problem is that when user click on the category from list then categoryId will pass in editProduct action class
I am searching its solution from two days please help me and define the complete solution

You can't nest JSP tags like that.
<s:hidden name="categoryId" id="categoryId" value="%{name}"/>
This, however, makes no sense--if it's not a form, why would you put the categoryId into a hidden form field? You're not submitting a form, you're clicking a link.
Put the categoryId into the URL as a parameter.
<s:url id="category" action="/editProduct.action">
<s:param name="categoryId" value="%{name}"/>
</s:url>
Note that unless "name" is actually something very ID-like, it's probably a bad idea to use it as a primary key.
You may want to go over some HTML and web app basics before proceeding too much further; it will save you time in the long run.

Related

How to display pre-checked checkboxes inside iterator in Struts 2

I need to iterate through List<String>, each element has s:checkbox.
I have defined one list in Action layer to keep selected elements.
Using my code I can submit my form and capture selected values in action layer. But, some of the checkboxes must be pre-checked. I cannot display pre- checked status when loading the page.
Value1 contains list of String objects.
functionCheckBoxList - The list I have defined to keep checked element
While loading, I added some element to functionCheckBoxList that belongs to Value1.
But still does not show pre-checked status in the page.
<s:iterator value="value1" var ="functionName">
<s:checkbox fieldValue="%{#functionName}" name="functionCheckBoxList"
value="%{#functionName}" theme="simple" >
</s:checkbox>
</s:iterator>
Note: I know how to do it using s:checkboxlist but can't here as I need special format in the iteration.
i could find a solution for my problem, i would like to share it with everyone.
<s:iterator value="value1" var ="functionName">
<s:checkbox fieldValue="%{#functionName}" name="functionCheckBoxList"
value="%{#functionName in functionCheckBoxList}" theme="simple" >
</s:checkbox>
</s:iterator>

jstl <select> to persist selection

I have a JSP page where am printing the arraylist content via an iterator
<s:iterator var="BeanList" value="BeanList">
<option value='<s:property value="#BeanList.simpleID"/>'>
<s:property value="#BeanList.simpleText" />
</option>
</s:iterator>
Every time the user selects an option, the form submits to the action handling. I want to be able to take the value of the clicked option, and when the page is reloaded after the submit, the same value persists in the select drop down.
Any help will be greatly apprecaited,
You are using HTML Select Tag, with values populated from Struts2 Property Tag.
No JSTL is involved.
But believe me, you can avoid this using the Struts2 Select Tag directly.
Official documentation: http://struts.apache.org/release/2.3.x/docs/select.html
In Action
#Getter #Setter List<String> allCities;
#Getter #Setter String selectedCity;
In JSP:
<s:select list="allCities"
name="selectedCity" />
Faster and cleaner than iterating manually :)
Eventually you can add an optional header value:
<s:select list="allCities"
name="selectedCity"
headerKey="-1"
headerValue="Select a City" />
For that you need to declare a variable with select box name in Action class, and put setter and getter for that. And then, when you are submitting the form, the name matches and it automatically populate in Action class.
When retrieving the data set value to same variable.Then it will populate automatically by using name.
This will happen by using params interceptor internally.

Preserving objects through actions in Struts2

Can I preserve object across actions in Struts2 without using session?
I'm working on a Struts2 project and curious if there are some ways to preserving object
value once an action is ended.
What I'm trying to do is:
Calling an action to read from an uploaded file and prepare its contents as a list of objects.
Then I display the list as an example for the user.
Then the user can click on a submit button to call an action to process the list which is created from the first action.
Usually the list would be lost once the action is end. So, I have to store the list as a session, but I think it should have some better methods to achieve the I'm working on.
If you want to preserve data across requests the session is the normal mechanism for doing so.
If you don't use the session you'd need to essentially duplicate its functionality to maintain the data associated with the current user/conversation.
If you need to preserve the List, then you have to use the Session.
But if you (if I've understood your problem) just need to handle the List through
ActionOne (that creates the List) ->
JSPOne (thast shows the List to the user) ->
ACtionTwo (that receives the List from JSPOne and does some business with it)
, without having to worry about the fact that the user can change the List client-side (for example manipulating the web page with FireBug), then you don't need the session.
You just need the List object to be declared on ActionOne and ActionTwo (with getters and setters, at least the getter on ActionOne and at least the setter on ActionTwo), and to include List name and index on name attribute of JSP tags.
If you just draw it (like with <s:property/> tag), instad of using some tag that will post the value, like <s:textfield />, then just use an <s:hidden /> to post te value.
For example, if you have:
List<MyObject> myList //with getters and setters
and assuming that MyObject has id and value fields (with getters and setters),
in JSP instead of
<s:iterator value="myList" >
<s:property value="id"/>
<s:textfield name="value"/>
</s:iterator>
use
<s:iterator value="myList" status="ctr" >
<s:hidden name="myList[#ctr.index].id" value="id"/>
<s:property value="id"/> <!-- no need here, you won't post this -->
<s:textfield name="myList[#ctr.index].value" value="value" />
</s:iterator>
You will eventually validate a List like this throught the Visitor Validator (somewhewre in the future after all the rest is done :)
Enjoy

How to retrieve table row value in Struts2 action

I am new to Struts 2 , please help me with the below scenario
I have a table in Jsp using struts2 tag , All I Want to do is to retrieve the selected(checked) table row's value in the action class.
Please let me know how to achieve it .
<s:iterator value="listOfDtos" status="stat">
<tr>
<td><s:checkbox name="delete" value="select" /></td>
<td><s:property value="FirstName"/></td>
<td><s:property value="LastName"/></td>
</tr>
</s:iterator>
<s:submit id="delete" value="delete"/>
}
"listOfDtos" is an array list od dto and is set in the action like the below
ActionContext.getContext().getValueStack().set("listOfDtos", listOfDtos);
Now when the user selects any row to be deleted I want to be able to first of all retrieve this list of dtos in the Action and iterate them to check which of the dtos have the attribute "select" set to true. select" is an attribute of type String in the dto.
How can this be done? Also how will the "listOfDtos" be made available in the Action.
I am not sure i have understood your question properly.but here are few inputs to start with.
There is no need to do ActionContext.getContext().getValueStack().set("listOfDtos", listOfDtos); as this will bind your action class with framework tightly.I suggest you to create a property in your action class with name listOfDtos as ArrayList and provides its getter and setters, framework will place your array-list in the value-stack for you.
You need to provide some unique values to your check-box based on which you can check in your action class which id has been selected as you can perform any desired operation on the ArrayList based on the ID.

show label of an object property in struts2 text tag

I want to show a property of an object in my Jsp page. the problem is that this property is the name of a label in my resource bundle and the only way I know is to use <s:text name="labelname"> but now that it's a property of an object, I don't know how to do this. This is a part of my code, 'brandList' is a list of brand objects and 'brandName' field is name of label. I want something like this
<s:iterator value="brandList" status="stat">
<s:text name="<s:property value='brandName'>" />
</s:iterator>
but this doesn't work. any idea?
Thanks in advance.
Try this:
<s:iterator value="brandList" status="stat" var="brandName">
<s:text name="%{brandName}" />
</s:iterator>

Resources