Tiny
Tiny

Reputation: 27899

Preserving query-string parameters using request scoped beans

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.

Upvotes: 0

Views: 74

Answers (0)

Related Questions