Spring p:selectManyCheckbox:将所选值放入bean中
我想为一个项目分配一个用户列表,那么我的xhtml如下所示:Spring p:selectManyCheckbox:将所选值放入bean中,spring,jsf,jakarta-ee,web,primefaces,Spring,Jsf,Jakarta Ee,Web,Primefaces,我想为一个项目分配一个用户列表,那么我的xhtml如下所示: <p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom"> <p:column> <
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
这是保存在ProjetComponent中的方法:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
private Projet projet;
private Utilisateur utilisateurs;
@Autowired
private ProjetDAO projetDAO;
@Autowired
private UtilisateurDAO utilisateurDAO;
@Autowired
private ProjetService projetService;
@Transactional
public String saveProjetUtilisateurs1(Projet p, List<Utilisateur> utilisateur) {
projet = projetService.saveProjetUtilisateurs(p, utilisateur);
return "/jsf/projet/viewProjet.xhtml";
}
<p:ajax event="change" process="@this" />
private Projet Projet;
私人提款人或提款人;
@自动连线
私人工程项目;
@自动连线
私人用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途用途;
@自动连线
私人项目服务项目服务;
@交易的
公共字符串saveProjetUtilisateurs1(项目p,列表利用者){
projet=projetService.saveProjetUtilisateurs(p,利用率);
返回“/jsf/projet/viewProjet.xhtml”;
}
此方法保存在组件ProJetComponent调用的ProjetService类中:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
@Transactional
public Projet saveProjetUtilisateurs(Projet projet,List<Utilisateur> ut)
{
projet.setAvancement(projet.getAvancement());
projet.setConfidentialite(projet.getConfidentialite());
projet.setDatedebut(projet.getDatedebut());
projet.setDatefineffective(projet.getDatefineffective());
projet.setDatefinprevu(projet.getDatefinprevu());
projet.setDescription(projet.getDescription());
projet.setDurreprojet(projet.getDurreprojet());
projet.setNomprojet(projet.getNomprojet());
projet.setObjectifprojet(projet.getObjectifprojet());
projet.setStatut(projet.getStatut());
projet.setUtilisateurs(ut);
projet = projetDAO.store(projet);
projetDAO.flush();
return projet;
}
<p:ajax event="change" process="@this" />
@Transactional
公共项目saveProjetUtilisateurs(项目项目,列表ut)
{
projet.setAvancement(projet.getAvancement());
projet.setsecretentialite(projet.getsecretentialite());
projet.setDateFirst(projet.getDateFirst());
projet.setDateFinefective(projet.getDateFinefective());
projet.setDatefinprevu(projet.getDatefinprevu());
projet.setDescription(projet.getDescription());
projet.setDurreprojet(projet.getDurreprojet());
projet.setNomprojet(projet.getNomprojet());
projet.setObjectiveProjet(projet.getObjectiveProjet());
projet.setStatut(projet.getStatut());
项目设置利用率(ut);
projet=projetDAO.store(projet);
projetDAO.flush();
返回项目;
}
这是Projet实体:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
@Entity
public class Projet implements Serializable {
@Id
private Integer idprojet;
@ManyToMany(mappedBy = "projets", fetch = FetchType.LAZY)
java.util.List<com.gestion.projet.domain.Utilisateur> utilisateurs;
}
<p:ajax event="change" process="@this" />
@实体
公共类Projet实现可序列化{
@身份证
私人整数idprojet;
@ManyToMany(mappedBy=“projets”,fetch=FetchType.LAZY)
java.util.List-Utisateurs;
}
屏幕截图:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
问题是获取一个空的用户列表:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
尝试在
h:selectManyCheckbox
上使用p:ajax
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
如果希望在Primeface Datatable上使用基于复选框的多项选择,则无需使用显式的h:selectManyCheckbox
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
您可以通过在第一个/最后一个p:column
上指定selectionMode=“multiple”
属性来实现这一点
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
请参阅。这是实体转换器:
<p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}" var="current" rows="15" paginator="true" paginatorPosition="bottom">
<p:column>
<h:selectManyCheckbox id="selectUser" value="#{ProjetComponent.projet.utilisateurs}" >
<f:selectItem var="utilisateurs" value="#{utilisateurs.iduser}" itemLabel=""/>
<f:converter converterId="entityConverter" />
</h:selectManyCheckbox>
</p:column>
</p:dataTable>
</h:panelGrid>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{projetmsgs['navigation.save']}" action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet,ProjetComponent.projet.utilisateurs)}"/>
</h:panelGroup>
</p:panel>
<p:ajax event="change" process="@this" />
@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {
private static Map<Object, String> entities = new WeakHashMap<Object, String>();
public String getAsString(FacesContext context, UIComponent component, Object entity) {
synchronized (entities) {
if (!entities.containsKey(entity)) {
String uuid = UUID.randomUUID().toString();
entities.put(entity, uuid);
return uuid;
} else {
return entities.get(entity);
}
}
}
public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
for (Entry<Object, String> entry : entities.entrySet()) {
if (entry.getValue().equals(uuid)) {
return entry.getKey();
}
}
return null;
}
}
@FacesConverter(value=“entityConverter”)
公共类EntityConverter实现转换器{
私有静态映射实体=new WeakHashMap();
公共字符串getAsString(FacesContext上下文、UIComponent组件、对象实体){
已同步(实体){
如果(!entities.containsKey(实体)){
字符串uuid=uuid.randomUUID().toString();
实体。put(实体,uuid);
返回uuid;
}否则{
返回实体。获取(实体);
}
}
}
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串uuid){
for(条目:entities.entrySet()){
if(entry.getValue().equals(uuid)){
return entry.getKey();
}
}
返回null;
}
}
selectionMode=“multiple”也有同样的问题,然后在p:dataTable
本身上使用p:ajax
<代码>。如果仍然存在问题,则您的entityConverter
中可能存在问题。