Can not use hidden commandButton with #RequestScoped backing-bean - jsf-2

I have following sample code.
Initially, only commandButton Two is visible. When I click this button, commandButton One is also visible. But when I click One, the backing-bean method click1 does not get fired.
Following is my code:
xhtml
<h:form id="form1">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}" />
</h:form>
<h:form id="form2">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button2" value="Two" action="#{bean.click2}" />
</h:form>
backing-bean
#RequestScoped
#Named("bean")
public class JsfTrial implements Serializable {
private static final long serialVersionUID = 2784462583813130092L;
private boolean show1; // + getter, setter
public String click1() {
System.out.println("Click1()");
return null;
}
public String click2() {
System.out.println("Click2()");
setShow1(true);
return null;
}
}
I found a very informative answer by BalusC.
h:commandLink / h:commandButton is not being invoked
If I understand it correctly, my problem is due to point 5 of this answer.
Does that also mean we can not use hidden commandButton with #RequestScoped backing-bean?

You can use the request scope, you should only pass the condition as a request parameter to the subsequent requests by <f:param> instead of by a JSF hidden input field <h:inputHidden>. The value of the hidden input field is only set in the model during Update Model Values phase, while the condition of the rendered attribute is already evaluated during Apply Request Values phase which is earlier.
So, use <f:param> instead of <h:inputHidden>:
<h:form id="form1">
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}">
<f:param name="show1" value="#{bean.show1}" />
</h:commandButton>
</h:form>
<h:form id="form2">
<h:commandButton id="button2" value="Two" action="#{bean.click2}">
<f:param name="show1" value="#{bean.show1}" />
</h:commandButton>
</h:form>
This way you can extract them as request parameter in bean's (post)constructor.
public JsfTrial() {
String show1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("show1");
this.show1 = (show1 != null) && Boolean.valueOf(show1);
}
Ugly, but CDI doesn't offer a builtin annotation which substituties JSF's #ManagedProperty("#{param.show1}"). You could however homegrow such an annotation.

Related

update component in another xhtml form from Custom layout p:selectOneRadio not working

