直接看代码:其中setChecked设置复选框状态的,setSelected是设置下拉框选择项。GetFormField用于生成form表单,目前支持的有:文本框,下拉框,单选,复选,段落,图片,日期,可视化编辑
public class FormUtil { public static String setChecked(Object ori,Object cur){ String s = ""; if(ori!=null && cur!=null){ if(ori.toString().equals(cur.toString())){ s=" checked=\"checked\""; } } return s; } public static String setSelected(Object ori,Object cur){ String s = ""; if(ori!=null && cur!=null){ if(ori.toString().equals(cur.toString())){ s=" selected=\"selected\""; } } return s; } public static String GetFormField(String fieldname,String formtype,String fieldvalue) { String formdefault = ""; String[] pararr=null; if(fieldvalue!=null) { formdefault=fieldvalue; pararr=fieldvalue.split(","); } StringBuffer sb = new StringBuffer(); if(formtype!=null){ if(formtype.equals("text")){ sb.append("<input type=\"text\" name=\"").append(fieldname).append("\" class=\"input_normal\" value=\"").append(formdefault).append("\" />"); } else if(formtype.equals("select")){ sb.append("<select name=\"").append(fieldname).append("\" class=\"selects\" >"); if(pararr!=null){ for(int i=0;i<pararr.length;i++){ sb.append("<option value=\"").append(pararr[i]).append("\">").append(pararr[i]).append("</option>"); } } sb.append("</select>"); }else if(formtype.equals("radio")){ if(pararr!=null){ for(int i=0;i<pararr.length;i++){ sb.append("<label><input type=\"radio\" name=\""+fieldname+"\" value=\""+pararr[i]+"\" id=\""+fieldname+i+"\" />"+pararr[i]+"</label> "); } } }else if(formtype.equals("checkbox")){ if(pararr!=null){ for(int i=0;i<pararr.length;i++){ sb.append("<label><input type=\"checkbox\" name=\""+fieldname+"\" value=\""+pararr[i]+"\" id=\""+fieldname+i+"\" />"+pararr[i]+"</label> "); } } }else if(formtype.equals("textarea")){ sb.append("<textarea name=\""+fieldname+"\" id=\""+fieldname+"\" class=\"cus_textarea \">"+formdefault+"</textarea>"); }else if(formtype.equals("image")){ sb.append("<input type=\"hidden\" id=\""+fieldname+"\" name=\"").append(fieldname).append("\" class=\"input_normal\" value=\"").append(formdefault).append("\" />"); sb.append("<iframe src=\"upload_pic.jsp?formid="+fieldname+"&imgid=ViewImgShow_"+fieldname+"\" height=\"30\" frameborder=\"0\" scrolling=\"no\" width=\"90%\"></iframe>"); sb.append("<div id=\"ViewImgShow_"+fieldname+"\"></div>"); }else if(formtype.equals("datetime")){ sb.append("<input type=\"text\" onclick=\"displayCalendar(this,'yyyy-mm-dd',this)\" name=\"").append(fieldname).append("\" class=\"input_mini\" value=\"").append(formdefault).append("\" />"); }else if(formtype.equals("editor")){ sb.append("<textarea name=\""+fieldname+"\" id=\""+fieldname+"\" class=\"cus_textarea \">"+formdefault+"</textarea>"); sb.append("<script type=\"text/javascript\">\n" + " var editor_"+fieldname+";\n" + " KindEditor.ready(function(K) {\n" + " editor_"+fieldname+" = K.create('textarea[name=\""+fieldname+"\"]', {\n" + " allowFileManager : false,\n" + " allowImageUpload : false,\n" + " width:'100%',\n" + " height:'150px',\n" + " items : [\n" + " 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',\n" + " 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',\n" + " 'insertunorderedlist', '|', 'emoticons', 'image', 'link']\n" + " });\n" + " });\n" + " </script> "); } } return sb.toString(); } }