jsp复选框值不绑定

时间:2015-05-07 14:04:38

标签: java jsp checkbox

我在从JSP获取checkbox的值时遇到问题。 我使用了SpringMVC,我的相关课程如下:
形式:

@Getter @Setter
public class Sample3Form extends AbstractForm {<
    private List<Sample1Bean> sampleList;   //all data
    private ValidPagedListHolder<Sample1Bean> samplePagedList;//1 page data
}

控制更改页面的类:
ValidPagedListHolder:

@SuppressWarnings("serial")
public class ValidPagedListHolder<E> extends PagedListHolder<E> implements
    Serializable {
    ...
    ...
    /**
    * Return a sub-list representing the current page.
    */
   @Override
   @Valid
   public List<E> getPageList() {
      return super.getPageList();
   }

    /**
    * コンストラクタ生成時の初期化処理を行うメソッド
    */
    protected void init() {
        setPageSize(DEFAULT_PAGE_SIZE);
    }
}

Sample1Bean:

@Getter @Setter
public class Sample1Bean extends AbstractOnlineDataBean {
    private String keiyakuno;
    private String keiyakunm;
    private String torisakino;

}

父豆类:

@Getter
@Setter
public abstract class AbstractOnlineDataBean extends AbstractDataBean {
    private Integer selectedIndex;
    private String maskPattern;
}

控制器:

@Controller
@RequestMapping(value = "app/sample3")
@SessionAttributes(value = "sample3Form")
@NotCheckToken
public class SampleController3 extends AbstractController {

    private static Logger logger = LoggerFactory.getLogger(UserUtil.class);

    @ModelAttribute(value = "sample3Form")
    public Sample3Form getForm() {
        return new Sample3Form();
    }
......
......
    @RequestMapping(value = "delete")
    public String delete(Model model, @Validated Sample3Form form, BindingResult result, HttpServletRequest request) throws GdcSystemException {    
        List<Sample1Bean> list = new ArrayList<Sample1Bean>();

        int size = form.getSampleList().size();
        ValidPagedListHolder<Sample1Bean> plist = form.getSamplePagedList();
        list = plist.getPageList();

        for (int i = 0 ; i < size; i ++) {
            if (form.getSampleList().get(i).getSelectedIndex() == null ) {

            } else {
                //I cannot arrive here even though I checked in JSP 
                list.add(form.getSampleList().get(i));
            }

        }
...
...
}

JSP:

<ab:form id="sample3" action="submit" modelAttribute="sample3Form">
    <table width="100%"  class="tableborder" cellpadding="0" cellspacing="0">
    ...
    ...
        <ab:tr index="${row.index}">
            <td width="2%" class="data_list_area">
                <ab:checkbox path="${sample3Form.samplePagedList.pageList[row.index].selectedIndex}" fwformat='NONE' 
                value="${sample3Form.samplePagedList.page * sample3Form.samplePagedList.pageSize + row.index }" name="checkedids"/>

            </td>            
            <td width="5%" class="data_list_area">
                <!--ab:out value="${sample3Form.samplePagedList.pageList[row.index].keiyakuno}" /-->
                <ab:label path="" fwformat='NONE'>${sample3Form.samplePagedList.pageList[row.index].keiyakuno}</ab:label>
            </td>
...
...

在上面的JSP中,我设置了复选框,并将复选框绑定到每个行数据的selectedIndex属性。但是我无法在控制器中获取选定的行,如下所示:

form.getSampleList().get(i).getSelectedIndex()


返回值始终为null。 为什么?请问谁能告诉我如何在控制器中获取selectedIndex?
顺便说一下,我可以在控制器中检查行,如下所示:

String[] arr = request.getParameterValues("checkedids");

备注:在JSP中,这些添加的复选框由名称“checkedids”定义。

<ab:checkbox>是一个标记类extends AbstractSingleCheckedElementTag
CheckboxTagImpl 的src在这里:

public class CheckboxTagImpl extends AbstractSingleCheckedElementTag {

    private String maskPatterns = null;

    private boolean escapeMaskFlg = false;

    /**
     * 
     * @return 
     */
    public String getMaskPatterns() {
        return maskPatterns;
    }

