All bean attributes are assigned null - spring-boot

I'm writing an application using Spring Boot. I'd like to create an instance of the bean LobbyBean, but after heaving filled out the form, all attributes are still null. Does anybody have an idea, why the input data doesn't get through to the controller? Any help is greatly appreciated.
Here is the (simplified) code:
LobbyBean
#Component
#Scope("session")
public class LobbyBean {
#GeneratedValue
private int pin;
private QuestionSet questionSet;
private User host;
private QuestionDifficulty questionDifficulty;
private int maxNumberOfPlayers;
private boolean hasJoker;
// getters and setters for all attributes
LobbyController
#Component
#Scope("view")
public class LobbyController {
#Autowired
private GameManager gameManager;
#Autowired
private UserService userService;
private LobbyBean newLobby = new LobbyBean();
// getter and setter for newLobby
public void createLobby()
{
newLobby.setHost(userService.getAuthenticatedUser());
gameManager.saveLobby();
newLobby = new LobbyBean();
}
lobbycreation.xhtml
<ui:define name="content">
<h:form id="lobbysettings">
<p:panel>
<h:panelGrid id="settings" columns="2">
<p:outputLabel value="Questionset: "/>
<p:selectOneMenu id="questionset" value="#{lobbyController.newLobby.questionSet}">
<f:selectItems value="#{lobbyDetailController.allQuestionSetNames}"/>
</p:selectOneMenu>
<p:outputLabel value="Difficulty: "/>
<p:selectOneMenu id="difficulty" value="#{lobbyController.newLobby.questionDifficulty}" >
<f:selectItems value="#{lobbyDetailController.allQuestionDifficulties}"/>
</p:selectOneMenu>
<p:outputLabel value="Max Num of Players: "/>
<p:inputNumber decimalPlaces="0" value="#{lobbyController.newLobby.maxNumberOfPlayers}" />
<p:outputLabel value="Joker?"/>
<p:inputSwitch value="#{lobbyController.newLobby.hasJoker}" />
<p:commandButton id="createLobbyButton" value="Lobby Erstellen" process="#this"
action="#{lobbyController.createLobby}"/>
</h:panelGrid>
</p:panel>
</h:form>
</ui:define>

Replace the process="#this"on your p:commandButton with process="#form" otherwise only the commandButton will be part of the submit.

Related

PrimeFaces Ajax call only once through the application

Hi i am developping a jsf spring application and i am using primefaces library
what i found is that ajax calls
<p:ajax listener="#{etatTest.onClientChange}" update="pret"/>
<p:ajax listener="#{etatTest.onEntiteChange}" update="clients"/>
<are not firing at all , i tried to debug but nothing
this is my bean
#Named
#ViewScoped
public class EtatTest implements Serializable{
#Autowired
private IEntiteManager entiteManager;
private Integer idEntite;
private List<Entites> ents;
private Entites entite;
#Autowired
private ICrmClientManager clientManager;
private Crmclients crmClient;
private List<Crmclients> crmClients;
#Autowired
private IBqPretManager bqpretManager;
private Bqpret pret;
private List<Bqpret> listPret;
#PostConstruct
public void init() {
ents =entiteManager.AllEntites("03052021105404", 1);
}
public void onEntiteChange()
{
if(entite !=null && !entite.equals(null)) {
crmClients=clientManager.getByEntite(entite);
}else {
crmClients=new ArrayList<Crmclients>();
}
}
public void onClientChange() {
if(crmClient==null && !crmClient.equals(null)) {
listPret = bqpretManager.listBqpret(null,null,null ,crmClient.getId(),null,null, null,null,null, null);
}else {
listPret=new ArrayList<Bqpret>();
}
}
//getters and setters
<h:form>
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true"/>
<p:panel header="Select a Location" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="entite" value="Entity: "/>
<p:selectOneMenu id="entite" value="#{etatTest.entite}" style="width:150px">
<p:ajax listener="#{etatTest.onEntiteChange}" update="clients"/>
<f:selectItem itemLabel="Select Entite" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{etatTest.ents}" var="p" itemLabel="#{p.libelle}" itemValue="#{p.id}"/>
</p:selectOneMenu>
<p:outputLabel for="clients" value="Client: "/>
<p:selectOneMenu id="clients" value="#{etatTest.crmClient}" style="width:150px">
<p:ajax listener="#{etatTest.onClientChange}" update="pret"/>
<f:selectItem itemLabel="Select client" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{etatTest.crmClients}" var="p" itemLabel="#{p.libelle}" itemValue="#{p.id}"/>
</p:selectOneMenu>
<p:outputLabel for="pret" value="Pret: "/>
<p:selectOneMenu id="pret" value="#{etatTest.pret}" style="width:150px">
<f:selectItem itemLabel="Select pret" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{etatTest.listPret}" var="p" itemLabel="#{p.libelle}" itemValue="#{p.id}"/>
</p:selectOneMenu>
</h:panelGrid>
</p:panel>
</h:form>

It is possible to have two lisneter using tag <p:ajax .... /> in a form ? (Primefaces and JSF )

I have a form that consists of 3 dropdown and when i chose an item from the first dropdown the listener retrieve the list of the 2nd dropdown perfectly. But when i chose an item from the 2nd dropdown listener does not work and does not even mention the method of my bean.
Here is my form
<p:outputLabel for="structure" value="Structure"/>
<h:panelGroup >
<p:selectOneMenu id="structure" value="#{etpBean.idt_structure}" style="margin-bottom:10px;" required="true">
<p:ajax listener="#{etpBean.onRessourceChange}" update="affectation" />
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{etpBean.structures}" var="structure" itemValue="#{structure.idt_structure}" itemLabel="#{structure.nom}" />
</p:selectOneMenu>
</h:panelGroup>
<p:outputLabel for="affectation" value="les ressources affectee"/>
<h:panelGroup >
<p:selectOneMenu id="affectation" value="#{etpBean.idt_affectation}" style="margin-bottom:10px;" required="true">
<p:ajax listener="#{etpBean.onAffectationChange}" update="activite" />
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{etpBean.affectations}" var="affectation" itemValue="#{affectation.idt_affectation}" itemLabel="#{affectation.ressource.nom}" />
</p:selectOneMenu>
</h:panelGroup>
<p:outputLabel for="activite" value="Activite"/>
<h:panelGroup >
<p:selectOneMenu id="activite" value="#{etpBean.idt_parent}" style="margin-bottom:10px;" required="true">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{etpBean.activites}" var="activite" itemValue="#{activite.idt_parent}" itemLabel="#{activite.nom}" />
</p:selectOneMenu>
</h:panelGroup>
<p:outputLabel for="pes" value="charge effectué"/>
<h:panelGroup>
<p:inputText id="pes" value="#{etpBean.etp}" />
<p:slider for="pes" step="1" />
</h:panelGroup>
</p:panelGrid>
</p:panel>
</h:form>
And Here is my bean
#ManagedBean(name="etpBean")
#ViewScoped
public class EquivalentTempPleinBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Double etp;
private Date dateDebut;
private Date dateFin;
private Long idt_affectation;
private Long idt_parent;
private Long idt_ressource;
private Long idt_structure;
private Collection<Ressource> ressources;
private Collection<Structure> structures;
private Collection<Activite> activites;
private Collection<Affectation> affectations;
private ETP equivalentTempPlein = new ETP();
private Ressource ressource = new Ressource();
private Affectation affectation;
private Activite activite = new Activite();
private Structure structure;
#ManagedProperty(value = "#{ressourceService}")
private IRessourceService ressourceService;
#ManagedProperty(value = "#{structureService}")
private IStructureService structureService;
#ManagedProperty(value = "#{activiteService}")
private IActiviteService activiteService;
#ManagedProperty(value = "#{affectationService}")
private IAffectationService affectationService;
public EquivalentTempPleinBean() {
super();
}
#PostConstruct
public void init()
{
structures=structureService.listStructures();
}
// work perfeclty
public void onRessourceChange(){
System.out.println("On ressource Change");
structure =structureService.findStructureById(idt_structure);
affectations=affectationService.affectationByStructure(idt_structure);
//ressources=ressourceService.ressourceOnAffectation(idt_structure);
}
//dont work
public void onAffectationChange(){
System.out.println("##########################################################");
affectation=(Affectation) affectationService.findAffectationById(idt_affectation);
activites=activiteService.activiteOnAffectation(idt_structure,affectation.getDateDebut(),affectation.getDateFin());
}
//Getters and setters
}

p:commandButton update action won't clear input fields after submission

<h:form id="registrationForm">
UserName:
<p:inputText id="usernameInput" value="#{userUI.thisUsername}" />
<br />
Password:
<p:inputText id="passwordInput" value="#{userUI.thisPassword}" />
<br />
<p:commandButton actionListener="#{userUI.createUser}"
update="registrationForm" value="Create" />
</h:form>
I want this form to reset its values after submission.
I enter the username and password, hit submit, it resets the values and persists empty values, not the ones i entered. Tried moving the commandButton out of h:form but it didn't change a thing. Also tried adding
<p:ajax update="registrationForm" resetValues="true" />
It still persisted empty values.
How do i do it right?
Give this a try:
Bean is of scoped Request.
To reset values reinitialize fields in post constructor, or call it manually in your createUser method.
#ManagedBean
#RequestScoped
public class UserUI implements Serializable {
private static final long serialVersionUID = 1L;
private String thisUsername;
private String thisPassword;
public UserUI(){
}
#PostConstruct
public void init(){
thisUsername="";
thisPassword="";
}
public void createUser(){
System.out.println("ThisUserName :"+thisUsername);
System.out.println("thisPassword :"+thisPassword);
init();
}
/////////GETTER SETTER
//thisUsername
//thisPassword
}
Your webPage.xhtml will be look like:
<h:form id="registrationForm">
<p:panelGrid columns="2">
<h:outputLabel value="UserName:" for="usernameInput" />
<p:inputText id="usernameInput" value="#{userUI.thisUsername}" />
<h:outputLabel value="Password:" for="passwordInput" />
<p:inputText id="passwordInput" value="#{userUI.thisPassword}" />
<p/>
<p:commandButton action="#{userUI.createUser}" update="#form" value="Create" />
</p:panelGrid>
</h:form>

jsf inputText don't execute change listener event

i have two .xhtml pages with two similar inputText elements ("customer id" and "customer name"), in both pages the inputText for "customer id" launch a request ajax event to looking for the name of the customer and to put it in the inputText for "customer name".
when i run the web application and use the first pages everything work fine, but if i don't use the first page and go to the second page (via commandlink in first page) without use the first page then the inputText for search the customer name don't work, indeed not enter to the testBean2.searchCustId method.
the code:
first page
<h:form>
<h:commandLink action="page2">PAGE 2</h:commandLink><br/>
<h:outputText value="customer id: "/>
<h:inputText id="custoId" label="CUSTOMER_ID" value="#{testBean.custId}" required="true">
<a4j:ajax event="change" listener="#{testBean.searchCustId(testBean.custId)}" render="custoName"/>
<f:validateLength minimum="1" maximum="12"/>
</h:inputText>
<rich:message for="custoID" ajaxRendered="true"/>
<h:outputText value="Customer name: "/>
<h:inputText id="custoName" label="CUSTOMER_NAME" value="#{testBean.custName}"/>
<rich:message for="custoName" ajaxRendered="true"/>
</h:form>
second page
<h:form>
<h:outputText value="customer id: "/>
<h:inputText id="custoId2" label="CUSTOMER_ID2" value="#{testBean2.custId}" required="true">
<a4j:ajax event="change" listener="#{testBean2.searchCustId(testBean2.custId, 0)}" render="custoName2"/>
<f:validateLength minimum="1" maximum="12"/>
</h:inputText>
<rich:message for="custoId2" ajaxRendered="true"/>
<h:outputText value="customer name: "/>
<h:inputText id="custoName2" label="CUSTOMER_NAME2" value="#{testBean2.custName}"/>
<rich:message for="custoName2" ajaxRendered="true"/>
</h:form>
now jsf managed bean
testBean
public class TestBean {
private String custId;
private String custName;
public TestBean() {
}
//getter and setter
public void searchCustId(String ced){
custName = ced + "- SOME CUSTOMER NAME"; //THIS IS FOR TESTING
}
}
testBean2
public class TestBean2 {
private String custId;
private String custName;
public TestBean2() {
}
//getter and setter
public void searchCustId(String ced, int que){
custName = ced + " - " + que; //THIS IS FOR TESTING
}
}
what may be happening?
i'm new on this and my english is not good :)
You can use f:setPropertyActionListener.
<a4j:ajax event="change" listener="#{myBean.listener}">
<f:setPropertyActionListener target="#{myBean.someProperty}" value="your_value_here"/>
</a4j:ajax>
But from your code it looks like you are trying to pass the very one value that's used on h:inputText, that's not necessary:
<h:inputText id="custoId" label="CUSTOMER_ID" value="#{testBean.custId}" required="true">
<a4j:ajax event="change" listener="#{testBean.searchCustId}" render="custoName"/>
<f:validateLength minimum="1" maximum="12"/>
</h:inputText>
After Ajax event the values testBean.searchCustId will automatically be updated on Managed bean after passing the validation, so your listener could be:
public class TestBean {
private String custId;
private String custName;
public TestBean() {
}
//getter and setter
public void searchCustId(){
custName = custId + "- SOME CUSTOMER NAME"; //THIS IS FOR TESTING
}
}

p:selectOneMenu ajax does not fire within p:dataGrid

I have a selectOneMenu rendered for each row of my dataGrid. The problem is the method of the ajax listener is not called when the selection changes.
If I use the same selectOneMenu outside the dataGrid, it works fine. Same behaviour occurs with p:selectBooleanCheckbox.
XHTML page:
<h:form id="form2">
<p:dataGrid id="gridC" widgetVar="gridC"
value="#{myBean.comp}" var="site" columns="1" rowIndexVar="siteIndex">
<p:column>
<h:outputText value="#{site.sito}" />
</p:column>
<p:column>
<p:selectOneMenu id="stato" value="#{site.stateId}"
required="true">
<p:ajax update="#form :tabView:frm_buttons" global="false"
listener="#{myBean.testChangeState}" />
<f:selectItems value="#{myBean.siteStates}" var="s"
itemLabel="#{s.state}" itemValue="#{s.stateId}" />
</p:selectOneMenu>
</p:column>
</p:dataGrid>
</h:form>
Managed bean:
#ManagedBean
#ViewScoped
public class MyBean implements Serializable {
private SiteState siteStates;
private Comp comp;
// getters and setters...
public void testChangeState() {
System.out.println("Test change state fired.");
}
}
SiteState bean:
public class SiteState implements Serializable {
private String state;
private String stateId;
// getters and setters...
}
Found the secret.
The data grid is within a tab of an accordion panel, and I used an id like this:
<p:tab id="sito#{sito.idSitoStoccaggio}"
This is not the case, because it produces strange behaviours in some situations, like events not firing from within a data grid, for example.
Switching to:
<p:tab id="sito"
resolved my issue.

Resources