• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Something wrong with my <s:select>

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to use map instead of linked list......... i think it will work for you
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specify a "listValue" attribute.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Yash Dutt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic