复选框的值未保存在数据库中

时间:2011-09-01 06:31:34

标签: java struts

我有一个jsp页面,上面有各种复选框。

默认情况下,某些复选框已被禁用,并且已被选中。可以选择创建新模板并为复选框指定值。这些值将保存到DB中。

我的问题是所选复选框的保存值总是比用户选择的值少两个。在调试java代码时,我发现传递的值是由用户选择的。但禁用复选框的值将保存到数据库中,用户选择的最后两个值不会保存到数据库中。

我特此附上挑选所选值的代码和保存复选框值的代码。

选择复选框值的代码。

public ActionForward listSave(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    HardwareKey key = SecurityUtils.getKeySecurity();
    key.isFeaturePresent(HardwareKey.WS_DATA_EXCHANGE);

    DataExchangeTemplateForm thisForm = (DataExchangeTemplateForm) form;
    DataExchangeService service = (DataExchangeService) ServiceLocator
            .getServiceWithName(DataExchangeService.class.getName());
    ActionMessages errors = new ActionMessages();
    Integer dexTemplateId = thisForm.getTemplateId();

    String[] paramValues = request.getParameterValues("isRequiredFlag");
    String[] fieldIds = request.getParameterValues("fieldId");
    String[] dataTypeIds = request
            .getParameterValues("dexDataType.dataTypeId");
    String templateType = request.getParameter("templateType");
    boolean isDexTemplate = false;

    String direction = thisForm.getDirection();
    String dexTemplateName = thisForm.getDexTemplateName();
    if (Validator.isNullOrEmpty(dexTemplateName)) {
        ActionMessage msg = new ActionMessage("error.data.exchange",
                " Template name Can't be empty");
        errors.add("error.data.exchange", msg);
        saveErrors(request, errors);
        return view(mapping, form, request, response);
    }
    String dexTemplateVersion = thisForm.getDexVersion();
    if (Validator.isNotNullGreaterThanZero(dexTemplateId)) {
        DexTemplate dexTemplateOld = (DexTemplate) service.getObject(
                DexTemplate.class, dexTemplateId);
        String profileNameOld = dexTemplateOld.getDescription();
        if (!dexTemplateName.equalsIgnoreCase(profileNameOld)
                && service.isDuplicateTemplateName(dexTemplateName)) {
            ActionMessage msg = new ActionMessage("error.data.exchange",
                    "Duplicate Template name");
            errors.add("error.data.exchange", msg);
            saveErrors(request, errors);
            return view(mapping, form, request, response);
        }

        service.updateDexTemplateAndParams(dexTemplateName, direction,
                dexTemplateId, dataTypeIds, paramValues, fieldIds,
                dexTemplateVersion);
    } else {

        if (!service.isDuplicateTemplateName(dexTemplateName)) {
            if(templateType != null && templateType.equals("extranetDex")) {
                isDexTemplate = true;
            }
            dexTemplateId = service.saveDataExchangeTemplate(
                    dexTemplateName, direction, dexTemplateVersion, isDexTemplate);
            // saving params...             
            service.saveDexTemplateParams(dexTemplateId, dataTypeIds,
                    paramValues, fieldIds);
        } else {
            ActionMessage msg = new ActionMessage("error.data.exchange",
                    "Duplicate Template name");
            errors.add("error.data.exchange", msg);
            saveErrors(request, errors);
            return view(mapping, form, request, response);
        }
    }

代码保存选定的复选框值:

public void saveDexTemplateParams(Integer dexTemplateId,
        String[] dataTypeIds, String[] paramValues, String[] fieldIds)
        throws BaajaException {

    DexTemplate dexTemplate = (DexTemplate) getObject(DexTemplate.class,
            dexTemplateId);
    hibernateInitialize(dexTemplate);
    int fieldOrder = 1;
    if(paramValues!=null){
    for (int i = 0; i < paramValues.length; i++) {
        if (!Validator.isNullOrEmpty(paramValues[i])
                && paramValues[i].equalsIgnoreCase("true")) {

            DexTemplateField templatefield = new DexTemplateField();
            DexField dexField = (DexField) getObject(DexField.class,
                    Integer.parseInt(fieldIds[i]));

            templatefield.setDexField(dexField);
            templatefield.setDexTemplate(dexTemplate);
            templatefield.setFieldOrder(fieldOrder++);

            saveObject(templatefield);
        }

请建议任何可能的解决方案。

1 个答案:

答案 0 :(得分:2)

检查最后2个复选框的名称/ ID是否与其他复选框相同。

如果您使用Struts框架,建议使用FormBean附加到JSP表单。在该FormBean中,声明ArrayList,其名称与<html:checkbox>的属性属性相同。这将有助于您最大限度地减少支持/维护时间。