How to save JSF textbox contents after page refresh? - jsf

What is the easiest way to save text that users type into the following JSF HTML textbox, so that the text stays in the textbox even after the page is refreshed (e.g. for when the user may navigate away from the page and then return, wishing to see what they had put into the textbox earlier)?
<s:decorate template="layout/display.xhtml">
<ui:define name="label">Name</ui:define>
<h:inputText id="name" value="#{countriesList.countries.id.name}"/>
</s:decorate>
Thanks in advance for your reply.
To give further explanation of the above code: I created a sample JBoss Seam Web Project, reverse-engineering code from an Oracle 10g database that had a table called "Countries" in it. The only problem that I have now is the fact that, whenever users leave the records list page to view or edit records, they lose all of their search criteria in the page that lists results.
Surely there is a simple setting in the web project that I can change to make this happen? Or am I being too optimistic?

Bind the value of the inputText to a component with session scope.
#Name("valueHolder")
#Scope(ScopeType.SESSION)
public class ValueHolder
{
private String value;
...set/get
}
And in the xhtml:
<h:inputText value="#{valueHOlder.value}"/>

Related

Creating command links dynamically from a managed bean

I have a need to create command links dynamically based on content coming from elsewhere.
When a user clicks on a link it should call a method in a managed bean, and the method needs to know which link was clicked.
I can create the command links using the following code:
JSF:
<h:outputText value="#{myBean.dynamicLinks}" escape="false" />
Bean:
public String getDynamicLinks(){
// Return an html string that contains a set of <a> elements, based on the dynamic content
}
This works fine, but what I can't work out is how my <a> elements can call back into the bean.
This is not the right way to "dynamically" create markup. For that you should be using XHTML, and absolutely not Java and for sure not massage some plain HTML in the model and present it with escape="false". That's plain nonsense. You're basically mingling the view into the model. You need to make the model itself dynamic, not the view. The view must be static and dumb. The view must just present the model to the world. The model itself can be dynamic. You normally achieve that by using a flexible collection, such as List<String>, List<SomeEntity>, etc which you then present using an iterator in the view such as <ui:repeat> or <h:dataTable>.
E.g.
<ui:repeat value="#{bean.links}" var="link">
<h:commandLink value="link" action="#{bean.action(link)}" />
</ui:repeat>
public void action(Link link) {
// ...
}
You see, the action method can know about the pressed link by just inspecting the method argument.
See also:
How to create dynamic JSF form fields
How can I pass selected row to commandLink inside dataTable?

Navigate to previous page with JSF

I use a backing bean where I can save a form and navigate to this page.
public String saveHere() {
return Faces.getViewId() + "?faces-redirect=true&includeViewParams=true";
}
How do I navigate to the previous page with OmniFaces?
My save action should result in the page where I have the "edit" button.
Typically a list-view (with edit-button next to very item)
or another page with (with edit-button to this item)
Thanks!
You need the page id you're navigating from somewhere. At first look, keeping it in Session seems to be a good idea, however, this could remain in coflict if you're navigating in multi-tab (sharing same Http Session through different browser tabs).
Having said that, the most proper solution for your case is to pass a view param to your edition view telling where you're coming from. That should be as easy as this:
<h:button outcome="edit" value="Edit registry">
<f:param name="pageFrom" value="#{view.viewId}" />
</h:button>
After that, in your edition view bind this param to your bean:
<f:metadata>
<f:viewParam name="pageFrom" value="#{editBean.pageFrom}" />
</f:metadata>
And just redirect to that view in your EditBean.java after saving, supposing it is #ViewScoped:
public String saveHere() {
//save here
return pageFrom + "?faces-redirect=true&includeViewParams=true";
}
That's the most straight forward way to achieve what you ask for. However, you must take into account the page where you're coming from remains to be a view parameter, so the end user could type what he wants there. With this behaviour, he could achieve being redirected to some other page rather than the one expected. If you want to avoid that, go with the flash scope instead of view parameters.
See also:
Get current page programmatically