My xhtml is split in to Menu area (defaultMenu.xhtml) and Content area (defaultContent.xhtml).
The code for defaultMenu.xhtml is:
<h:form id="defaultmenuform">
<p:outputPanel id="menupanel" class="contain auto-fixed-center">
<p:panel id="pmenu" visible="#{phController.user.menuVisible}">
<table id="stutable">
<tr>
<td width="15%">
<p:outputLabel id="stuname" value="#{phController.phBean.studentName}" />
</td>
<td>
<p:tabMenu activeIndex="#{param.selectedtab}">
<p:menuitem value="Home" outcome="phome" icon="ui-icon-star">
<f:param name="selectedtab" value="0" />
</p:menuitem>
<p:menuitem value="Bank" outcome="bhome" icon="ui-icon-person">
<f:param name="selectedtab" value="1" />
</p:menuitem>
</p:tabMenu>
</td>
</tr>
</table>
</p:panel>
</p:outputPanel>
</h:form>
The defaultContent.xhtml actually displays the ph.xhtml content (as part of functional navigation) and the code is:
<ui:define name="content">
<f:event listener="#{phController.readPeople}" type="preRenderView">
</f:event>
<h:form id="form">
<p:selectOneRadio id="selstud" value="#{phController.phBean.ssSeq}" layout="custom">
<p:ajax update=":defaultmenuform:parentmenupanel :defaultmenuform:stuname" listener="#{phController.onChangePerson}"/>
<f:selectItems value="#{phController.selectStudents}" />
</p:selectOneRadio>
<div style="width: 300px; float:left;">
<p:dataGrid var="studentlist" value="#{phController.listStudent}" columns="1" rowIndexVar="stuindex">
<p:panel header="" style="text-align:left">
<h:panelGrid columns="1" style="width:100%">
<h:outputText value="#{studentlist.studentName}" />
<p:radioButton for=":form:selstud" itemIndex="#{stuindex}"/> Select
</h:panelGrid>
</p:panel>
</p:dataGrid>
</div>
</h:form>
</ui:define>
The code for backing bean is:
Map<String, Object> studentparam = new HashMap<>();
studentparam.put("studentSeq", phBean.getSsSeq());
lS = getBaseDAOService().readStudent("readStudent", studentparam);
phBean.setStudentName(lS.get(0).getStudentFirstName() + " " + lS.get(0).getStudentLastName());
As you can see, I am calling the onChangeStu method to display the selected Student Name in defaultMenu.xhtml. I am using Custom Layout p:selectOneRadio in ph.xhtml and onClick trying to update a p:outputLabel in defaultMenu.xhtml.
The backing bean method gets invoked successfully and the value is also set in variable phController.phBean.studentName, but the update is not working. I also checked using view source and the id is “:defaultmenuform:stuname”, I also tried updating the menu panel ":defaultmenuform:menupanel”, but none of this works.
Not sure how to resolve this. Please suggest.
Including the structure of all .xhtmls
<h:body id="entirePageBody">
<div id="page">
<ui:insert name="header" >
<ui:include src="/template/defaultHeader.xhtml" />
</ui:insert>
<ui:insert name="menu" >
<ui:include src="/template/defaultMenu.xhtml" />
</ui:insert>
<div id="content_div" class="auto-fixed-center">
<div id="content_div_padding" class="content-block">
<ui:insert name="content" >
<ui:include src="/template/defaultContent.xhtml" />
<ui:debug hotkey="z" />
</ui:insert>
</div>
</div>
<ui:insert name="footer" >
<ui:include src="/template/defaultFooter.xhtml" />
</ui:insert>
</div>
</h:body>
PhController.java:
public class PhController extends BaseController implements Serializable {
private List<Stud> listStudent;
private List selectStudents;
SelectItem option;
private PhBean phBean;
private Boolean menuVisible;
int counter = 0;
public PhController() {
phBean = new PhBean();
}
public void readPeople() {
listStudent = new ArrayList<Stud>();
listStudent.add(new Stud(1, "John Miller"));
listStudent.add(new Stud(2, "Scott Jackson"));
selectStudents = new ArrayList();
option = new SelectItem(listStudent.get(0).getStudentSeq(), "Select");
selectStudents.add(option);
option = new SelectItem(listStudent.get(1).getStudentSeq(), "Select");
selectStudents.add(option);
phBean.setSsSeq(String.valueOf(1));
phBean.setSelectedName(listStudent.get(0).getStudentName());
menuVisible = true;
}
public void onChangePerson() {
phBean.setSelectedName(listStudent.get(1).getStudentName());
}
// Getters and Setters
}
PhBean.java:
public class PhBean implements Serializable {
private String ssSeq;
private String studName; // Used to display the name in the Menu bar.
private String selectedName;
public PhBean() {
}
// Getters and Setters
}
I'd say that in the p:ajax in defaultContent.xhtml the list of components to be updated should be separated with spaces only, no commas - so try changing this:
update=":defaultmenuform:menupanel, :defaultmenuform:stuname"
to this:
update=":defaultmenuform:menupanel :defaultmenuform:stuname"
UPDATE
I played with this a bit more and may have found a clue - please add the following code to defaultmenuform:
<p:messages autoUpdate="true" showDetail="true" />
This should help us tracking the reason for failed validation (in case failing validation is the root cause for you - as I said, I have rather limited possibility to reproduce this issue).
Anyway, when I selected some item in p:selectOneRadio, an error message like this appeared:
Conversion Error setting value 'test001.Student#4110c95c' for 'null Converter'.
And the root cause was on this row:
<p:selectOneRadio id="selstud" value="#{phController.phBean.ssSeq}" layout="custom">
p:selectOneRadio expects only String to be passed as a value - and ssSeq is very likely of a different type. Try to change the way value is populated to ensure it is always String - maybe a different attribute of the phBean or simply a brand new String one.
NOTE: if this doesn't help, maybe you could update your question with very simplified example of how phController and phBean could look like if we are to test it.
UPDATE #2
So you have explained there is a problem that you want to call phController.readPeople every time the page is loaded/refreshed, but instead it gets loaded with each and every Ajax request, thus overwriting the values.
In your PhController (it is a bean, right? session scoped?) you could add something like this (omitted null checks for the sake of readability):
public void readPeopleOnGet() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest req = (HttpServletRequest) ec.getRequest();
String reqMethod = req.getMethod();
if ("GET".equals(reqMethod)) {
readPeople();
}
}
With the above method you could keep this part of your defaultContext.xhtml in place, provided it is actually called (I assume so), just with the listener method changed:
<f:event listener="#{phController.readPeopleOnGet}" type="preRenderView">
</f:event>
The method readPeopleOnGet will still be called with every request to the page, but since Ajax requests are POST, it will only call readPeople when the page is loaded or refreshed as whole.
This may not be a "perfectly clean" solution, but seemed to work properly when I tested it.
UPDATE #3
Well, since you use PrimeFaces, it would be possible to identify Ajax call also this way:
public void readPeopleOnGet() {
RequestContext rc = RequestContext.getCurrentInstance();
if (!rc.isAjaxRequest()) {
readPeople();
}
}
But if I got your point from latest comments, you want to only run readPeople when the page is loaded for the very first time - so the following part could be even better for that.
You didn't answer if PhController is actually a bean, but I assume it is (there were no annotations visible from the code you posted). You may try making it #SessionScoped:
#ManagedBean
#SessionScoped
public class PhController extends BaseController implements Serializable {
// the rest of the PhController goes here...
Then you could add something like this to the PhController:
#PostConstruct
private void init() {
readPeople();
}
The annotation #PostConstruct ensures the method init is called exactly once after the bean was created. You can continue calling the method readPeople from other places as necessary, while removing both the <f:event ... /> and readPeopleOnGet as these will no longer be needed.

why action in <h:commandLink> doesn't work inside a composite component

I have a very simple JSF 2/Facelets page that looks like this:
<ui:repeat value="#{myBean.names}" var="_name">
<h:commandLink value="#{_name}" action="#{myBean.sayHello(_name)}">
<f:ajax execute="#this"/>
</h:commandLink>
<br/>
</ui:repeat>
The backing bean provides a java.util.List<String> with names and the action-method just prints a "hello <name>" message to standard output.
This works fine. I get a list of names in the browser and a click fires the action-method that says hello to the specified name.
The Problem arises, when I want to put this code in a composite component that does the iteration and renders the actual link via a facet:
<ui:component xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:cc="http://xmlns.jcp.org/jsf/composite">
<cc:interface>
<cc:attribute name="value" type="java.util.List" required="true" />
<cc:facet name="content" />
</cc:interface>
<cc:implementation>
<ui:repeat value="#{cc.attrs.value}" var="_name">
<cc:renderFacet name="content"/>
</ui:repeat>
</cc:implementation>
</ui:component>
I use the composite component like this:
<my:myComp value="#{bean.names}">
<f:facet name="content">
<h:commandLink value="#{_name}" action="#{bean.sayHello(_name)}">
<f:ajax execute="#this"/>
</h:commandLink>
<br/>
</f:facet>
</my:myComp>
In the browser I get a list of names that looks exactly like before. But clicking a link now renders a "hello null" message. So _name is resolved correctly in the value attribute of <h:commandLink> but not in the action attribute.
I also tried using actionListener instead of action or the listener attribute from the <f:ajax> tag with no difference.
Could anybody shade some light on this issue?
My environment:
WildFly 8.1 with
JSF 2.2.6 (Mojarra)
The issue has to do with the scope of the variable in this case _name which is evaluated once when the <ui:repeat/> is being processed. In my case, I ran your code and it produced Hello John even though their were other names in my list. To get around this, I introduced a <f:param/> that would contain the value of the _name, and modified your code as follows:
<h:form>
<my:myComp value="#{bean.names}">
<f:facet name="content">
<h:commandLink value="#{_name}" action="#{bean.sayHello()}">
<f:param name="name_" value="#{_name}"/>
<f:ajax execute="#this"/>
</h:commandLink>
<br/>
</f:facet>
</my:myComp>
</h:form>
I also modified the sayHello() method as follows for a #RequestScoped bean:
#ManagedProperty(value = "#{facesContext}")
private FacesContext facesContext;
public void setFacesContext(FacesContext facesContext) {
this.facesContext = facesContext;
}
public void sayHello() {
Map<String, String> params = facesContext.getExternalContext()
.getRequestParameterMap();
String name = params.get("name_");
System.out.println("Hello " + name);
}
You could change this to something shorter in a #ViewScoped bean to:
public void sayHello() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
String name = params.get("name_");
System.out.println("Hello " + name);
}
The final result is that it prints out the names correctly.

