0

I have some readonly beans which do not require maintaining their state on postbacks. These beans can thus be request scoped such as,

@Named
@RequestScoped
public class Bean extends LazyDataModel<Entity> {

    @Inject
    private Service service;
    private Entity entity; // Getter & setter.

    public Bean() {}

    @Override
    public List<Entity> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        setRowCount(service.rowCount().intValue());
        return service.getList(first, pageSize);
    }
}

Entity is supplied as a query-string parameter which is defined on the template client as follows.

<ui:define name="metaData">
    <f:metadata>
        <o:viewParam name="id" value="#{bean.entity}"/>
    </f:metadata>
</ui:define>

The parameter id being passed is converted by a custom implicit JSF converter marked by

@FacesConverter(forClass = Entity.class)

This bean however, needs to be turned into a view scoped bean only for one reason. There are two global <p:selectOneMenu>s on the master page template representing a list of languages and a list of selected PrimeFaces themes.

When either a language or a theme is changed after arriving at the page backed by the above request scoped managed bean, query-string parameter id will get lost as obvious unless this bean is designated as a view scoped bean at least.

Changing a language or a theme using a <p:selectOneMenu> is only occasionally necessary. Having a view scoped bean only for this functionality should be excluded somehow.

Is there a way to keep this bean a request scoped bean somehow while preserving the query-string parameter, when either a language or a theme is occasionally changed in their respective <p:selectOneMenu>s?


Additional :

Those two <p:selectOneMenu> are backed by two respective session scoped beans. Request to change either a language or a theme is triggered by a <p:remoteCommand>.

The request at the end, is redirected after a language or a theme whichever happens has been set to the current session as follows.

public String themeAction() {
    themeBean.setTheme(Theme.valueOf(theme)); // Theme is an enum. theme is a request parameter which is supplied by a <p:remoteCommand>.
    return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true&includeViewParams=true";
}

The current bean is a request scoped bean and the ThemeBean is a session scoped bean which has been injected into the current request scoped bean.

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Is this acceptable as dupe? http://stackoverflow.com/q/14735726 – BalusC Dec 30 '15 at 20:25
  • That works but it happens with `` only. Why doesn't it happen with ``? – Tiny Dec 30 '15 at 20:56
  • That's unexpected. I will check it. You can in your specific case also just use ``. – BalusC Dec 30 '15 at 21:01
  • `` works with `` but has no effect, when `` is used as happens with ``. – Tiny Dec 30 '15 at 21:10
  • I forgot myself that `` is stateless and doesn't run on postbacks and should be used with view scoped beans only. Perhaps I should clarify that more in its javadoc. Is there a technical reason you're using it instead of ``? – BalusC Dec 31 '15 at 15:05
  • The reason for using `` is mentioned [here](http://stackoverflow.com/a/23906643/1391249) but this can be avoided in this specific case. Thus, I can safely go with `` in this case, since the bean is readonly and no Ajaxical postbacks to this bean are made. – Tiny Dec 31 '15 at 15:15
  • Yes, exactly. `` should be all fine in this specific case with a request scoped bean. – BalusC Dec 31 '15 at 15:16
  • What is the difference between `includeRequestParams` and `includeViewParams` in ``? Can't understand just by reading the documentation, "*Whether to include all URL (GET) request parameters in the action URI. This overrides `includeViewParams`.*" – Tiny Jan 31 '16 at 06:23
  • `includeQueryString` was after all perhaps more clear. All of those GET request parameters which you see in browser's address bar thus. – BalusC Jan 31 '16 at 10:02

0 Answers0