    /**
     * 
     * @param maskPatterns 
     */
    public void setMaskPatterns(String maskPatterns) {
        this.maskPatterns = maskPatterns;
    }

    /**
     * @return
     */
    public Boolean isEscapeMaskFlg() {
        return escapeMaskFlg;
    }

    /**
     * @param escapeMaskFlg
     */
    public void setEscapeMaskFlg (Boolean escapeMaskFlg) {
        this.escapeMaskFlg = escapeMaskFlg;
    }

    @Override
    protected int writeTagContent(TagWriter tagWriter) throws JspException {

        if (maskPatterns != null) {
            FormTagImpl formTag = TagUtil.getFormTag(this);
            String formClassName = formTag.getModelAttribute();
            String maskPattern = TagUtil.getMaskPattern(formClassName, pageContext);
            if (maskPattern != null) {
                String[] maskPatternArray = maskPatterns.split(",");
                for (int i = 0; i < maskPatternArray.length; i++) {
                    if (maskPattern.equals(maskPatternArray[i])) {
                        setDisabled(true);
                        break;
                    }
                }
            }
        }

        FormTagImpl formTag = TagUtil.getFormTag(this);
        String formClassName = formTag.getModelAttribute();
        Boolean maskFlg =  TagUtil.getMaskFlg(formClassName, pageContext);
        if (maskFlg == true && escapeMaskFlg == false) {
            setDisabled(true);
        }

        if (!isDisabled()) {
            // Write out the 'field was present' marker.
            tagWriter.startTag("input");
            tagWriter.writeAttribute("type", "hidden");
            String name = WebDataBinder.DEFAULT_FIELD_MARKER_PREFIX + getName();
            tagWriter.writeAttribute("id", getId());
            tagWriter.writeAttribute("name", name);
            tagWriter.writeAttribute("value", processFieldValue(name, SystemConstant.FLAG_OFF, getInputType()));
            tagWriter.endTag();
        }

        super.writeTagContent(tagWriter);

        return SKIP_BODY;
    }

    @Override
    protected void writeTagDetails(TagWriter tagWriter) throws JspException {
        tagWriter.writeAttribute("type", getInputType());

        Object boundValue = getBoundValue();
        Class<?> valueType = getBindStatus().getValueType();

        if (Boolean.class.equals(valueType) || boolean.class.equals(valueType)) {
            // the concrete type may not be a Boolean - can be String
            if (boundValue instanceof String) {
                boundValue = Boolean.valueOf((String) boundValue);
            }
            Boolean booleanValue = (boundValue != null ? (Boolean) boundValue : Boolean.FALSE);
            renderFromBoolean(booleanValue, tagWriter);
        } else if (String.class.equals(valueType) && getValue() == null) {
            renderFromValue(SystemConstant.FLAG_ON, tagWriter);
        }

        else {
            Object value = getValue();
            if (value == null) {
                throw new IllegalArgumentException("Attribute 'value' is required when binding to non-boolean values");
            }
            Object resolvedValue = (value instanceof String ? evaluate("value", value) : value);
            renderFromValue(resolvedValue, tagWriter);
        }
    }

    @Override
    public void doFinally() {
        if (PropertiesUtil.getProperty("cleanAtDoFinally").equals("true")) {
            super.doFinally();
            this.maskPatterns = null;
            this.escapeMaskFlg = false;
            setDisabled(false);
        }
    }

    @Override
    protected String getInputType() {
        return "checkbox";
    }

    @Override
    protected String getName() throws JspException {
        if (getPath()==null) {
            return super.getName();
        } else {
            return getPath();
        }
    }

    @Override
    protected BindStatus getBindStatus() throws JspException {
        BindStatus bindStatus = null;
        if (bindStatus == null) {
                // HTML escaping in tags is performed by the ValueFormatter class.
                String nestedPath = getNestedPath();
                String pathToUse = (nestedPath != null ? nestedPath + getPath() : getPath());
                if (pathToUse.endsWith(PropertyAccessor.NESTED_PROPERTY_SEPARATOR)) {
                        pathToUse = pathToUse.substring(0, pathToUse.length() - 1);
                }
                bindStatus = new BindStatus(getRequestContext(), pathToUse, false);
        }
        return bindStatus;
    }
}

感谢。

0 个答案:

没有答案