【问题标题】:How to insert PrimeFaces p:selectManycheckbox value to database如何将 PrimeFaces p:selectManycheckbox 值插入数据库
【发布时间】:2018-03-01 10:41:22
【问题描述】:

我是 primefaces 的新手,在将我的 primefaces SelectManyCheckbox 值保存到数据库时遇到问题。我正在使用休眠和mysql。示例代码如下所示

我的 xhtml 页面代码是:

   <h:outputText value="#{msg['elicense.examinationform.personal.classofcertificates']}"/> 
        <p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedClass}" layout="grid" columns="1">
            <f:selectItems value="#{examinationFormBean.examinationPart}"var="className" itemLabel="#{className.name}" itemValue="#{className}" />
      </p:selectManyCheckbox>

我的豆子是:

  private String[] selectedClass;
  private List<CertificateClass> examinationPart=new ArrayList<CertificateClass>();
  getter()
  setter()

我要保存复选框的方法是:

        private void saveExaminationDetails()
          {
             examDetails.setElementaryPrinciples();  //bolean field
             examDetails.setLightinig()
             //no of setter
           }

我不知道如何在方法上设置选中和未选中的复选框值

【问题讨论】:

  • 如果我没记错的话,sele p:selectManyCheckbox 将选择值保存在字符串集合(列表、数组列表...等)中。你只需要保存集合中的每个元素。
  • 我将作为答案发布 好的,关闭这个问题,可以吗?
  • 请不要帮我解决它,因为我没有找到解决方案

标签: jsf jsf-2 primefaces


【解决方案1】:

查看 primefaces 展示:http://primefaces-rocks.appspot.com/ui/selectManyCheckbox.jsf

examinationFormBean.examinationPart 中的选定值应设置在p:selectManyCheckbox 属性value 中,然后您可以在 bean 方法中使用此选定列表。 你的例子应该是这样的:

    <p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedExaminationParts}" layout="grid" columns="1">
                <f:selectItems value="#{examinationFormBean.examinationParts}" var="className" itemLabel="#{className.name}" itemValue="#{className}" /> 
</p:selectManyCheckbox>

然后你可以在你的saveExaminationDetails()中使用selectedExaminationParts

【讨论】:

  • 我的问题是我需要一个复选框。如果是检查,它应该显示为真,否则为假
  • 有什么问题?您有examineFormBean.selectedExaminationParts 和examineFormBean.examinationParts 列表。因此,您可以返回一些其他对象,而不仅仅是检查部分模型,如果需要,为此需要使用 getAsObject 和 getAsString 方法实现转换器stackoverflow.com/questions/19883771/…
【解决方案2】:

p:selectManyCheckbox 选择值与托管 bean 上的 String Collection(List, ArrayList...等) 竞争。您只需将存在的每个String 保存在Collection 上。

我会给你一个例子来展示你如何做到这一点:

例子:

...
@Named(value = "myBean")
@SessionScoped
public class InscricaoBean implements Serializable {
...
private List<String> selectedElemnts = new ArrayList();

//selectedElements get and set
...

在 JSF 上你有类似的东西:

...
<h:outputText value="#{msg['elicense.examinationform.personal.classofcertificates']}"/> 
        <p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedElemnts}"...>
            <f:selectItems value="#{examinationFormBean.examinationPart}"var="className"   
            itemLabel="#{className.name}" itemValue="#{className}" />
      </p:selectManyCheckbox>
...

关于保存方法:

...
private void saveExaminationDetails()
{
   for (String nameAux: selectedElemnts )
   {
       //you save the data here
   }
}
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多