aspose file tools*
The moose likes Struts and the fly likes Something wrong with my <s:select> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Something wrong with my <s:select>" Watch "Something wrong with my <s:select>" New topic
Author

Something wrong with my <s:select>

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
I am having a weird problem where my <s:select> tag render the value on HTML, but when I submit the form, the addFieldError() method fired. I don't understand why it's not passing any value but look into the HTML source I found



here is my <s:select>



I use struts2 action to populate employeeType



so can anyone tell me why I get <option value="">
and what I couldnt understand was that the <s:select> tag worked before, I haven't change anything from the code I pasted above.

Thanks for help
Yash Dutt
Greenhorn

Joined: Apr 01, 2009
Posts: 5
try to use map instead of linked list......... i think it will work for you
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Specify a "listValue" attribute.
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Mr. Newton, I don't really understand what the documentation for the listValue attribute
last night I try removing listKey and it works but "Busser" automatically get selected

Can you elaborate a little more on examples of how to use listValue?
do you have to use listKey in conjunction with listValue?

Thanks for your thoughts
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

listKey and listValue go together.

Pre-selection is determined by the "value" or "name"/"key" attributes depending on the action property's value.
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
when I specify listKey="key"
than I get <option value="">Host</option>

when I remove listKey and specify listValue="value"
I get <option value="Host"></option>

when I specify both
I get <option value=""></option>

when I remove both listKey and listValue
I get <option value="Host">Host</option> plus all the others

but when I didn't have listKey and listValue specify, the headerValue="- Please Select -" is not displayed when I load the page

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Davie you need to understand why the code is behaving that way. You are using a List to show the <select> tag. The list contains strings. So when you specify "key" as listKey or listValue, then it tries to look for a property named "key" inside your elements of the List. And it finds nothing. If you used a Map to create the <select> tag, then it would have used the key of the map when you specify listKey = "key" (which is also the default behavior). I think giving nothing as listKey and listValue has solved your problem, and if you want a header key, then you can use headerKey and headerValues of the <select> tag to set the first option of the select box...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Yash Dutt
Greenhorn

Joined: Apr 01, 2009
Posts: 5
hi Davie try any one of the followings........
USAGE-1
<s:select headerKey="-1" headerValue=" Select Employee " list="#employeeTypes"/>
-----------------------------------------------------------------
public class Register extends ActionSupport {
private Hashtable employeeTypes;
public String execute() throws Exception{
EmployeeTypes = new Hashtable();
EmployeeTypes.put("1","Host");
EmployeeTypes.put("2","Server");
EmployeeTypes.put("3","Busser");

return SUCCESS;
}
public List getEmployeeTypes() {
return EmployeeTypes;
}
}

==============================================================================
USAGE-2
<s:select headerKey="-1" headerValue=" Select Employee " list="#employeeTypes" listValue="id" listKey="name"/>
------------------------------------------------------------------
public class Register extends ActionSupport {
private List<EmployeeType> employeeTypes;
public String execute() throws Exception{
EmployeeType = new LinkedList<EmployeeType>();
EmployeeType.add(new EmployeeType("1","Host"));
EmployeeType.add(new EmployeeType("2","Server"));
EmployeeType.add(new EmployeeType("3","Busser"));

return SUCCESS;
}
public List getEmployeeTypes() {
return employeeTypes;
}
}
public class EmployeeType{
private String id;
private String name;
public EmployeeType(String id,String name){
this.id=id;
this.name=name;
}
public void setId(String id){
this.id=id;
}
public String getId(){
return this.id;
}
public void setName(String id){
this.name=name;
}
public String getName(){
return this.name;
}
}
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Thank you Yash and Ankit, I am just confused because it worked before. But I need to understand the behavior of the code like what Ankit suggested. That usually takes some patients, I need to develop the patients to see the behavior of the code. From this I also finding out some of the design flaw I have, I know what to change now, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Something wrong with my <s:select>