0

In my JSF application some values are set as requestScope and some are set in sessionScope. For example, if you are making a booking, the DTO is saved in sessionScope, so that it will be available after successful submission of booking in the next page and afterwards. So the user can save the booking as a template in the next page, and use the template in future. After saving the template, users will be redirected to home page. These booking values are saved in sessionscope but some other values that are needed only in succssfull submission page set in requestScope. So requestScope did the work required when loading this page. Then I faced a problem, that if the user use a name that is already available to save as template, the page should return to the same page with the error message after checking in the database in backing bean, instead of forwarding to the homepage. Since the booking values is saved in sessionScope, it is not a problem. But other values that are saved in requestScope get lost and I get nullPointerException in jsp page. Is there any way that I can achieve my task without using sessionScope for the other values. One of the ways I thought was that using postrender function in jsp to set these requestscope values again in the backing bean. But I am not sure if the request values will be available for postrender. Can someone please help me on this?

techtabu
  • 12,803
  • 2
  • 21
  • 27
  • I am not sure if I fully understand what you are trying to accomplish here, but would `@ViewScoped` serve you here? http://balusc.blogspot.nl/2010/06/benefits-and-pitfalls-of-viewscoped.html – Menno Oct 19 '12 at 20:11
  • Secondly, I assume you are using JSF 1.2, as you are using JSP pages? – Menno Oct 19 '12 at 20:16

2 Answers2

0

Even though this information is missing in the question, I'll assume that you're still on legacy JSF 1.x and can't migrate to JSF 2.x which offers the new view scope, otherwise you wouldn't have asked this question. The presence of the IBM tag also hints something about some legacy monster.

Get Tomahawk for JSF 1.2 and reference your request scoped bean by a <t:saveState> anywhere in the view.

<t:saveState value="#{managedBeanName}" />

That's all. This does under the covers exactly the same as the new JSF 2.0 view scope is doing. So this way your #{managedBeanName} instance becomes effectively a view scoped bean.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
0

If you're in JSF 2, you can use the @ViewScoped annotation to make your bean being alive while the user is in the same view.

If you're in JSF 1.2, you must use a third party library that handles this for you. I only know about two choices:

  • From Tomahawk library, you can use the <t:stavestate> component.

  • From RichFaces 3.3.x, you can use <a4j:keepAlive> component or as an annotation in your classes. I've worked with this one and it's of great help. I'll let you an example.

The managed bean with RequestScope configuration in the faces-config.xml file.

@KeepAlive
public class Bean {
    private String name;
    //constructor...
    //getters and setters...
}

Your JSP/Facelets page:

<h:form>
    <h:outputText value="Write your name" />
    <h:inputText value="#{bean.name}" />
    <br />
    <a4j:commandButton value="Say hello!" reRender="hello" />
</h:form>
<!-- Since this is outside the form, the #{bean.name} value won't be send to the server -->
<h:outputText value="#{'Hello ' + bean.name}" id="hello" />
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
  • @BalusC Yep, you guys are right, we are using JSF 1.2 and legacy IBM jsf. I have no option, but to use it, since I don't make any decision. It's not just beans, but some other values are also put into requestScope like getRequestScope().put("showActualStatus", "true") I need these values available after clicking some command in the forwarded page. I don't need to scope of the bean, only the values passed to the forwarding page. I decided to put them in session scope and used those values. – techtabu Oct 22 '12 at 17:24