When talking in JSF context, you normally do that by setting the <h:selectManyListbox value>
to the desired value beforehand, like as you would do for every other JSF UIInput
component in order to preset/preselect the value.
The desired HTML output can be achieved with this view
<h:selectManyListbox id="example-presets" value="#{bean.selectedItems}" size="5">
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyListbox>
and this model in #{bean}
private List<String> selectedItems; // +getter+setter
private List<SelectItem> availableItems; // +getter (no setter necessary)
@PostConstruct
public void init() {
selectedItems = Arrays.asList("option1", "option3", "option4");
availableItems = Arrays.asList(
new SelectItem("option1", "Option 1", null, false),
new SelectItem("option2", "Option 2", null, false),
new SelectItem("option3", "Option 3", null, false),
new SelectItem("option4", "Option 4", null, false),
new SelectItem("option5", "Option 5", null, true),
new SelectItem("option6", "Option 6", null, true),
new SelectItem("option7", "Option 7", null, false),
new SelectItem("option8", "Option 8", null, false),
new SelectItem("option9", "Option 9", null, false)
);
}
The generated HTML output is:
<select id="form:example-presets" name="form:example-presets" multiple="multiple" size="5">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3" selected="selected">Option 3</option>
<option value="option4" selected="selected">Option 4</option>
<option value="option5" disabled="disabled">Option 5</option>
<option value="option6" disabled="disabled">Option 6</option>
<option value="option7">Option 7</option>
<option value="option8">Option 8</option>
<option value="option9">Option 9</option>
</select>
Exactly like you intented. Make sure that your jQuery selector is right, by the way. See also How to select JSF components using jQuery?