PrimeFaces commandButton doesn't navigate

I have this code :
Add.xhtml :
<h:form id="nouv">
<h:panelGrid columns="2">
<h:outputText value="Nom:"></h:outputText>
<p:inputText value="#{ddeBean.nom}"></p:inputText>
<p:commandButton id="save"
value="ajouter"
update=":nouv:btn"
actionListener="#{ddeBean.ajouter}">
</p:commandButton>
<p:outputPanel id="btn">
<h:outputText rendered="#{ddeBean.created}" value="#{ddeBean.message}"/>
<p:commandButton id="btn_cr" value="add" rendered="#{ddeBean.created}"
action="pool.xhtml?faces-redirect=true">
</p:commandButton>
</p:outputPanel>
</h:panelGrid>
</h:form>
ddeBean.java :
#ManagedBean(name = "ddeBean")
#RequestScoped
public class DemandeBean implements Serializable{
ddeDAO dao = new ddeDaoImpl();
private String nom;
public String message = "";
private boolean created = false;
public void test(ActionEvent event){
Demande p = new Demande();
p.setDde(this.nom);
dao.Nouveau_dde(p);
created = true;
this.setMessage("saved!");
}
}
When I click commandButton Ajouter the message an the commandbutton Add will be shown, but commandbutton Add doesn't redirect to pool.xhtml.
When you click on the button Add, the bean is recreated and the value #{ddeBean.created} then reinitialized to false so the button doesn't get rendered before action take place.
To fix that, you need to change the scope of your bean to #ViewScoped.
You should also make sure to replace
public void test(ActionEvent event){
by
public void ajouter(ActionEvent event){
if you want that your button work properly.
Another solution
Since you are using PrimeFaces, you could show your button by JavaScript :
<p:commandButton id="save"
value="ajouter"
oncomplete="panel-create.show();"
actionListener="#{ddeBean.ajouter}">
</p:commandButton>
<p:outputPanel id="btn" style="display: none;" widgetVar="panel-create">
<h:outputText value="#{ddeBean.message}"/>
<p:commandButton id="btn_cr" value="add"
action="pool.xhtml?faces-redirect=true">
</p:commandButton>
</p:outputPanel>
Note the oncomplete attribute on the p:commandButton, the widgetVar attribute on the p:panel and the rendered attributes removed.

dynamic adding radio and InputText in jsf

i am getting problem in editing in textBox which are generated dynamically.
see my code.
<h:form>
<h:panelGroup>
<h:panelGrid columns="2">
<h:panelGrid columns="1">
<h:selectOneRadio id="radio1" value="#{dynamicBean.radiovalue}" layout="pageDirection" >
<f:selectItems value="#{dynamicBean.objectList}" var="k1" itemValue="#{k1.value}" itemLabel="" />
</h:selectOneRadio>
</h:panelGrid>
<h:panelGrid columns="1" rowClasses="raw1">
<c:forEach items="#{dynamicBean.objectList}" var="k3">
<p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" >
</p:inputText>
</c:forEach>
</h:panelGrid>
<h:commandButton value="add new" action="#{dynamicBean.addNew}"/>
</h:panelGrid>
</h:panelGroup>
</h:form>
and this is my bean.
#ManagedBean
#ViewScoped
public class DynamicBean implements Serializable{
private String radiovalue;
private List<Pojo> objectList=new ArrayList<Pojo>();
int i=0;
private Pojo single=new Pojo();
public DynamicBean() {
System.out.println("In Cons");
if(objectList.isEmpty())
{
Pojo p1=new Pojo();
p1.setName("Name-"+i);
p1.setValue("Value-"+i);
p1.setTextvalue("Text-"+i);
objectList.add(p1);
i++;
setRadiovalue(p1.getValue());
}
}
public void addNew()
{
Pojo p1=new Pojo();
p1.setName("Name-"+i);
p1.setValue("Value-"+i);
p1.setTextvalue("Text-"+i);
objectList.add(p1);
i++;
setRadiovalue(p1.getValue());
}
public void ajaxEvent(ValueChangeEvent e)
{
System.out.println("New:"+e.getNewValue());
System.out.print("Old:"+e.getOldValue());
}
following are three variable in Pojo with getter and setter
private String name;
private String value;
private String textvalue;
biggest confusion is i can change first object value on in text box but i cant change the value of new generated objects.
Thanks.
Your concrete problem is caused because you're using JSTL <c:forEach> tag which runs during view build time while you're using a view scoped bean. View scoped beans are stored in the view state. When you submit the form to the server, the view state will be restored, but the original view scoped bean isn't available yet at that moment and thus a new one will be created (thus, with all properties set to default!). After restoring the view, the original view scoped bean will be put back in scope, overriding the temporary one.
You need a fullworthy JSF UI component instead of a JSTL tag. For this particular purpose, you need the <h:dataTable>.
Replace
<h:panelGrid columns="1" rowClasses="raw1">
<c:forEach items="#{dynamicBean.objectList}" var="k3">
<p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" >
</p:inputText>
</c:forEach>
</h:panelGrid>
by
<h:dataTable value="#{dynamicBean.objectList}" var="k3" rowClasses="raw1">
<h:column>
<p:inputText value="#{k3.textvalue}" valueChangeListener="#{dynamicBean.ajaxEvent}" />
</h:column>
</h:dataTable>
See also:
JSTL in JSF2 Facelets... makes sense?
Unrelated to the concrete problem, you've some code duplication there in the constructor. Just call addNew() method in there. I'd also remove the instance variable i as that makes no sense. Just use a local one instead which get initialized with List#size().
public DynamicBean() {
addNew();
}
public void addNew() {
Pojo p1 = new Pojo();
int i = objectList.size();
p1.setName("Name-" + i);
p1.setValue("Value-" + i);
p1.setTextvalue("Text-" + i);
objectList.add(p1);
setRadiovalue(p1.getValue());
}

JSF 2.0 redirect with parameters

I my Java EE application I have articles administration. There is url like http://localhost:8080/articles/detail.xhtml?article=70 where article means id of article. This page displays article comments etc. it is not important. But there is button Edit and I would like to redirect on page edit.xhtml but user should still see http://localhost:8080/articles/detail.xhtml?article=70 because I don't want the edit page to be bookmarkable.
Can you help me how to set up faces-config to change page but not url? I thought that if I don't write <redirect /> then url would stay same but I was wrong. Url changes from detail.xhtml?article=70 to detail.xhtml
Thanks for any advise.
I'd suggest to bring in some ajaxical powers so that no synchronous request is fired.
<h:panelGroup id="article" layout="block">
<h:panelGroup rendered="#{!bean.editmode}">
View article (can if necessary be an ui:include)
<h:form>
<h:commandButton value="Edit" action="#{bean.edit}">
<f:ajax render=":article" />
</h:commandButton>
</h:form>
</h:panelGroup>
<h:panelGroup rendered="#{bean.editmode}">
Edit article (can if necessary be an ui:include)
<h:form>
<h:commandButton value="Save" action="#{bean.save}">
<f:ajax render=":article" />
</h:commandButton>
</h:form>
</h:panelGroup>
</h:panelGroup>
Bean:
private boolean editmode;
public void edit() {
this.editmode = true;
}
public void save() {
this.editmode = false;
}
public boolean isEditmode() {
return editmode;
}

Resources