I am working on a JSF application.
I have recently faced a problem, that how to hide any attribute at the start-up of the application.
For Example -
I have a h:panelGrid attribute for displaying table, but I want to show this table only when a checkbox is clicked, Here I am able to made it working for show/hide but only from second time onwards.
What I want is to hide this table using h:panelGrid when application loads this view, the later part as I told I have achieved.
It would be grateful if somebody cite it for general hide at start-up.
Thanks.
Create a boolean attribute in the ManagedBean which this page uses and set it to false (for example in PostConstruct method). Then use it as a rendered attribute of your <h:panelGrid> and it's going to be defaultly hidden.
let's say
#ManagedBean
#RequestScoped
public class Bean {
private boolean visible = false;
//setters and getters
}
//later on page
<h:panelGrid rendered="#{bean.visible} />
I got it...somehow like this
in panelGrid set style="visibility:hidden"
and in checkBox, call javascript function and in there set
if (show)
{
obj.style.display = "block";
obj.style.visibility="visible";
}
else
{
obj.style.display = "none";
}
It does the trick.
Thanks Petr for help.
Related
I have a #ViewScoped-annotated managedbean whose #PostContruct-method fetches a list from database to be displayed in a table in the view.
Now when I delete an item I want the changes to be seen in the view.
To keep this dynamic and reusable I only want to delete from database (not manually from list). So I need to destroy/recreate the bean I suppose. Now I do this by navigating to the same view. But the way I do is not reusable.
Can I just destroy the bean manually or navigate to the same view without explicitly navigating to THAT specific view (reusability)?
I am using JSF 2.1
You're already on the right track. viewMap is just like any other map; You can remove a ViewScoped bean by name. Please excuse the atrocious chaining:
FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("yourBean");
One solution I found is to destroy the bean by
FacesContext.getCurrentInstance().getViewRoot().getViewMap().clear();
I am not sure if this is the way to go because it simply destroys every viewscoped bean. It's not that bad in this case, but doesn't feel clean.
I appreciate any thought on that or alternative solutions.
When you return a non null value inside a method from an action attribute the Bean gets recreated.
index.xhtml
...
<h:commandButton value="delete" action="bean.delete" />
...
Bean.class
...
public String delete() {
// do operations
return "index.xhtml?faces-redirect=true";
}
...
Trying to navigate from page 1 to page 3 as well as page 2 to page3.In page3 when I click on back button it should navigate to the appropriate page.Could anyone please help me how to achieve this?
You can achieve this by having a bean which has a field storing the previous page. Then in the third page you can use the value of this field to return to the appropriate page. For example in your first page you need to have a commandLink navigating you to third page, while setting the previousPage as firstPage. Similar commandLink should be part of second page too. You need to enclose h:commandLinks in h:forms as they use post method to pass parameters.
<h:commandLink value="Third Page" action="#{navigationPageBean.updateLastVisitedPage("/firstPage.xhtml")}"/>
<h:commandLink value="Third Page" action="#{navigationPageBean.updateLastVisitedPage("/secondPage.xhtml")}"/>
#Named
public class NavigationPageBean implements Serializable
{
private String previousPage;
public String updateLastVisitedPage(String pageName)
{
previousPage = pageName;
return "/thirdpage.xhtml";
}
public String getPreviousPage()
{
return previousPage;
}
public void setPreviousPage(String previousPage)
{
this.previousPage = previousPage;
}
}
You can achieve this by using javascript methods to learn the previous page too I think, but I don't know the exact methods.
You have to implement url based navigation if you want to achieve that. To navigate from 1 to 3 you can implement an <h:button> with an outcome attribute.
<h:button value="Go to third" outcome="/thirdpage.xhtml" />
You can also do it specifying navigation rules. However you have dozens of tutorials to do this.
http://www.mkyong.com/jsf2/jsf-2-button-and-commandbutton-example/
http://www.mkyong.com/jsf2/implicit-navigation-in-jsf-2-0/
JSF 2.0 implicit navigation, different views
In order to know where are you coming from there are different ways, but I think the right one is to use view params. In a button or link you can specify an <f:param name="ComingFrom" value="#{sourceBean.id}" /> and you receive it in the destination page that way <f:viewParam id="ComingFrom" name="ComingFrom"
value="#{destinationBean._ParamSourceId}" />.
That way you are passing parameters through the request and you can build your back button to point an url or the other one.
There you have more stuff.
https://blogs.oracle.com/rlubke/entry/jsf_2_0_bookmarability_view
I have this scenario: in the first tab of a primefaces wizard component, I have some fields and a button. The button saves some data, does some business logic and, with the results, sets some properties of the form bean (which is in ViewScope) that are not related to a specific field of the form. I have checked that in the invoke application phase, the values are set properly.
In the second tab I have another button that has to do some business logic using the values set by the first one but, doing some debug, I noticed that the values, even if not related to any field of the form, are overwritten I think during the update model phase invoked when I click the second button.
How can I avoid this? Is there a way to obtain the correct behaviour?
I looked around but I couldn't find any example of a wizard form with multiple submissions. Thanks for help!
<p:wizard widgetVar="wiz" render="true" id="wizard" showNavBar="false">
<p:tab id="step0" title="Step0" step="0">
<!-- Some other fields-->
<p:commandButton value="Save and do some business logic"
action="#{formBean.save}"
oncomplete="wiz.loadStep (wiz.cfg.steps [1], false)">
</p:commandButton>
</p:tab>
<p:tab id="step1" title="Step1" step="1">
<!-- Some other fields-->
<p:commandButton value="Second button: use the previous informations"
action="#{formBean.doSomething}"
oncomplete="wiz.loadStep (wiz.cfg.steps [2], false)">
</p:commandButton>
</p:tab>
</p:wizard>
Edit:
To show an example I can say that my bean contains a business logic object. During the first submission, this object is being saved so the database (Mysql and Hibernate), assigns to it a progressive id.
During the second submission, when I try to read this id, the value is zero so, obviously, I get an error.
Something like:
public class FormBean{
private BLObject object;
// Constructor Getters and setters
// Method executed during the first submission
public void save(ActionEvent actionEvent) {
//Save the object and set his id
PersistanceClass.getInstance().save(object);
}
// Method executed during the second submission
public void doSomethingWhitTheId(ActionEvent actionEvent) {
//Access the id... id=0 returned
int id = object.getId();
}
}
The problem was that, navigating from one page to another, Primefaces wizard, uses a String to identify the tab. Returning the value of this String, which is not null nor void, makes the view bean go out of scope. It is ok when you have a simple form but, if you wanna set some properties not related to a form element during the flow, they get overwritten during the tab changes.
I solved the problem adding to the form an hidden parameter linked to the property I wanted to preserve.
<h:inputHidden value="formBean.idToPreserve" id="inputHidden" />
I need help. I'm new to JSF and im using JSF2 and richfaces in my project.
I want to clear the form for which I'm using <f:ajax render="#form"/> in refresh button. I have an ADD button on that screen which adds one record and I hit refresh then it's going to the default page. But when I once again go to enter a record then those values which I entered earlier remain in the form fields.
Can anyone please help me out with this issue?
Assuming that you mean the browser's refresh button when you say "I hit refresh", then that can happen if you've incorrectly placed the bean holding view scoped data in the session scope. You're then basically reusing the very same bean as the form is previously been submitted to. Putting the bean in the view scope instead of the session scope should fix this problem. The view scope ends when you navigate to a different page or fires a new request (as by hitting browser's refresh button).
See also:
How to choose the right bean scope?
Update if you're due to bad design restricted to using session scope, then you might want to hack this around by a
<f:event type="preRenderView" listener="#{sessionScopedBeanWhichShouldActuallyBeViewScoped.resetModel}" />
with
public void resetModel() {
if (!FacesContext.getCurrentInstance().isPostback()) {
model = null;
}
}
This will clear the model on every GET request. However, regardless of this hack, you'll still run into serious problems when the enduser opens the same view in a different browser tab/window within the same session.
The right solution is to put the bean in the view scope instead of the session scope, as said earlier.
I've created a composite component that has a commandLink embedded inside of a ui:repeat. I need to be able to dynamically change the method that is called via the commandLink's action property but it doesn't seem to be possible due to the fact that you need to specify the ID of the commandLink in the
Since the commandLinks are in a UI:repeat, they all have a dynamic ID.
As a workaround, I'm trying to use setPropertyActionListener on the command link. However, it doesn't look like the method is ever being called. Am I missing something? Is this the wrong way to go about what I want?
Here is some sample code.
Composite Component:
<ui:repeat value="#{cc.attributes.value}" var="aUser">
<li class="ui-widget-content ui-state-default q-userListResult">
<p:commandLink
styleClass="q-userList-resultLink"
update=":userList:q-userList-formUsers:userToolTip">
<f:setPropertyActionListener value="{aUser}" target="#{cc.attributes.resultLinkActionListener}"/>
Using Page:
<q:userList id="userList"
value="#{caseWizardBackingBean.companyContacts}"
renderHeader="false"
resultLinkActionListener="#{caseWizardBackingBean.selectedCompanyContact}"/>
Bean:
private CTProfile selectedCompanyContact;
public CTProfile getSelectedCompanyContact() { return this.selectedCompanyContact; }
public void setSelectedCompanyContact(CallTrackProfile ctp) { this.selectedCompanyContact = ctp; }
I tried adding some debug statements and breakpoints into the property's getter and setter but they are never hit. I'm guessing something odd is happening because all of the examples I can find show that this should work (but they don't use a composite component).
I should point out, I'm using the Primefaces commandLink but this seems to happen with the regular commandLink too.
OK, from what I can tell this can't be done in the current version of JSF (2.1.3).
In order to work around the issue I made the component a single commandLink and then the using page has a ui:repeat with the commandLink inside of it.