来自其他bean的JSF Bean属性

时间:2012-08-07 14:37:39

标签: jsf static set javabeans

我尝试将一个ManagedBean(SessionScoped)(loginBean)的属性带到另一个(RequestScoped)ManagedBean(newsBean):

<c:set value="#{loginBean.user.nickname}" target="#{newsBean}" property="author" />
            <h3>News verfassen:</h3>
            <h:form>
                <p:inputText label="Titel" value="#{newsBean.title}"></p:inputText>
                <p:inputTextarea label="Inhalt" value="#{newsBean.description}"></p:inputTextarea>
                <p:commandLink type="button" value="Absenden" action="#{newsBean.writeMsg}"></p:commandLink>
            </h:form>

我认为我可以使用c:set标签,就像我想要设置一个属性“静态”.. :)

也许有人可以帮助我?

Domii

-----回答之后------

LoginBean:

@ManagedBean
@SessionScoped
public class LoginBean {
public LoginBean() {
    email = "";
    pwd = "";
    device = "";
    user = null;
}

private String email;
private String pwd;
private User user;
private String device;
/**
 * @return the user
 */
public User getUser() {
    return user;
}

/**
 * @param user the user to set
 */
public void setUser(User user) {
    this.user = user;
}

和NewsBean:

public class NewsBean {

@ManagedProperty("#{loginBean.user.nickname}")
private String nickname;

private String title;
private String description;

/**
 * Creates a new instance of NewsBean
 */
public NewsBean() {
}

是的,我还在用户中声明了昵称的getter / setter!

1 个答案:

答案 0 :(得分:2)

只需使用@ManagedProperty

@ManagedBean
@ViewScoped
public class NewsBean {

    @ManagedProperty("#{loginBean.user.nickname}")
    private String author;

    // ...
}