Question

I have a controller where the user fills in information into a form. On saving the form, JPA objects are created and persisted to a database and the user is redirected to another controller to view the form. I am not interested in passing the whole objects between the controllers, just Strings that represent the objects. I could reload the objects from the database, but due to slow load times, is there any way I can pass data without accessing the DB?

Edit: When one controller redirects to another, I am interested in "sending" strings from the first controller to the redirected controller so that those strings can be displayed.

Thank you for the help!

Edit 2:

Here's the code in the first controller:

//Called by a commandButton
public String saveForm() {
    //Objects are created and persisted to DB
    //Store the Objects' names as strings. These are the values I want to access in the second controller
    return "view_form.jsf?formId=" + formId.toString() + "&faces-redirect=true";
}

In the second controller called viewForm, I'm loading the formId with viewParam:

<f:metadata>
  <f:viewParam name="formId" value="#{secondController.formId}" required="true"/>   
  <f:event type="preRenderView" listener="#{secondController.loadForm}"/>
</f:metadata>

In loadForm I would like to access the Strings created in saveForm. I'm currently looking at the link Rodmar Conde posted about storing the data in the Session Map. Is this the correct way to go about this?

Thank you for your help so far!

Was it helpful?

Solution

The code looks fine. This is also the recommended approach for the case the target page needs to be opened by an idempotent request. See further also How to navigate in JSF? How to make URL reflect current page (and not previous one) for some general hints.

Your real problem is apparently just the DB performance (although a single select on a properly indexed table should be relatively cheap, sure that it isn't just the connection cost (use connection pool) or the network latency (host DB server near to web server)). Introducing kind of 2nd level cache in persistence layer may help a lot. Refer the documentation of persistence layer in question using the keywords "second level cache" or "L2 cache".

Note that this is beyond the scope of JSF. Abusing the JSF session scope is another way, but not the right one. Sharing data between two view scoped bean instances separated by a GET request is not possible as their lifecycle ends and starts with a GET request. See also JSF ViewScoped variable not surviving redirect to same page.

OTHER TIPS

Here is what i settled for. Save the values in the session as below in the first ViewScoped controller:

ExternalContext etx = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest httpReq = (HttpServletRequest)etx.getRequest();
    HttpSession session = httpReq.getSession();
    session.setAttribute("formId", "formId_value_here"); //use a map if you have multiple values

And retrieve the value in the PreRenderView of the second ViewScoped controller (or anywhere) as below:

ExternalContext etx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest httpReq = (HttpServletRequest)etx.getRequest();
HttpSession session = httpReq.getSession();
String formId = (String)session.getAttribute("formId");

Hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top