jsp_stusts 2コンボボックスの評価と選択の流れ


<tr>
<td align="right">
      
</td>
<td colspan='1'>
<select id='paid' name='paid' style='width: 150px'></select>
 </td>
</tr>
 
<script type='text/javascript'>
function AddSelectOption( selectElement, optionText, optionValue ,optionSelect){
var oOption = document.createElement('OPTION') ;
oOption.text	= optionText ;
oOption.value	= optionValue ;
oOption.selected = optionSelect;
selectElement.options.add(oOption) ;
if(optionSelect=='selected')
selectElement.value=optionText;
return oOption ;}
${selectOptions}
</script>
 
public class Constants {
	public final static String SELECTOPTIONS = "selectOptions";

 }
 
	//           
		Pageadvert pa = new Pageadvert();
		List paList = pageadvertManager.getList(null);
		if (paList.size() != 0) {
			List<String> selectList = new ArrayList<String>();
			for (int i = 0; i < paList.size(); i++) {
				pa = (Pageadvert) paList.get(i);
				String id = pa.getId();
				String name = pa.getName();
				String val = name + "," + id;
				selectList.add(val);
			}
			String messages = (String) getRequest().getAttribute(
					Constants.SELECTOPTIONS);
			if (messages == null) {
				messages = "";
			}
			String obj = JsUtils.addSelectOptions("paid", null, selectList);
			messages += obj;
			saveRequest(Constants.SELECTOPTIONS, messages);
		}
 
	/**
	 *  select      BYList
	 * @param filedName        paid
	 * @param list    String    list.add("    ,life");list.add("    ,service");
	 * @return
	 */
	public static  String addSelectOptions(String filedName,String selectedValue, List<String> list){
		StringBuffer sb = new StringBuffer();
		for (String str : list) {
			String[] vals = str.split(",");
			String selected = "";
			sb.append(addSelectOption(filedName,vals[0],vals[1],selected));
		}
		if(StringUtils.isNotEmpty(selectedValue) )
		{
			sb.append(setSelectedOption(selectedValue,filedName));
		}
		return sb.toString();
	}
	/**
	 *  select     
	 * @param filedName     
	 * @param lable         
	 * @param value        
	 * @param selected          
	 * @return
	 */
	public static  String addSelectOption(String filedName,String lable,String value,String selected){
		return "AddSelectOption(document.all."+filedName+",'"+lable+"','"+value+"','"+selected+"');
"; } /** * * @param value * @param filedName * @return */ public static String setSelectedOption(String value,String filedName){ return "SetOptionSelected('"+value+"',document.all."+filedName+");
"; }