struts2自定义枚举类型
1 枚举类
public enum TrialType{ BUG("BUG"), IMPROVE("改进"), REQUIREMENT("需求"); private String value; private TrialType(String value) { this.value = value; } public String getValue() { return value; } public String getKey(){ return this.toString(); } public static void main(String[] args){ System.out.println(Enum.valueOf(TrialType.class, "BUG")); for(TrialType t : TrialType.values()){ System.out.println(t.getValue()); System.out.println(t.getKey()); } } // bug 改进 需求}// 反馈种类
private Map<String, String> trialTypes = Maps.newHashMap(); public Map<String, String> getTrialTypes() { for (TrialType t : TrialType.values()) { trialTypes.put(t.getKey(), t.getValue()); } return trialTypes; }
<s:select list="trialTypes" listKey="key" listValue="value" name="trialType" />
public class GenericEnumConverter extends StrutsTypeConverter { @SuppressWarnings("unchecked") @Override public Object convertFromString(Map context, String[] values, Class toClass) { if (toClass.isEnum()) { if (values == null) {// 这个value的值就是 传来参数,比如性别的:男或女. return null; } if (values.length == 1) {// 大家都知道表单中的name是可以同名的,当然会有数个的情况 return Enum.valueOf(toClass, values[0]);// toClass就是哪个枚举了比如我这的:SexType枚举,调用Enum.valueOf就可以根据String返回相应对象了. } else { Object[] oo = new Object[values.length]; for (int i = 0; i < values.length; i++) { oo[i] = Enum.valueOf(toClass, values[i]); } return oo; } } else { return null; } } @Override public String convertToString(Map context, Object o) { return null; }}