托管bean永远不会被实例化

时间:2016-05-15 18:00:42

标签: jsf managed-bean

在我的JSF中,我有一个dataTable。每一行(显示用户)都有一个表单,该表单将其ID作为唯一参数。问题是永远不会调用关联的backing bean(方法跟随/取消关注,也不是构造函数和PostConstruct注释方法)。

searchUserResult.xhtml

<h:dataTable value="#{searchUserMB.searchResult}" var="user"
            rowClasses="tdUser"
            rendered="#{not empty searchUserMB.searchResult}">
            <h:column>
                <f:facet name="header">
                    <h:outputText value="#{bundle.Username}" />
                </f:facet>
                <h:outputText value="#{user.username}" />
            </h:column>
            <h:column>
                <f:facet name="header">
                    <h:outputText value="#{bundle.Name}" />
                </f:facet>
                <h:outputText value="#{user.name}" />
            </h:column>
            <h:column>
                <f:facet name="header">
                    <h:outputText value="#{bundle.Surname}" />
                </f:facet>
                <h:outputText value="#{user.surname}" />
            </h:column>
            <h:column>
                <f:facet name="header">
                    <h:outputText value="#{bundle.Action}" />
                </f:facet>
                <h:form rendered="#{userMB.user.isFollower(user.id)}">
                    <h:commandButton value="#{bundle.Unfollow}"
                        action="#{followMB.unfollow}">
                        <f:param name="followerId" value="#{user.id}" />
                    </h:commandButton>
                </h:form>
                <h:form rendered="#{not userMB.user.isFollower(user.id)}">
                    <h:commandButton value="#{bundle.Follow}"
                        action="#{followMB.follow}">
                        <f:param name="followerId" value="#{user.id}" />
                    </h:commandButton>
                </h:form>
            </h:column>
        </h:dataTable>

FollowMB

@ManagedBean
@RequestScoped

public class FollowMB {

@EJB
private DBObjectRemote db;

@ManagedProperty(value="#{userMB}")
private UserMB userMB;

@ManagedProperty(value="#{param.followerId}")
private int followerId;

public FollowMB() {
    System.out.println("I'm in the constructor");
}

@PostConstruct
public void init() {
    System.out.println("I'm in the PostConstruct");
}

public String follow() {
    // DB stuff
}

public String unfollow() {
    // DB stuff
}

public UserMB getUserMB() {
    return userMB;
}

public void setUserMB(UserMB userMB) {
    this.userMB = userMB;
}

public int getFollowerId() {
    return followerId;
}

public void setFollowerId(int followerId) {
    this.followerId = followerId;
}
}

生成的HTML:

<form id="j_idt15:0:j_idt30" name="j_idt15:0:j_idt30" method="post" 
action="/myApp/private/searchUserResult.xhtml" 
enctype="application/x-www-form-urlencoded">

<input type="hidden" name="j_idt15:0:j_idt30" value="j_idt15:0:j_idt30">

<script type="text/javascript" 
src="/myApp/javax.faces.resource/jsf.js.xhtml?ln=javax.faces"></script>

<input type="submit" name="j_idt15:0:j_idt30:j_idt31" value="Follow" 
onclick="mojarra.jsfcljs(document.getElementById('j_idt15:0:j_idt30'),
{'j_idt15:0:j_idt30:j_idt31':'j_idt15:0:j_idt30:j_idt31','followerId':'502'},'');
return false">

<input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" 
value="-6780758405072567889:8862631453580026176" autocomplete="off">

main.jsf

<h:form>
    <h:inputText value="#{searchUserMB.searchTerm}" required="true"/><br/>
    <h:commandButton action="#{searchUserMB.search}" />
</h:form>

SearchUserMB

@ManagedBean
@RequestScoped
public class SearchUserMB {

@EJB
private DBObjectRemote db;

private String searchTerm;
private List<User> searchResult;

public SearchUserMB() {}

public String search() {
    searchResult = db.searchUser(searchTerm);

    return "searchUserResult";
}

public String getSearchTerm() {
    return searchTerm;
}

public void setSearchTerm(String searchTerm) {
    this.searchTerm = searchTerm;
}

public List<User> getSearchResult() {
    return searchResult;
}

public void setSearchResult(List<User> searchResult) {
    this.searchResult = searchResult;
}

}

0 个答案:

没有答案