リンクを使用してBeanアクションメソッドを呼び出す方法は?オンクリックは機能しません

StackOverflow https://stackoverflow.com/questions/8825776

質問

クリックして再配置できるユーザー名のリストを実装しようとしています また リンク。

<ul>
    <ui:repeat var="user" value="#{cc.attrs.value}">
        <li>
            #{user.name}
            <h:link outcome = "user" value = "left" onclick="#{accountController.moveDown}">
                <f:param name="id" value = "${user.id}" />
            </h:link>
        </li>

    </ui:repeat>
</ul>

ここでの問題は、OnClick属性を正しく使用していないように見えることです。これを行うための適切な方法は何ですか?


編集: あなたのアドバイスに従って、私はすべてのリンクをフォームに配置しました。

<h:form>
        <ui:repeat value="#{cc.attrs.value}" var = "user">
        <div class = "user">
            <h:commandLink id = "Link1" value = "Up" binding = "#{accountController.ommandLink}" action = "#{accountController.moveUserUp}">
                <f:attribute name = "userId" value = "#{user.id}" />
            </h:commandLink>
            <h:commandLink id = "Link2" value = "Down" binding = "#{accountController.commandLink}" action = "#{accountController.moveUserDown}">
                <f:attribute name = "userId" value = "#{user.id}" />
            </h:commandLink>
            <h:commandLink id = "Link3" value = "Delete" binding = "#{accountController.commandLink}" action = "#{accountController.deleteUser}">
                <f:attribute name = "userId" value = "#{user.id}" />
            </h:commandLink>
    </div>
</h:form>

マネージドビーン:

private UIComponent commandLink;

public void moveUserUp(){
    Integer userId = (Integer)commandLink.getAttributes().get("userId");
    System.out.println("MOVE TAB LEFT :" + userId);
}

public void moveUserDown(){
    Integer userId = (Integer)commandLink.getAttributes().get("userId");
    System.out.println("MOVE TAB RIGHT: " + userId);
}

public void deleteUser(){
    Integer userId = (Integer)commandLink.getAttributes().get("userId");
    System.out.println("DELETE TAB: " + userId);
}

public UIComponent getCommandLink() {
    return commandLink;
}

public void setCommandLink(UIComponent commandLink) {
    this.commandLink = commandLink;
}

コマンドリンクとマネージドビーンの間の通信は機能していますが、UIでは最後のコマンドリンク(緊密なアクション)のみが表示されます。

役に立ちましたか?

解決

リンクをクリックすると、Beanアクションメソッドを呼び出すには、必要です <h:commandLink>. 。これはaに囲まれている必要があります <h:form>.

<h:form>
    <h:commandLink ... action="#{bean.action}" />
</h:form>

public String action() {
    // ...

    return "/other.xhtml";
}

JSFでは、 属性 EL式をaとして解釈します MethodExpression アクションメソッドを宣言するために使用できます。他のすべての属性は、と解釈されます ValueExpression また、HTML出力がJSFによって生成されるとすぐに実行されます。これはをカバーします onclick 属性。その値は実際にJavaScript関数を表すべきです。

実際にgetリンクを使用する場合は、アクションメソッドをに移動します <f:viewAction> ターゲットページ。これは、ターゲットページのページロードに呼び出されます。

<h:link ... outcome="/other.xhtml" />

<f:metadata>
    <f:viewAction action="#{bean.onload}" />
</f:metadata>

public void onload() {
    // ...
}

参照:


あなたのアドバイスに従って、私はすべてのリンクをフォームに配置しました

コマンドリンクとマネージドビーンの間の通信は機能していますが、UIでは最後のコマンドリンク(緊密なアクション)のみが表示されます。

複数の物理的に異なるコンポーネントを1つと同じBeanプロパティにバインドしないでください。また <f:attribute> 引数に合格することはハッキーであり、JSF2ではもう必要ありません。サーブレット3.0 / EL 2.2コンテナを使用していると仮定すると(質問履歴はGlassfish 3を使用していることが確認されています)、メソッド引数として引数を直接渡すだけです。

<h:commandLink id="Link1" value="Up" action="#{accountController.moveUserUp(user)}" />
<h:commandLink id="Link2" value="Down" action="#{accountController.moveUserDown(user)}" />
<h:commandLink id="Link3" value="Delete" action="#{accountController.deleteUser(user)}" />

public void moveUserUp(User user) {
    // ...
}

public void moveUserDown(User user) {
    // ...
}

public void deleteUser(User user) {
    // ...
}

参照:

他のヒント

onclick 属性は、JavaScript関数(クライアント側)を呼び出すために使用されます。 JavaScriptクリックイベントHanlderを添付するときに使用されます。

"#{accountController.moveDown}" 方法表現です。そして、名前が示唆するように accountController マネージドビーンです。

として h:link doc 言う:

javax.el.valueExpression(java.lang.stringに評価する必要があります)

最終的に文字列に評価する必要がある値式にすることができます。

JavaScriptコードは、この要素の上にポインターボタンをクリックしたときに実行されます。

アップデート:

あなたが探しているものかもしれません h:commandLink. 。使用できます action バッキングビーンメソッドを呼び出す属性。

I have modified your code, let me know if this is what you are looking at achive

    <h:form>
        <a4j:outputPanel id="userList" ajaxRendered="false">
            <ui:repeat  value="#{manageUser.userList}" var="user">
                <div class="user">
                    <h:panelGrid columns="3">
                    <h:outputText value="#{user.userId} ---- #{user.userName} ---- " />

                    <a4j:commandLink id="LinkUp" value="Up" execute="@this" 
                        action="#{manageUser.moveUserUp}" limitRender="true" render="userList" >
                        <f:setPropertyActionListener value="#{user}" target="#{manageUser.user}" />
                    </a4j:commandLink>

                    <a4j:commandLink id="LinkDown" value="down"
                        action="#{manageUser.moveUserDown}" execute="@this" limitRender="true" render="userList" >
                        <f:setPropertyActionListener value="#{user}" target="#{manageUser.user}" />
                    </a4j:commandLink>
                    </h:panelGrid>
                </div>
            </ui:repeat>
        </a4j:outputPanel>      
        </h:form>

Managed Beans (ManageUser)


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="manageUser")
@ViewScoped
public class ManageUser  implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5338764155023244249L;

    private List<UserBean> userList;

    private UserBean user;

    /**
     * @return the user
     */
    public UserBean getUser() {

        return user;
    }

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


    /**
     * @return the userList
     */
    public List<UserBean> getUserList() {
        return userList;
    }

    /**
     * @param userList the userList to set
     */
    public void setUserList(List<UserBean> userList) {
        this.userList = userList;
    }

    public ManageUser() {
        UserBean user1= new UserBean();
        user1.setUserId("1");
        user1.setUserName("userName1");

        UserBean user2= new UserBean();
        user2.setUserId("2");
        user2.setUserName("userName2");

        UserBean user3= new UserBean();
        user3.setUserId("3");
        user3.setUserName("userName3");

        userList = new ArrayList<UserBean>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
    }

    public void moveUserDown(){

        if(user !=null){
            int indexObj= userList.indexOf(user);

            if(indexObj < userList.size()-1){
                UserBean tempUser=userList.get(indexObj+1);
                userList.set(indexObj+1, user);
                userList.set(indexObj, tempUser);

            }
        }
    }

    public void moveUserUp(){

        if(user !=null){
            int indexObj= userList.indexOf(user);

            if(indexObj > 0){
                UserBean tempUser=userList.get(indexObj-1);
                userList.set(indexObj-1, user);
                userList.set(indexObj, tempUser);

            }
        }
    }

}

UserBean
import java.io.Serializable;

public class UserBean  implements Serializable {



    /**
     * 
     */
    private static final long serialVersionUID = 3820279264217591645L;

    private String userName;

    private String userId;

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top