Java 如何使弹簧复选框默认选中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9738965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:56:49  来源:igfitidea点击:

How to make spring checkboxes checked by default?

javaspringspring-mvc

提问by Dave

I'm using Spring 3.1.0.RELEASE. I have this field in my command object ...

我正在使用 Spring 3.1.0.RELEASE。我的命令对象中有这个字段......

public Set<EventFeed> getUserEventFeeds() {
    return this.userEventFeeds;
}

On my Spring JSP page, I want to display a checkbox list of all possible event feeds, and then have checked checkboxes if the user has one in his set. I want to have some special HTML formatting around each checkbox, so I'm trying ...

在我的 Spring JSP 页面上,我想显示所有可能的事件提要的复选框列表,然后如果用户在他的集合中有复选框,则选中复选框。我想在每个复选框周围设置一些特殊的 HTML 格式,所以我正在尝试......

<form:form method="Post" action="eventfeeds.jsp" commandName="user">
    ...
        <c:forEach var="eventFeed" items="${eventFeeds}">
        <tr>
            <td><form:checkbox path="userEventFeeds" value="${eventFeed}"/></td>
            <td>${eventFeed.title}</td>
        </tr>
        </c:forEach>
    ...

However, the items aren't getting checked by default if one is in the set. How do I do this? Here is the binder I'm using in my controller class ...

但是,如果项目在集合中,则默认情况下不会检查项目。我该怎么做呢?这是我在控制器类中使用的活页夹...

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(EventFeed.class, new EventFeedEditor());
}

private class EventFeedEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        setValue(eventFeedsDao.findById(Integer.valueOf(text)));
    }

    @Override
    public String getAsText() {
        return ((EventFeed) getValue()).getId().toString();
    }
}

采纳答案by raddykrish

@Dave, there is something called form:checkBoxes. You can try with that.

@Dave,有一种叫做 form:checkBoxes 的东西。你可以试试。

<form:checkboxes path="userEventFeeds" items="${eventFeeds}" itemLabel="id" itemValue="value"/>

My assumption here is you should have "id" and "value" defined in the EventFeed class.

我的假设是您应该在 EventFeed 类中定义“id”和“value”。

I just tried this by having a String[] availableList and String[] selectedList. It works like charm. You can give a try as well.

我只是通过使用 String[] availableList 和 String[] selectedList 来尝试这个。它就像魅力一样。你也可以试试。

回答by Jhonathan

You can do this by placing selected default property true in your class

您可以通过在您的班级中放置选定的默认属性 true 来做到这一点

class User {
   boolean userEventFeeds = true;

}

回答by Andrea

I've tried both form:checkboxesand form:checkboxwith the same data and the first works, the second doesn't. (Same release of Spring you have)

我都试过form:checkboxes,并form:checkbox使用相同的数据和第一部作品,第二个没有。(与您拥有的 Spring 版本相同)

It looks like there was a bugwhich, despite their claim, seems to be still there.

看起来有一个错误,尽管他们声称,但似乎仍然存在。

回答by keaplogik

Interestingly this works:

有趣的是,这有效:

<form:checkbox path="services" value="${type}" checked="checked"/>

回答by Angelo Fuchs

For my usecase (reacting on stuff in a list that has nothing to do with the object being filled), this code worked:

对于我的用例(对列表中与正在填充的对象无关的内容做出反应),此代码有效:

(Please be aware that this is a last-resort kind of code, other solutions are most likely better suited.)

(请注意,这是最后一种代码,其他解决方案很可能更适合。)

<%@ taglib prefix="jstl" uri="http://java.sun.com/jsp/jstl/core" %>
<jstl:forEach var="listObject" items="${someList}">
  <jstl:if test="${listObject.active}">
    <form:checkbox path="active" checked="checked"/>
  </jstl:if>
  <jstl:if test="${!listObject.active}">
    <form:checkbox path="active"/>
  </jstl:if>
</jstl:forEach>