日期:2014-05-19  浏览次数:20820 次

spring接收从页面传回的enum问题
model
Java code

@Entity
@Table(name = "tbl_role_header")
public class Role {

    private String id;
    private String code;
    private String name;
    private RoleType roleType;
         //getter setter
}

public enum RoleType {
    /**
     * 1.role.
     */
    ROLE(1),
    
    /**
     * 2.group.
     */
    GROUP(2);
    
    private int roleType;

    private RoleType(int roleType){
        this.setRoleType(roleType);
    }

    public int getRoleType() {
        return roleType;
    }

    public void setRoleType(int roleType) {
        this.roleType = roleType;
    }
}



JS页面文件
JScript code

function saveRole() {
    var roleNo = $('#roleNo').val();
    var roleType = $('#roleType').val();  //roleType的值为1或2
    var roleName = $('#roleName').val();
    var roleActive = $('#roleActive').val();
    
    if(roleNo == null || roleName == null) {
        $.messager.alert('提示','角色代號和角色名稱不能為空!','info');
        return;
    }
    var active = 0;
    if(roleActive == 'on') {
        active = 1;
    }
    
    var roleDesc = $('#roleDesc').val();

    var rows = $('#tblRoles').datagrid('getRows');    
    var roleMembers = new Array();
    for(var i=0; i<rows.length; i++) {
        var employee = {'employeeId':rows[i].empNo,
                          'name':rows[i].empName};
        var member = {'member':employee,'remark':rows[i].remark};
        roleMembers.push(member);
    }
    var data = {'code':roleNo,'roleType':roleType,'name':roleName, 'active':active,'remark':roleDesc,'roleMembers':roleMembers};  
    $.ajax({
        type:'post',
        url:'save_role',  
        data:JSON.stringify(data),  
        contentType: "application/json",
        success:function(data){  
            $.messager.alert('提示',data.message,'info');    
            window.location.href= "list"; 
        },
        error:function(data){
            $.messager.alert('提示',data.responseText,'info');
        }
    }); 
}



controller
Java code

@RequestMapping(value="/save_role", method=RequestMethod.POST)
    @ResponseBody  
    public Map<String, String> addRole(@RequestBody Role role){
        Map<String, String> result = new HashMap<String, String>();          
        try {
            List<Role> rs = testGetRoles();
            //List<Role> rs = roleService.getAll(Role.class);
            for(Role r : rs) {
                if(r.getCode().equals(role.getCode())) {
                    result.put("message", "已存在同樣的角色代號,不可重覆新增!");
                    return result;
                }
            }

            roleService.save(role);
            result.put("message", "操作成功!");
        } catch (Exception ex){
            ex.printStackTrace();
            result.put("message", "操作失败!");
        }
        
        return result;
    }



-------------------------------------
报错如下:
2012-03-29 09:52:02.102:WARN::/bpm.web/roles/save_role: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of bpm.core.model.org.RoleType from String value '2': value not one of declared Enum instance names| at [Source: org.mortbay.jetty.HttpParser$Input@c8b9ac; line: 1, column: 14] (through reference chain: bpm.core.model.org.Role["roleType"])

问题描述:
一个对象包含一个enum对象,如何将该对象从页面传回controller

不知你们遇到类似的问题没?