Populating select options from Beans in struts - java

I am Having Struts 1.2 bean as below which contains Locations in Array
Register.java
public class Register extends ActionForm
{
private String[] userLocation = {"Chennai", "Bangalore", "Delhi", "Singapore"};
.
.
//Getters and Setters
.
.
.
}
I want to populate this in Dropdown select in options.I tried the Following code but it ain't working.Any Idea how to do this.Is it possible to do this with out using collection by using array.
RegisterForm.jsp
<html:select property="userSelectedLocation">
<html:options property="id" labelProperty="name" />
</html:select>

Read the documentation of the select tag and of the options tag.
The select tag expects to find the selected value of the select box in a property. You gave it userSelectedLocation as the property, but the action form doesn't seem to have any getUserSelectedLocation() method.
The options tag gets options from a collection of values. The place where it gets the collection depends on three attributes: collection, name and property. The documentation says:
Only property is specified - The value of this attribute is the name of a property of the ActionForm bean associated with our form, which will return the collection.
This means that the options tag looks for a method getId() in the action form that would return the array of values. It doesn't seem you have such a method.
The documentation clearly explains how the tag works. If you told us what you want to do (what do you want the select options to have as value and as labels, what is the property that contains the selected value), we could tell you what to use.

Related

Struts 2 Select Tag Default Value

I'm creating a database record edit form using Struts.
<s:select id="status" name="newRecord.status" list="statusTypes" listKey="id" listValue="description" label="Status:" value="" /><br />
Example list:
Status' list:
1 Open
2 Closed
3 Pending
I want to set a default value on the status field as the current status which is stored in record.status which contains the string representation e.g. "Open".
If I set value="%{record.status} it doesn't work because that's not any list key. Is there a way I can set this value when I only have the string representation to work with?
Or should I redesign record.status to be an object with ID and String?
You should initialize the value of the newRecord.status that should be a separate property that contains the list key value. Then simply set the default value="%{record.status}". When I said the property I mean the bean property that is accessible via OGNL.
You need to use a hash instead of a list. Notice
listKey="id" listValue="description"
Then you can set the value = the key

Spring Roo jspx Nightmare... ItemLabel is not enough

I got a row in my create.jspx as follows:
<field:select field="groupsowdrefs"
id="c_com_usergroups_manager_domain_Users_groupsowdrefs"
itemValue="id" items="${groupsviews}" multiple="true"
path="/groupsviews" z="yuLSgZ+z3Zrwet6KAYzGT+xFndc="/>
this field displays a box with rows populated by the relationship #ManyToMany between 2 entities.
Now the problem is that the first entity has only one String attribute which populates this box, but has itself a #ManytoOne relation with another entity. This value is important and is not shown in the box rows!
This happens because the field:select shows only attributes and not fields of related entities.
I tried to put an ItemLabel tag and it works but box rows show only one field at time.
Maybe a simple div which shows the info i need on clicking the rows of the box, or a concatenate ItemLabel, a jquery trick..or anything..would solve this, but jspx is hard to understand to me.
A possible solution would be to add a getter to the class you want, as a read-only property:
public getSelectDescription() {
// concatenate desired values
}
and use that property in the itemLabel
itemLabel="selectDescription"

How do I get the attributes of a tag?

Been looking everywhere, I just can't seen to find it (probably because I'm wording it wrong or something).
In have a simple select tag in a from with several options. Each option has an id attribute. I want to get the id from the selected options.
The name of the select tag is "group", so in the servlet I call:
String group = request.getParameter("group");
This only returns the "label" of the option (the name of it, what the user sees). I don't want that, I want the id of it. I tried
String group = request.getParameterValues("group");
but that just returns an array with one item, the label of the option.
How can I do this? Thanks.
You should set the value attribute of the option tag to what you want to get from servlet:
<select name="parent">
<c:forEach items="${parents}" var="parent">
// here set value="yourid"
<option value="${parent.id}">${parent.name}
</c:forEach>
</select>

How to update a the contents of a list displayed on JSP using Struts2?

I'm using Struts2 to display the contents of a list of objects on a JSP.
The flow of events is as following:
GetDataAction.java -> fetches values from
the database, fills in the ArrayList
named tableList. On success, the
displayData.jsp is shown.
displayData.jsp -> uses the s:iterate tag to display the values of objects
in the tableList.
The user changes some values in the
displayData.jsp and presses on the
Update button. On the click of
Update button, the
UpdateDataAction.java is called.
Now my problem is; How do I use the same tableList in UpdateDataAction.java to get the modified values?
I tried declaring an ArrayList with the same name 'tableList' (along with getters and setters), in UpdateDataAction.java but it throws a NullPointerException.
Please suggest.
IMO the way you are updating is not a good idea.Either you should link every row to a seperate edit page or use ajax.There are many plugins available to update table values using ajax,If you need i can provide you the links
Back to your way of doing it,i guess you are doing it as follows
<s:form action="UpdateDataActionName">
<s:iterator value="tableList">
<s:textfield name="objectName.propertyName1" value="%(propertyName1)">
<s:textfield name="objectName.propertyName2" value="%(propertyName2)">
<s:textfield name="objectName.propertyName3" value="%(propertyName3)">
</s:iterator>
<s:submit value="Update"/>
</s:form>
Now declare a list in your UpdateDataAction,of type <objectNameoftableListType> i.e. the same object type which the tabeList is representing.The name of the list must be objectName.Try to Iteate and check if you are getting the right values as submitted from the jsp.

Can we have an optional atrribute in a jstl tag depending upon another attribute?

Earlier we had a tag ifRole such that
<op:ifRole role="role1">
<li id="menu3SubMenu4">This will be shown only to the user with role1</li>
</op:ifRole>
In this tag we had one required field role and another optional field secondaryRole. Now I added another optional attribute excluding such that
<op:ifRole excluding="role2">
<li id="menu3SubMenu4">This will be shown to all users except the one with role=role2</li>
</op:ifRole>
Also, I changed the attribute role from required to optional. Now, i don't want anybody to use this tag as:
<op:ifRole excluding="role2" role="role1">
This thing should not be allowed. One way is to throw the exception in doStartTag when both of these params are supplied. But I want other way round.
You can associate your tag with a javax.servlet.jsp.tagext.TagExtraInfo class, which can perform runtime validation of the attributes. This is perhaps a a cleaner way of doing this validation than doing it in the tag class itself.
The JavaEE tutorial covers it here

Categories

Resources