i have two text box in two different pages a

i have two text box in two different pages and same variable use in two text box when enter value in first text box it's reflection show in second text box , how i reduce it in jsf ?
You can create a #SessionScoped bean named GeneralBean
Set a variable there e.g.:
private String name;
add getters and setters.
In the pages where the user enters his data you should have
<h:form>
<h:inputText value="#{generalBean.getName}">
<f:ajax event="change" />
</h:inputText>
</h:form>
Using Ajax, whenever he changes the value it will affect the name variable which appears also in the other screen.
EDITED:
I presumed that you are using JSF2.0. If not, please indicate it.
EDITED:
Since you are using JSF1.1 you will need external tool support.
You can use RichFaces a4j:support.
For that you need to download richfaces.
Use a4j:support. See an example here:
a4j:support
EDITED:
This answer is if the pages are not opened simultaneously.If they are opened at the same time you will have to use richfaces push optionin the other one.

jsf, richfaces, popup window

I would like to make a list-detail view with richfaces. There will be a link for every record in the list that should open a new window containing record details.
I tried to implement the link this way:
<a4j:commandLink oncomplete="window.open('/pages/serviceDetail.jsf','popupWindow', 'dependent=yes, menubar=no, toolbar=no, height=500, width=400')" actionListener="#{monitoringBean.recordDetail}" value="details" />
I use <a4j:keepAlive beanName="monitoringBean" ajaxOnly="false" /> for both the list and the detail page. recordDetail method fills the data of the selected record to a variable of the bean that I would like to display on the detail page.
The problem is that keepalive doesn't work, so I get new bean instance on the detail page every time. So the the previously selected record from the other bean is not accessible here.
Is there a way to pass parameter (id) to the detail page to handle record selection. Or is there any way to make keepalive work? (I this this would be the easiest).
Thanks
Avoid using window.open(..) - it will fail on most browser configurations nowadays (due to pop-up blocking).
Use <rich:modalPanel> instead.

Problem With JSF 1.1 and PopUp

I am trying to popup a window when someone clicks a button on the data table.
<h:commandButton
action="#{cacheController.popupDetails}"
immediate="false"
onclick="popup()"
value="View Details"
styleClass="submit">
</h:commandButton>
The associated popup function is
function popup() {
window.open('RDDetails.jsf','popupWindow', 'dependent=yes, menubar=no, toolbar=no, height=500, width=400');
}
Now in the new 'RDDetails.jsf" file, I am trying to access the same managedBean cacheController. But the problem is, the pop-up window and JSF lifecycle is not in sync. As a result, the popup first displays blank and when I refresh, it pulls out the proper data.
Is there anyway I can click on a button which will do some processing in the managed bean and then opens a pop up which rerieves the processed data from the managed bean.
I am using JSF 1.1.
You're here basically firing two independent requests: one associated with the form submit and other which opens the RDDetails.jsf in a popup. You'll need to combine this in one request. You can achieve this in basically two ways:
Get rid of the onclick and just add target="_blank" to the <h:form> so that it get submitted into a new window/tab.
Block the default action by adding return false; to the onclick and do the business logic in the constructor of the bean associated with RDDetails.jsf. The only (major) caveat is here that the model won't be updated with the form fields. Thus, you'll need to pass the form fields as request parameters of the popup URL manually with help of JavaScript. You can then make use of managed property entries in the faces-config.xml to inject the GET request parameters into the model.
First way is obviously the easiest, but this doesn't give you a "fullworthy" popup/modal dialog. The second way is a bit harder (unless you've already a good grasp on both JavaScript and JSF). I would then consider to look for a component library which provides a ready-to-use popup component.
See my example:
<h:commandLink action="#{controller.myAction}" onmousedown="document.forms['idform'].target='_blank';">
I'm using jsf 1.1

Resources