简体   繁体   中英

JSF - Set multiple values on @ManagedProperty in a single bean

I need to set 2 different ManagedProperty on the same bean. So i tried :

@ManagedBean(name="selector")
@RequestScoped
public class Selector {
    @ManagedProperty(value="#{param.page}")
    @ManagedProperty(value="#{param.profile_page}")
    private String page;
    private String profile_page;

    public String getProfile_page() { 
        if(profile_page==null || profile_page.trim().isEmpty()) {
            this.profile_page="main";
        }
        return profile_page;
    }
    public void setProfile_page(String profile_page) { this.profile_page = profile_page; }

    public String getPage() {
        if(page==null || page.trim().isEmpty()) {
            this.page="homepage";
        }
        return page;
    }
    public void setPage(String page) { this.page=page; }
}

but unfortunatly i can't write 2 different @ManagedProperty : it says duplicate annotations. How can I fix it?

Another : when i return this value, its a String, and i need to confrontate. This syntax :

<h:panelGroup rendered="#{selector.profile_page.compareTo("main")}">
    <ui:include src="/profile/profile_main.xhtml" />
</h:panelGroup>

will work?

Cheers

The annotations have to be declared directly before the class, method or field of interest.

So:

@ManagedProperty(value="#{param.page}")
private String page;

@ManagedProperty(value="#{param.profile_page}")
private String profile_page;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM