I've created a picklist via PrimeFaces. Now i want to handle the selected items which are listed in the target list when i click the commandButton.
I want to pass the data through the controller and store them in my database. But everytime i call the function duallist.getTarget() it's empty.
I've crated a foreach-Loop where i want to select all items in the target list:
Controller (Bean):
private List<DTOAktivitaet> source = new ArrayList<DTOAktivitaet>();
private List<DTOAktivitaet> target = new ArrayList<DTOAktivitaet>();
private List<DTOAktivitaet> zwischen = new ArrayList<DTOAktivitaet>();
public void speicherAktiZug() {
DTOAktivitaet aktivitaet_vorgaenger = null;
for (DTOAktivitaet item : controller.getAktivitaeten()) {
if (item.toString().equals(selected)) {
aktivitaet_vorgaenger = item;
}
}
for (DTOAktivitaet aktivitaet : zwischen) {
try {
dao.aktiZugAkt(aktivitaet_vorgaenger, aktivitaet);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public AktiListController() {
for (DTOAktivitaet ak : controller.getAktivitaeten()) {
source.add(ak);
}
aktis = new DualListModel<DTOAktivitaet>(source, target);
zwischen = aktis.getTarget();
}
JSF:
<h:form id="form" name="formular">
<h:outputText id="aktivitaet"
value="#{aktiListController.selected}" />
<p:pickList id="pickList" value="#{aktiListController.aktis}"
var="aktivitaet" itemValue="#{aktivitaet}"
itemLabel="#{aktivitaet}" converter="aktivitaetsConverter"
showSourceControls="true" showTargetControls="true" />
<h:commandButton
action="#{aktiListController.speicherAktiZug}"
value="Aktivität-Abhängigkeit anlegen" class="commandButton">
</h:commandButton>
</h:form>
Converter:
@EJB
public class AktiListConverter implements Converter {
private InitialisierungController controller = InitialisierungController
.getInstance();
DTOAktivitaet aktivitaet = new DTOAktivitaet();
String name = "";
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
for (DTOAktivitaet item : controller.getAktivitaeten()) {
if (item.toString().equalsIgnoreCase(arg2)) {
this.aktivitaet = item;
System.out.println(aktivitaet);
return aktivitaet;
}
}
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
this.aktivitaet = (DTOAktivitaet) arg2;
return this.name = aktivitaet.getTeambezeichnung();
}
}
My Problem: The target-List is empty before i want to store the items in my database.
I don't fully understand your code as it is not written in English but as far as I can see your Converter is written badly. As far as I can see you do a toString()
and a fromString()
basically. This is quite error prone and the way you did it, heavy in performance. It is a better idea to use unique ID's (business or database).
Example:
@FacesConverter(value = "aktiListConverter")
public class AktiListConverter implements Converter
{
private InitialisierungController controller = InitialisierungController.getInstance();
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
{
//Get object by it's unique ID
return controller.getById(Long.parseLong(arg2));
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
{
//Return object's unique ID
return ((DTOAktivitaet) arg2).getId();
}
}
In stead of using the object as itemLabel (which performs a toString()
) use something that generates a nice label like getName()
for a person.
itemLabel="#{aktivitaet.nameOrSomething}"
The speicherAktiZug()
method doesn't really make sense to me so I came this far:
public class AktiListController
{
private List<DTOAktivitaet> source;
private List<DTOAktivitaet> target = new ArrayList<DTOAktivitaet>();
private DualListModel<DTOAktivitaet> aktis;
public AktiListController()
{
source = controller.getAktivitaeten();
aktis = new DualListModel<DTOAktivitaet>(source, target);
}
//Getters and setters
public void speicherAktiZug()
{
target = aktis.getTarget();
//target should contain the picked items here.
}
}
I see you are also using aktiListController.selected
but I cannot see what it's used for.
converter="aktivitaetsConverter"
public class AktiListConverter implements Converter {...}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.