user1287523
user1287523

Reputation: 957

Set default value of sj:select

I'm using the struts2-jquery plugin I need to set a default value for the sj:select drop-down widget.

The widget currently works and will display all the appropriate values (from a map, where the value is the value to be displayed on the menu). It is called from an action class that handles the appropriate methods.

<s:url id="menu" action="getListofObjects" />
<sj:select name="list" href="%{getListofObjects}" list="objects"/>

Lets say the object is blue and its ID is 3. Currently I have a .jsp page where all the objects are listed. If I click on the '3' it calls an action, which finds the object (blue) and saves both '3' and blue inside the action. Then, it calls a new .jsp page which displays info about the object.

What I want is to have an sj:select drop-down menu that will default to the object I selected (in this case, the colour blue), but when clicked will display the rest of the objects. When I call the widget with value="%{object}" I will get the same object displayed twice.

Does anyone have a solution for this? I've seen people using jQuery to scrub the dropdown of duplicates but I want to know if there is a true solution where I can set the default based upon what the dropdown menu returns.

Also, I'm using sj:select because I have some double/triple select methods so I cannot switch to s:select.

Thanks.

Update -> More Code

page1.jsp

<s:iterator value="penList">
<tr class="detail">
<td class="detail">
<s:url action="penUpdate" var="urlTag">
<s:param name="penId"><s:property value="penId"/></s:param></s:url>
<a href="<s:property value="#urlTag" />"><s:property value="penId" /></a></td>

penUpdateAction.java

public String execute(){
    getPen();
    return Action.SUCCESS;
}
public PenVO getPen(){
penVO = penService.searchPens(penId);
name = penVO.getName();
    id = penVO.getPenId();
return penVO;
}

Page1 calls the penUpdateAction which calls page2

Page2.jsp

<s:url id="nameList" action="getNames" />
<sj:select name="name" href="%{nameList}" list="names" onChangeTopics="reloadsecondlist"/>

This is a dropdown menu that shows all available names. It is working but I want it to DEFAULT to the value stored in the penUpdateAction class. Then the user can change the value or leave it as is.

Upvotes: 3

Views: 3714

Answers (1)

Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

Try this. I have not tested it but i think it should work:

<sj:select name="list" href="%{getListofObjects}" list="objects" listKey="id" listValue="name" value="%{id}"/>

Note: The value of listKey and listValue must be properties of the Object in the list you are populating the sj:select with.

The id property set in value attribute should be a property in your action class which carries the default value that you want to set. Let say the value is 3, and you have a select list like this

<select>
    <option value="1">Yellow</option>
    <option value="2">Black</option>
    <option value="3">Blue</option>
</select>

When the page loads, the select will display by default Blue. Let me know if u have issues doing this.

Upvotes: 4

Related Questions