代码片段:
public class LoginAction extends BaseAction { private Map<Integer,String> usCiteis = new HashMap<Integer,String>(); private Map<Integer,String> canadaCiteis = new HashMap<Integer,String>(); private Map<Integer,String> mexicoCiteis = new HashMap<Integer,String>(); public String method1() throws Exception { canadaCiteis.put(1, "Canada city 1"); canadaCiteis.put(2, "Canada city 2"); canadaCiteis.put(3, "Canada city 3"); canadaCiteis.put(4, "Canada city 4"); canadaCiteis.put(5, "Canada city 5"); canadaCiteis.put(6, "Canada city 6"); usCiteis.put(7, "US city 1"); usCiteis.put(8, "US city 2"); usCiteis.put(9, "US city 3"); mexicoCiteis.put(10, "Mexico city 4"); mexicoCiteis.put(11, "Mexico city 5"); mexicoCiteis.put(12, "Mexico city 6"); return SUCCESS; } public Map<Integer, String> getUsCiteis() { return usCiteis; } public void setUsCiteis(Map<Integer, String> usCiteis) { this.usCiteis = usCiteis; } public Map<Integer, String> getCanadaCiteis() { return canadaCiteis; } public void setCanadaCiteis(Map<Integer, String> canadaCiteis) { this.canadaCiteis = canadaCiteis; } public Map<Integer, String> getMexicoCiteis() { return mexicoCiteis; } public void setMexicoCiteis(Map<Integer, String> mexicoCiteis) { this.mexicoCiteis = mexicoCiteis; } }
HTML代码片段:
<s:select name="city" label="City" list="#{'0':'choose one'}"> <s:optgroup label="Canada" list="canadaCiteis"/> <s:optgroup label="US" list="usCiteis"/> <s:optgroup label="Mexico" list="mexicoCiteis"/> </s:select>
其中,<s:select>标签里头的list属性是必填项。可以用它来做一个默认标题。