JSF <h:selectOneMenu> doesn't save my choose [duplicate] - jsf

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
(2 answers)
Closed 2 years ago.
I'm working with JSF and want to save a choose from a dropdown, but it doesn't save it, what can I do?
xhtml:
<h:form>
<h:selectOneMenu value="#{spielController.ausgewaehlteKategorie}" >
<f:selectItems value="#{spielController.alleKategorien}"/>
</h:selectOneMenu>
</h:form>
<br/>
<h:form>
<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
</h:form>
BackingBean:
#Named(value = "spielController")
#SessionScoped
public class SpielController implements Serializable {
private String ausgewaehlteKategorie;
private ArrayList<String> alleKategorien = new ArrayList<>();
public SpielController() throws SQLException {
for (String kategorie : kdao.getKategorien()) {
alleKategorien.add(kategorie);
}
}
public String getAusgewaehlteKategorie() {
return ausgewaehlteKategorie;
}
public void setAusgewaehlteKategorie(String ausgewaehlteKategorie) {
this.ausgewaehlteKategorie = ausgewaehlteKategorie;
}
public ArrayList<String> getAlleKategorien() {
return alleKategorien;
}
public void setAlleKategorien(ArrayList<String> alleKategorien) {
this.alleKategorien = alleKategorien;
}
}
I get the things from a DAO-class and it works, but when I press the button, it doesn't work because the choose I made above wasn't saved

Move
<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
to h:form
<h:form>
<h:selectOneMenu value="#{spielController.ausgewaehlteKategorie}" >
<f:selectItems value="#{spielController.alleKategorien}"/>
</h:selectOneMenu>
<br/>
<h:commandButton action="#{spielController.ladeFrage()}" value="Spielen"/>
</h:form>
This is the reason why your setter not invoked.

Related

Validate form when checking p:selectBooleanCheckbox [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to perform JSF validation in actionListener or action method?
(5 answers)
JSF doesn't support cross-field validation, is there a workaround?
(2 answers)
Closed 2 years ago.
I spent some days to try to validate part form with a OneSelect like as "Terms and Conditions", for example If are pre-filled Name and Telephone and I selected "Terms and Conditions" in this moment validate the previous fields are filled and enable "Next" button.
I tried using two ways, the first I put p:attribute inside p:selectOneRadio and received in method validateSelection() always null, the second is used validateSelection(String name, String telephone), but both methods show null values,
Does anyone know why this happens?
Thanks a lot.
(this is my view)
<h:form id="frmUser" enctype="multipart/form-data">
<p:outputLabel id="lblName" for="txtName" value="#{msg.lbl_name}" styleClass="lbl8"/>
<p:inputText id="txtName" maxlength="7" required="true" value="#{mainBean.userDTO.name}"/>
<p:message for="txtName" display="text"/>
<p:outputLabel id="lblTel" for="lblTel" value="#{msg.lbl_tele}" />
<p:inputText id="lblTel" maxlength="7" required="true" value="#{mainBean.userDTO.telephone}"/>
<p:message for="lblTel" display="text"/>
<p:outputLabel for="console" value="Accept Terms and Conditions" />
<p:selectOneRadio id="console" unselectable="true" value="#{mainBean.selectTerms}">
<f:selectItem itemLabel="Yes" itemValue="true" />
<f:selectItem itemLabel="No" itemValue="false" />
<f:attribute name="setname" value="#{mainBean.userDTO.name}" />
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection}" />
<!--WAY TWO -- >
<!--
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection(mainBean.userDTO.name,mainBean.userDTO.telephone)}" />
-->
</p:selectOneRadio>
</h:form>
This is my Bean
#Named("mainBean")
#SessionScoped
public class mainBean implements Serializable{
private UserDTO userDTO;
public String init() {
userDTO = new UserDTO();
//call url to display
}
/*
* Getters y Setters
* */
public UserDTO getUserDTO() {
return userDTO;
}
public void setUserDTO(UserDTO userDTO) {
this.userDTO = userDTO;
}
public void validateSelection(AjaxBehaviorEvent event) {
System.out.println("fill name"+ userDTO.getName());
System.out.println("fill name in ajax event :"+event.getComponent().getAttributes().get("setname"));
UIComponent component = event.getComponent();
System.out.println("fill name in ajax event component:"+
component.getAttributes().get("setname"));
//If two values are filled, eneabled NEXT BUTTON
}
public void validateSelection(String name, String telephone){
System.out.println("fill name"+name);
System.out.println("fill telephone"+telephone);
//If two values are filled, eneabled NEXT BUTTON
}
}
And this is the UserDTO class
public class UserDTO implements Serializable{
private String name;
private String telephone;
public UserDTO(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

Why doesn't panelgroup update after clicking? [duplicate]

This question already has answers here:
How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)
(3 answers)
Closed 6 years ago.
I try to update component panelgroup after clicking on commandLink, but it doesn't succeed. I have already tryed many ways but nothing helps. Please, say me, what should I do that to update panelgroup?
<h:panelGroup layout="block" id="content">
<h:panelGroup layout="block" id="nav">
<h:form id="itemsMenu">
<h:commandLink value="View" update="workplace" actionListener="#{main.determineAction}">
<f:param name="link" value="Viewing telephone book"/>
</h:commandLink>
</h:form>
</h:panelGroup>
<h:panelGroup id="workplace">
<h:panelGroup layout="block" rendered="#{main.responseRendered}">
<ui:include src="#{main.linkPage}"/>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
Bean code:
#ManagedBean(name = "main")
#ViewScoped public class MainBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<String> listItemsMenu;
private String linkPage;
private boolean responseRendered = false;
public boolean isResponseRendered() {
return responseRendered;
}
public void setResponseRendered(final boolean responseRendered) {
this.responseRendered = responseRendered;
}
public String getLinkPage() {
return linkPage;
}
public void setLinkPage(final String linkPage) {
this.linkPage = linkPage;
}
public void determineAction(final ActionEvent event) {
final Locale currentLocale = SessionBean.getCurrentLocale();
final MessageManager messageManager = new MessageManager(currentLocale);
final Map<String, String> mapParameters = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
final String linkType = mapParameters.get(Constants.ATTRIBUTE_LINK_TYPE);
if (linkType.equals(messageManager.getProperty(Constants.MESSAGE_MENU_VIEWING))) {
linkPage = Constants.PAGE_VIEW;
}
...
responseRendered = true;
}
}
Ok well command link doesn't have an attribute update, so that should have thrown an error here:
<h:commandLink value="View" update="workplace">
I'm assuming you want an ajax solution it would be:
<h:commandLink value="View">
<f:param name="link" value="Viewing telephone book"/>
<f:ajax render="workplace" listener="#{main.determineAction}" />
</h:commandLink>

