How to call an action method of a UICommand Component which was rendered conditionally? - 2
My previous for the same problem , Now, I am in a same situation and looking for some solutions / suggestions.
My Restrictions are
- My Bean has to be request scoped
- I cannot use tomahawk - we use a customized app server built on tomcat + SOA - blah , blah so we cannot use.
I have a collection of search results which is different for each search criteria (and thats why the bean is request scoped - because the user would want to use diff tabs in the same browser to compare results or do whatsoever he wants ). I like to show the search results in pages like prev + next.
like this :
<tr>
<td colspan="14" class="WhiteRowSmallText">
<div align="right">
<h:commandLink rendered="#{adminBean.showPrev}" action="#{adminBean.goPrev}">
<h:outputText> < Prev  | </h:outputText>
</h:commandLink>
<h:outputText>  |  </h:outputText>
<h:commandLink rendered="#{adminBean.showNext}" action="#{adminBean.goNext}">
<h:outputText> Next ></h:outputText>
</h:commandLink>  
</div>
</td>
</tr>
Instead of hitting Database for every 'Next' click, I wanted to store them in session, like
pub开发者_Go百科lic static Object getSessionMapValue(String key) {
Log.debug(CLASS_NAME + "getSessionMapValue: key=" +key );
return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(key);
}
public static void setSessionMapValue(String key, Object value) {
Log.debug(CLASS_NAME + "setSessionMapValue: key=" +key );
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(key, value);
}
My Questions are :
If I use this approach to store a huge number of search results , will it still be available if the user opens up another tab ? I think so - is not it ?
When the results were more, i tried 'Next' , it did not even call its action method because a new Bean was created (!) and made that showNext as 'false' - What are my options to allow the user to browse thru pages considering my restrictions above ?
Is it okay to use a hidden Bean variable and make it 'true' through javascript when the link was clicked and in the CustomPhaseListener - and in the RESTORE VIEW PHASE - make the showNext as 'true' based on this hidden value? is that a worth approach?
All scoldings and suggestions are welcome.
If you use inputHidden,you may get ClassCast Exception , I am trying like this,not sure yet, if this works
public Boolean getShowNextValue() { if(showNext != null && showNext.getValue()!= null) { log.debug("showNext.getValue() ]" +showNext.getValue()); if(((String)showNext.getValue().toString()).equals("false")) { return false; }else{ return true; } }else{ return false; } }
If I use this approach to store a huge number of search results , will it still be available if the user opens up another tab ? I think so - is not it ?
I'd just make the bean session scoped. Alternatively, you can also split all "to-be-session-scoped" properties out of the request scoped bean and create a new session scoped bean with those properties. You can then make use of the JSF managed-property facility to inject the session scoped one (for data) in the request scoped one (for actions) whenever needed. Also see this example.
You however need to keep in mind that this might eat more memory when the webapp is more busy and concurrently visited.
When the results were more, i tried 'Next' , it did not even call its action method because a new Bean was created (!) and made that showNext as 'false' - What are my options to allow the user to browse thru pages considering my restrictions above ?
That's because the bean is request scoped and the properties responsible for the condition of the rendered
attribtue did not have the same outcome during apply request values phase of the subsequent request as it was during the render response phase of the initial request. Solutions to that are answered in your previous question (as you linked yourself).
Is it okay to use a hidden Bean variable and make it 'true' through javascript when the link was clicked and in the CustomPhaseListener - and in the RESTORE VIEW PHASE - make the showNext as 'true' based on this hidden value? is that a worth approach?
If Tomahawk is really not an option, I'd go for a <h:inputHidden>
with binding
.
E.g.
<h:inputHidden binding="#{adminBean.showPrev}" />
<h:commandLink rendered="#{adminBean.showPrevValue}">
...
with
private HtmlInputHidden showPrev = new HtmlInputHidden(); // +getter +setter.
and
public void setShowPrevValue(Boolean showPrev) {
showPrev.setValue(showPrev);
}
public Boolean getShowPrevValue() {
return (Boolean) showPrev.getValue();
}
Use the last getter/setter instead when you want to get/set the "real" boolean value in your bean.
精彩评论