JSF 2 : Status-Description selectonemenu - java

I have 2 selectonemenu elements, one that contains the status of the request, and the other contains a description of that status.
Im trying to implement it as follows :
I have a class called status that has 2 fields : status & description.
the selectonemenu would be :
<h:outputText id="requestStatusH" value="Status : " />
<h:selectOneMenu id="statusDD" value="#{exApp.status}">
<f:selectItems value="#{exApp.statusList}" />
</h:selectOneMenu>
and my exApp bean would be :
private RequestStatus status;
private List<RequestStatus> statusList;
//Getters + Setters
when i select a status in one selectonemenu i want the corresponding description to appear in another disabled selectonemenu.
The problem is that im not able to even get the selected value in to the backing bean variables.
after i submit the page, nothing is happening.
I tried to look into the stackoverflow selectonemenu guide, and i couldnt find anything about converters.
any help would be highly appreciated.
Edit :
My RequestStatus code :
public class RequestStatus implements Serializable {
public RequestStatus(String status, String description) {
this.status = status;
Description = description;
}
private String status;
private String Description;
//Getters + Setters
my form :
<h:form>
<p:commandLink id="saveChanges" value="Save Changes" actionListener=
"#{exApp.saveRespond()}"> </p:commandLink>
<h:outputText id="requestStatusH" value="Status : " />
<h:selectOneMenu id="statusDD" value="#{exApp.status}">
<f:selectItems value="#{exApp.statusList}" />
</h:selectOneMenu>
</h:form>
RequestStatus Class is not a backing bean, its a POJO that is used as a field in the RequestScope exApp backing bean.
my page is using Form. and when i submit my form NOTHING happens.

Related

<p:selectOneMenu> selected value always null

My goal is to get the selected value of type Sistema, so I can update a datatable and another SelectOneMenu.
Whenever a value is selected the ajax tag behaves correctly, and calls configura(), however my sistemaSelecionado (wich is an Object in Bean)always return as null to my Backing Bean.
The converter is working fine in another page with the same class Sistema. But it doesn't fire on this specific page.
This is the xhtml page
<div>
<p:outputLabel title="Sistema"></p:outputLabel>
</div>
<div>
<p:selectOneMenu id="selectOneMenu"
value="#{permissaoListBean.sistemaSelecionado}"
converter="entityConverter">
<p:ajax event="change" listener="#{permissaoListBean.configura}"
update="selectOneMenuGrupo,secoes"></p:ajax>
<f:selectItem itemLabel="Selecione" itemValue="" />
<f:selectItems value="#{permissaoListBean.listaSistema}"
var="sistema" itemLabel="#{sistema.nome}" itemValue="#{sistema}">
</f:selectItems>
</p:selectOneMenu>
</div>
Here is my Backing Bean
#Component("permissaoListBean")
#Scope("session")
public class PermissaoListBean {
private List<Sistema> listaSistema = new ArrayList<Sistema>();
//Service das entidades
#Autowired
private SistemaService sistema;
#Autowired
private GrupoService grupo;
#Autowired
private SecaoService secao;
//Objetos da view
private Sistema sistemaSelecionado;
private Grupo grupoSelecionado;
private List<Secao> secaoSelecionada = new ArrayList<Secao>();
private LazyDataModel<Secao> modelo ;
public void populaSistema(){
listaSistema = sistema.findAll();
}
public void configura(){
populaGrupo();
populaSecao();
}
public void populaGrupo(){
setListaGrupo(grupo.findBySistema(sistemaSelecionado));
}
//dataTable lazyLoad code
//end of lazyload
public List<Sistema> getListaSistema() {
setListaSistema(sistema.findAll());
return listaSistema;
}
public void setListaSistema(List<Sistema> listaSistema) {
this.listaSistema = listaSistema;
}
//getters and setters

JSF and Primefaces graphicImage rendered attribute changes but nothing happens

I've a problem that I can't find any solutions for. I'm using JSF 2.1 and primefaces, and I have a bean that contains a boolean variable with a getter and setter. I use p:ajax to call a method that updates the boolean value when p:graphicImage is updated.
During debugging this work correctly; p:graphicImage has the attribute rendered set to the boolean variable of the bean. When the image is updated by ajax however, the getter is called and returns the correct value, true, but image is not rendered!
This is my code:
<p:panel>
<p:panelGrid columns="3" cellpadding="5">
<p:row>
<p:column>
<p:outputLabel value="#{msg.username}"></p:outputLabel>
</p:column>
<p:column>
<h:inputText id="username" value="#{user.userName}">
<p:ajax listener="#{registationController.checkUserName}" event="blur" update="imgControl"/>
</h:inputText>
</p:column>
<p:column id="imgControl">
<p:graphicImage
value="/resources/images/#{registationController.userNameAvailable}"
height="24px" rendered="#{registationController.imgShow}"/>
</p:column>
<p:message for="username" id="msgUsername" severity="info"></p:message>
</p:row>
</p:panelGrid>
</p:panel>
And this is the bean:
#ManagedBean(name="registationController")
public class RegistrationController implements Serializable{
private static final long serialVersionUID = 1086976296682850824L;
#ManagedProperty(value="#{user}")
private User usr;
private MyLogger log;
private String userNameAvailable=null;
private boolean imgShow=false;
public void checkUserName(AjaxBehaviorEvent event){
User tempUser=null;
UserDAOImpl userDAOImpl=new UserDAOImpl();
if(usr.getUserName()!="" && usr.getUserName()!=null){
tempUser=userDAOImpl.selectUserByUsername(usr.getUserName());
imgShow=true;
} else {
imgShow=false;
}
if(tempUser!=null){
userNameAvailable="no.png";
} else{
userNameAvailable= "yes.png";
}
}
public boolean isImgShow() {
return imgShow;
}
others methods...
}
Any ideas why this doesn't works?
Thanks,
John

Reloading the same page based on a toggle button preserving the paramters

I have a page where I have a static list containing the list of products which are again grouped into product groups.I have a toggle button in the JSP page which shuffles between the enabled and disabled products .Code for my toggle button is as follows
<h:commandButton value="retrieve" image="#{displayProductsBean.productsToggleImage}" actionListener="#{displayProductsBean.fetchProductsBasedOnStatus}">
<c:choose>
<c:when test="${displayProductsBean.productFetchCriteria=='0'}">
<f:attribute name="buttonSelected" value="1" />
</c:when>
<c:otherwise>
<f:attribute name="buttonSelected" value="0" />
</c:otherwise>
</c:choose>
</h:commandButton>
Now in the managed bean I am able to get the value of the button selected and have logic to retrieve either enabled or disabled products
But I don't know how would I get back to the same page and also I don't want the list to be reloaded again from the DB.Code in my bean class is as follows
public void fetchProductsBasedOnStatus(ActionEvent event)
{
System.out.println("The fetchProductsBasedOnStatus in bean is called");
String selected = (String) event.getComponent().getAttributes().get("buttonSelected");
System.out.println("The value of toggle button is"+selected);
setProductFetchCriteria(Integer.parseInt(selected));
System.out.println("The value of toggle button is"+this.toString());
}
Somebody please help me resolve this .....
But I don't know how would I get back to the same page
Just return null or void in action method.
and also I don't want the list to be reloaded again from the DB
Just don't do that? If you keep the bean in the view scope and load the lists in the (post)constructor, then the same lists will be kept as long as the enduser is interacting with the same view. You should only not use JSTL tags as it breaks the view scope.
Your code can be simplified as follows:
<h:commandButton value="retrieve" image="#{bean.showDisabledProducts ? 'enabled' : 'disabled'}.png" action="#{bean.toggle}">
<f:ajax render="#form" />
</h:commandButton>
<h:dataTable value="#{bean.products}" ...>
...
</h:dataTable>
with
#ManagedBean
#ViewScoped
public class Bean {
private boolean showDisabledProducts;
private List<Product> enabledProducts;
private List<Product> disabledProducts;
#EJB
private ProductService service;
#PostConstruct
public void init() {
enabledProducts = service.listEnabledProducts();
disabledProducts = service.listDisabledProducts();
}
public void toggle() {
showDisabledProducts = !showDisabledProducts;
}
public List<Product> getProducts() {
return showDisabledProducts ? disabledProducts : enabledProducts;
}
public boolean isShowDisabledProducts() {
return showDisabledProducts;
}
}

JSF Bean property not updated [duplicate]

This question already has an answer here:
JSF does not populate #Named #RequestScoped bean with submitted input values
(1 answer)
Closed 5 years ago.
I have a bean with a field called 'name', and a JSF form that has an inputText mapped with this field. The initial value of the field is well displayed on the form.
The problem is when I submit the form, the value is not updated with the content of the inputText. In the savePlayer() method below, the value of name is always 'name', not what I typed inside the form input.
The bean :
#Named
#RequestScoped
public class PlayerForm {
#Inject
private PlayerRepository playerRepository;
private String name = "name";
public String savePlayer(){
Player player = new Player();
player.setName(name);
playerRepository.savePlayer(player);
return "saveUserOk";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
The form :
<h:form>
<h:inputText value="#{playerForm.name}" />
<h:commandButton value="Submit" action="#{playerForm.savePlayer}" />
</h:form>
Thanks very much for any help!
This can happen if you imported #RequestScoped from the package javax.faces.bean (JSF) instead of from javax.enterprise.context (CDI). Every single EL expression #{} would then create a brand new and completely separate instance of the bean. The given form example would then end up in two instances of the bean, one where the name is set and another where the action is invoked.
The javax.faces.bean.RequestScoped annotation can only be used in conjunction with JSF's own #ManagedBean annotation not with CDI's #Named annotation.

<h:inputText> howto execute bean method on blur?

I have <h:inputText> on form and what I need is to execute some method from backing bean on BLUR event:
public void test()
{
System.out.print("HELLO!");
}
Can you help me?
You can use <f:ajax>
<h:form>
<h:inputText value="#{managedBean.val}" >
<f:ajax event="blur" render="result" listener="#{managedBean.test}"/>
</h:inputText>
</h:form>
#ManagedBean(name = "managedBean")
public class Bean {
private String val; // getter and setter
...
public void test() {
System.out.print("HELLO!");
}
}
Alternative :
If you are using richfaces then you can use a4j:jsFunction
See Also
JSF2: Ajax in JSF – using f:ajax tag
How-to-update-a-value-displayed-in-the-page-without-refreshing

Categories