Struts2 iterator with dynamic columns - struts2

Imagine a POJO "Employee" which has three properties.
public Class Employee{
private String id;
private String name;
private double salaray;
}
I have a method that fetches list of Employee. In jsp i display the list with tag.
<s:iterator value="listEmployee">
<s:property value="id"/>
<s:property value="name"/>
</s:iterator>
As you can see, I have displayed only two columns inside iterator. Now in my case, how do i add the field salary inside iterator dynamically?
This is small example relevant to my actual situation. Can anyone throw some light?

I don't see any practical way. If you set your view up to handle such dynamic objects then it can be done with reflection, although I will recommend Apache BeanUtils.
Say you do something like (not tested):
public class Introspector{
private BeanMap properties;
public Introspector(){
myProperties = new BeanMap(this);
}
public Set getProperties(){
return properties.keySet();
}
}
With that done, the Employee class should be able to use it...
public class Employee extends Introspector{
private String id;
private String name;
private double salaray;
//getters setters for above properties
}
then in the view you could say...
<s:iterator value="listEmployee">
<s:iterator value="properties">
<s:property>
</s:iterator>
</s:iterator>

Related

struts 2 name of field name conflict

I have a field in an object
public class MyObj {
private Date qGuardExpireDate;
...
public void setQGuardExpireDate(Date qGuardExpireDate) {
this.qGuardExpireDate = qGuardExpireDate;
}
public Date getQGuardExpireDate() {
return qGuardExpireDate;
}
....
I have a getter and setting in the action class (struts 2.3.36) for the myObj definition.
In the jsp I have
<s:hidden name="myObj.qGuardEffectDate" />
I have other dates in the dataobject that work fine in the same jsp. This one however will not populate the value in the jsp....
I was playing around and added this to the dataobject:
public Date getGuardExpireDate() {//removed q from name of getter
return qGuardExpireDate;
}
Change the jsp to show the
<s:hidden name="myObj.guardEffectDate" />
And that one works fine!!! Returning the same value from the object. Any ideas why that would be?? It seems to hate that variable name.

change HtmlSelectOneMenu display value based on SelectItem information

Hi I have a problem about dynamically displaying the value of a HtmlSelectOneMenu. Below is a small application that describes my problem.
I have a list of cars List<Car> carList = new ArrayList<Car>() in my backing bean.
Car is a abstract class and Toyota and Ford extends Car.
Now I need to display different message in the selectonemenu based on the class type. If it is Toyota then I would display something else. Maybe its clearer for the codes to tell the story.
Backing Bean:
#ManagedBean(name="myBean")
#SessionScoped
public class MyCarBackingBean implements PhaseListener {
private List<Car> carList = new ArrayList<Car>();
private HtmlSelectOneMenu hsom;
Car myCar;
#PostConstruct
public void init() {
carList.add(new Ford());
carList.add(new Toyota());
}
#Override
public void beforePhase(PhaseEvent event) {
//hsom becomes null here. Im pretty sure the setHsom was called before and the variable was set.
if(hsom != null) {
switch((Integer)hsom.getValue()){
case 1: hsom.setValue("This is a Ford car"); break;
case 2: hsom.setValue("This is a Toyota car");
}
}
//The rest of the world...
}
And i bind the selectonemenu to the component in my page:
<h:form>
<h:selectOneMenu binding="#{myBean.hsom}">
<f:selectItems value="#{myBean.carList}" var="car" itemValue="#{car.id}" itemLabel="#{car.id}" />
</h:selectOneMenu>
<h:commandButton value="Submit" action="#{myBean.mySubmit()}"/>
</h:form>
And finally the model classes:
public abstract class Car {
protected int id;
//Getters and Setters
}
public class Toyota extends Car {
public Toyota(){
this.id = 2; //in case of ford car, id is 1.
}
}
And I'm thinking using a phase listener to change the display, cos I read some posts saying that it is bad to change the getters and setters and put business logic in them. Nor do I want to wrap those cars in other objects and make use of itemLabel and itemValue.
But when I was debugging it I found that hsom is null when the execution reaches beforePhase but it is not null in the rest part of the code.
So my questions are : is it a good way to use a phase listener for this? And why is the component object null in beforePhase?
Add a different attribute (say description) to your class. Implement it as you like, and refer to it in the selectItems tag. V.g.
<f:selectItems value="#{myBean.carList}" var="car" itemValue="#{car.id}" itemLabel="#{car.description}" />
Alternatively, replace myBean.carList with a method that returns a List<SelectItem>, and create the selectItems as you wish.
As a rule of thumb, try to keep the .xhtml as "logic-free" as possible.

How to include an include tag parameter as value of an iterator tag in struts2

I have a jsp *to_include.jsp* that i wanna include in another one using the < s:include > tag passing a string parameter. Here is the code of the including jsp:
<div>
<s:include value="to_include.jsp">
<s:param name="list">list_of_objects</s:param>
</s:include>
</div>
That list param is the name of an array object that is iterated inside the included jsp. I can easily access the list name in the include jsp using:
<%= request.getParameter("list") %>
But in the < s:iterator > attribute value you can't include expression inside tags like the one just showed. My question is, how can i access the list parameter inside the iterator tag?
<s:iterator value="?">
...
</s:iterator>
Instead, use the s:set tag to take an action property and set it to a value on the value stack which the included page can then access.
If what you want is to have the list that is being iterated to be determined by a request parameter, then why not have the selection occur in the action instead of the jsp?
For instance:
public class ListAction {
private List<?> list;
private ListService listService;
public String execute() {
return "success";
}
public void setListName(String listName) {
list = listService.getList(listName);
}
public void setListService(ListService listService) {
this.listService = listService;
}
public List<?> getList() {
return list;
}
}

Drop down list in struts2

How to populate ddropdown list in a web page in struts2 from database. The items of the drop down will be taken from database.
For creating list in struts2, we can use implement Preparable.
public class CountryListAction extends ActionSupport implements Preparable {
private List<String> country;
getters and setters
public String prepareCountryList() throws Exception{
country=new ArrayList<String>();
country=fill country from database;
}
public String countryList() throws Exception{
return SUCCESS;
}
}
For jsp, use:
<s:select label="Select Country"
name="country"
headerValue="Select Country"
list="%{country}"
/>
Preparable interface will call prepareCountryList method before calling countryList and jsp will be prepopulated. You can populate as many drop down list using single method in struts2.
You need to create a List in you action class with getters and setters and all you have to do is to populate the list inside your Action's execute method.
Action Class
public class MyListAction extends ActionSupport{
private List<String> country;
getters and setters
public String execute() throws Exception{
country=new ArrayList<String>();
countr=fill country from database;
return SUCCESS;
}
}
Now all you need to have a S2 select tag with following entry
<s:select label="Select Country"
name="country"
headerKey="-1" headerValue="Select Country"
list="%{country}"
/>
here list is an Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.
So list=%{country} will be evaluated by OGNL as getCountry() method in your action class and will fetch the required list from value-stack to populate the drop down
You can use the below code:
<s:select name="someName" id="someId"
list="someMap_or_List" onchange="someFunction();"/>
here someMap_or_List is the List or Map that you can use to populate your Drop Down List.

How To Prepopulate Checkboxes With Struts2 and Jquery?

I am trying to determine the best/easiest way to prepopulate certain checkboxes created using Struts2 form tags. My application is a "normal" three tier setup, using Struts2 on the controller layer.
Before I really, really dig deep here, does the tag support creating the list of all possible checkboxes, then populating it (say, via the below action)?
Sample action:
public class UserManagementAction extends ActionSupport implements Preparable {
private List<String> allRoles;
private List<String> rolesToPrepopulate;
// get/set methods
public void prepare() throws Exception {
// populate the allRoles and rolesToPrepopulate lists
}
public String execute() throws Exception {
return INPUT;
}
(Note: assume that struts.xml has been configured with which JSP to return for INPUT)
Thanks for any help.
Jason
What I would do is a new object class and use it as for checkboxes.
For example:
public class StrutsCheckbox {
private Integer id;
private Boolean selected;
...
}
And in prepare() method you can set selected field as you wish (and also id to all of them).
Next in JSP:
<s:iterator value="allRoles">
<s:checkbox name="selected" id="selected" fieldValue="%{id}" value="%{selected}"/>
</s:iterator>
And then in submit action Collection selected will be filled with ids.
public class UserManagementAction extends ActionSupport implements Preparable {
private List<StrutsCheckbox> allRoles;
private List<StrutsCheckbox> rolesToPrepopulate;
private List<Integer> selectedCheckboxes;
// get/set methods
public void prepare() throws Exception {
// populate the allRoles and rolesToPrepopulate lists
// fill and set allRoles and/or rolesToPrepopulate
}
public String execute() throws Exception {
return INPUT;
}
public String submit() throws Exception {
// list selectedCheckboxes is filled with selected fields id's
return INPUT;
}
Maybe with some corrections it will work, but the main idea is here.

Resources