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.
Related
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>
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 get the value of selected radio button in action class, struts 2?
<s:form action="vote.action" method="post">
<s:radio name="vote" list="#{'1':'Candidate1','2':'Candidate2','3':'Candidate3'}" value="2" />
<s:submit method="execute" key="label.vote" align="center" />
In order for struts2 to inject your form values in your action class you need to do one of following things
Create individual properties in your action class with same name as field value in your JSP.
Create a bean with properties required by you and make sure to name those properties same as the one in your JSP.
Create getter and setter for the properties of bean in your action class.
I suggest to go through some of the documents describing how data flows between your JSP and Action class as well in reverse way.
In short for your radio button all you need to do is define getter and setter in your action class with same name as name of the jsp radio button field and you are all set to receive the value in your action class (power and magic of interceptors ;))
processing-forms
<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.
I'm Using Struts2 in jsp page i have iterator values for checkbox. how to get the values in action class? Am using javascript to validate the checkboxes. So please help me to solve this problem.
i believe you have checkboxex something like
<s:form action="myaction">
<s:checkbox name="a" fieldValue="ORIGINATOR" value="%{value1}" label="A"/>
<s:checkbox name="a" fieldValue="EVALUATOR" value="%{value2}" label="B"/>
<s:checkbox name="a" fieldValue="EXECUTOR" value="%{value3}" label="C"/>
</s:form>
here is how you will get values in your action class
public class Handler extends ActionSupport{
private String[] a;
public void setA(String[] a){
this.a= a;
}
#Override
public String execute() throws Exception {
// use the checkbox values here
return Action.SUCCESS;
}
}
hope this will help you.
There are two struts tags which can generate checkbox items on the page.
checkboxlist
checkbox
The tag checkboxlist is used to populate checkboxes from a map, list or array. This will generate checkboxes to the number of items in the structure you provide. The list attribute, which is mandatory is used to specify the list or map or array. When the form gets submitted, to get the selected items in your action object, map it using attribute name. i.e. to get the selected items in action, specify the name attribute, define an array or list with the same name in action class, provide the getter and setter for the array or list. You can refer the discussion below to know more:
http://www.mkyong.com/struts2/struts-2-scheckboxlist-multiple-check-boxes-example/
The checkbox tag will generate a single checkbox item on your page. It is comparatively simple and easy to implement. Please refer the link below to understand how it works:
http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/
And, to validate the checkbox items, you can implement the logic as you handle the basic html + javascript. You may use cssClass attribute to specify the html class while using checkboxlist and then use jQuery select by class to get the items and validate.