commandLink in dataTable not invoked [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 7 years ago.
I have problems with invoking commandLink action in dataTable.
<h:body>
<h:dataTable value="#{manageStaffAccountBean.accounts}" var="staff">
<h:column>
#{staff.surname}
</h:column>
<h:column>
<h:form>
<h:commandButton value="Change status"
action="#{manageStaffAccountBean.changeActivity(staff)}" />
</h:form>
</h:column>
</h:dataTable>
</h:body>
By click on "Change status" i need to invoke changeActivity() method in bean
Managed Bean code:
#Named
#Scope("request")
public class ManageStaffAccountBean implements Serializable {
private List<Staff> accounts = null;
public String changeActivity(Staff staff){
System.out.println(staff.getId());
return "manageStaffAccounts";
}
public void updateAccountsList(){
accounts = staffService.findAll();
}
// ...
}
However, it is not invoked. Can you help me to find the problem?
I found the solution. If we use h:commandButton inside h:dataTable, our manage bean should have scope "session", or button wouldn't work. Just JSF magic

Sending JSF textbox value to a managed bean function as a parameter [duplicate]

This question already has an answer here:
How to send form input values and invoke a method in JSF bean
(1 answer)
Closed 7 years ago.
I have two textboxes and one submit button in my login.xhtml page. I also have a bean. Here are the codes:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Welcome to Online Banking</title>
</h:head>
<h:body>
<h:outputText value="Online Banking System Login" ></h:outputText>
<h:form>
<h:panelGrid columns="2" border="1">
<h:outputText value="Username:"></h:outputText>
<h:inputText id="username" value="#{loginBean.username}"></h:inputText>
<h:outputText value="Password"></h:outputText>
<h:inputSecret id="password" value="#{loginBean.password}" > </h:inputSecret>
<h:commandButton value="Login" action="#{loginBean.loginCheck(username, password)}"></h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
And the beans file:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
/**
*
* #author SUUSER
*/
#Named(value = "loginBean")
#Dependent
public class LoginBean {
/**
* Creates a new instance of LoginBean
*/
public LoginBean() {
}
private static String username="", password="";
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
public void setUsername(String Username){
username=Username;
}
public void setPassword(String Password){
password=Password;
}
public void loginCheck(String username, String password){
}
}
I will do the database check in my loginCheck function, so i need to pass the values of those two textboxes as a parameter. But i do not know how to do this. I just tried the code but it just passes empty strings as parameteres. Can anyone help me with this?
Thanks
Actually, you do not need to pass username and password parameters to a action method in your case.
A JSF page has a request life cycle. If you look inside JSF request life cycles, you will notice that values is applied to managed bean before the action.
Therefore, loginBean.username and loginBean.password values are set to managed bean username and password fields before the action. You can access them in the action method.
Your action will be
<h:commandButton value="Login" action="#{loginBean.loginCheck}"></h:commandButton>
and the action method
public String loginCheck(){
// search username and password in database.
// username, password field are set to values on page and accessible in the action method.
// Do not forget to navigate a proper page after according to login attempt.
}
For further reading
Communication in JSF 2.0 tutorial by BalusC
Basic Login Mechanism using Filters
JSF Request Life Cycle
How to pass value from textbox to Bean
1- create a .xhtml file
<h:form id="frm">
<h:inputText id="t1" value="#{user.x}" />
<h:commandButton action="#{user.check}" value="ok"/>
<h:outputText value="Value is #{user.msg}" ></h:outputText>
</h:form>
2- create a bean
ManagedBean(name="user")
RequestScoped
public class UserNumber {
int x;
String msg;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void check()
{
if(x%2==0)
{
msg= "Even";
}
else
{
msg= "Odd";
}
}
public UserNumber() {
}
}
JSF automatically binds textbox values to variables in backing bean.
So you don't need to pass values to backing bean in calling function. You can just put like this
<h:commandButton value="Login" action="#{loginBean.loginCheck}"></h:commandButton>
And in bean you can use those as regular variable. I also suggest not to use static variables to store backing bean data.
public String loginCheck(){if(username == <fromDB> && password == <fromDB>) return "loginsuccess"; else return "loginfailure"; }
Hope this helps.
Well, from what I understand, do you that check in your loginCheck function the values of those textboxes. Automatically the JSF set the values of parameters to the variables, so you can work with them by gets(). For example is the following:
<h:form>
<h:panelGrid columns="2" border="1">
<h:outputText value="Username:"></h:outputText>
<h:inputText id="username" value="#{loginBean.username}"></h:inputText>
<h:outputText value="Password"></h:outputText>
<h:inputSecret id="password" value="#{loginBean.password}" > </h:inputSecret>
<h:commandButton value="Login" action="#{loginBean.loginCheck()}"></h:commandButton>
</h:panelGrid>
</h:form>
end in his ManagedBean is the following:
#ManagedBean(name= "loginBean")
public class LoginBean {
public LoginBean() {
}
// gets and sets
public void loginCheck(){
if(getUsername() != null && getPassword() != null){
// and here you chek database parameters
}
}
}

How to dynamically add an input field [duplicate]

This question already has answers here:
How to dynamically add JSF components
(3 answers)
Closed 7 years ago.
I want to add dynamically input fields. Like
Is there a good component in PF on how to add such a component?
Pls give me a hint on how you would develop it, cause I have no clue at the moment.
I really appreciate your answer.
My technology stack:
Hibernate: 4.0.1.Final
Spring: 3.1.1.RELEASE
Primefaces: 3.5
jsf-version: 2.2.0-m08
PrimefacesMobile-version: 0.9.3
Apache Tomcat/7.0.12
Maybe the following piece of code can help you out, I'm afraid there's not a component for this (at least to my knowledge):
HTML
<h:form>
<ui:repeat value=#{bean.values}
var="value">
<h:inputText value="#{value}" />
<br />
</ui:repeat>
<h:commandButton value="Extend">
<f:ajax listener="#{bean.extend}"
process="#form"
render="#form" />
</h:commandButton>
<h:commandButton action="#{bean.submit}"
value="Save" />
</h:form>
BEAN
#ManagedBean
#ViewScoped
public class Bean {
private List<String> values;
#PostConstruct
public void init() {
values = new ArrayList();
values.add("");
}
public void submit() {
// save values in database
}
public void extend() {
values.add("");
}
public void setValues(List<String> values) {
this.values = values;
}
public List<String> getValues() {
return values;
}
}

Resources