简体   繁体   English

h:commandLink 操作不会在使用 JSF 2 的每次点击中触发

[英]h:commandLink action doesn't fire in every click using JSF 2

I have a masterLasyout.xhtml :我有一个masterLasyout.xhtml

<h:form id="abc">
  <h1>
    <ui:insert id="abc1" name="title"></ui:insert>
  </h1>
  <p>
    <ui:insert id="abc2" name="body"></ui:insert>
  </p>
</h:form>

I have 2 snippets files (they are inside ui:composition ):我有 2 个片段文件(它们在ui:composition内):

<p:inputText id="it1" value="#{exampleBean.name}" immediate="true" ></p:inputText>
<h:commandLink id="cl1" immediate="true"  value="Text1" action="#{exampleBean.ModifyLink2}" actionListener="exampleBean.Modify">
    <p:ajax update=":abc:main"></p:ajax>
</h:commandLink>

second file:第二个文件:

<p:inputText id="it2" value="#{exampleBean.name}" immediate="true"></p:inputText>
<h:commandLink id="cl2" immediate="true" value="Text2" action="#{exampleBean.ModifyLink}" actionListener="exampleBean.Modify"  >
    <f:param value="/snippets/snippet1.xhtml" id="link"></f:param>
    <p:ajax update=":abc:main"></p:ajax>
</h:commandLink>

I have the managed bean:我有托管bean:

Now, I wish to switch between the snippets, and between fire an action in the following managed bean:现在,我希望在片段之间切换,并在以下托管 bean 中触发一个动作:

@ManagedBean (name="exampleBean")
@RequestScoped
public class ExampleBean {

    /** Creates a new instance of ExampleBean */
    public ExampleBean() {
        m_User = new User();
        SnippetFileName = "/snippets/snippet2.xhtml";
    }

    public void Modify(ActionEvent a){
        System.out.println("Modify");
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        String t = externalContext.getRequestParameterMap().get("link");        
        System.out.println("SnippetFileName in ModifyLink is " + t);
    }

    public String ModifyLink()
    {        
        System.out.println("ModifyLink");
        SnippetFileName = "/snippets/snippet1.xhtml";
        return "page";
    }

    public String ModifyLink2()
    {
        System.out.println("ModifyLink2");
        SnippetFileName = "/snippets/snippet2.xhtml";
        return "page";
    }

    private String SnippetFileName;

    public String getSnippetFileName()
    {
        return SnippetFileName;
    }

    public void setSnippetFileName(String i_filename)
    {
        SnippetFileName = i_filename;
    }
    private User m_User;
    public String getName(){
        System.out.println("getName");
        if (m_User==null)
        {
            return null;
        }
        return m_User.getName();
    }

    public void setName(String i_Name){
        System.out.println("setName");
        String name = i_Name.trim();
        if (m_User!=null)
        {
            m_User.setName(name);
        }
    }
}

What that is happening is that in clicks number 1,3,5,7... the ModifyLink method fires, and in clicks number 2,4,6,8... `ModifyLink2' doesn't.发生的情况是,在点击数 1、3、5、7 ... ModifyLink方法触发,而在点击数 2、4、6、8 ... `ModifyLink2' 没有。 I can't seem to understand why this behavoiur happens.我似乎无法理解为什么会发生这种行为。 I read few articles, including commandButton/commandLink/ajax action/listener method not invoked or input value not updated that states 7 issues that none of them fits in the spoken case and http://vierwaende.org/articles/posts/jsf-2-evaluation-test.html which brings about a very good overview on JSF 2 lifecycles.我读了几篇文章,包括未调用 commandButton/commandLink/ajax 操作/侦听器方法或未更新输入值,指出 7 个问题都不适合口语案例和http://vierwaende.org/articles/posts/jsf- 2-evaluation-test.html对 JSF 2 个生命周期进行了很好的概述。 I have also tried to remove Ajax, and it still does the same.我也尝试删除 Ajax,它仍然是一样的。

Thanks in advance.提前致谢。

Problem is fixed.问题已解决。 In web.xml I defined:在 web.xml 我定义:

 <context-param>
     <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
     <param-value>false</param-value>
 </context-param>

I wonder whether there is a possibility to add dynamically (programatically or using xml files) in JSF 2 workflow page controls that has to be added dyanmically.我想知道是否有可能在 JSF 2 必须动态添加的工作流页面控件中动态添加(以编程方式或使用 xml 文件)。 In the case of the question above, one of the controls was not defined in the control tree that is created during the first request of the page.在上述问题的情况下,其中一个控件未在页面第一次请求期间创建的控件树中定义。 So, in another note: is there an option to add controls dynamically to the controls tree without modifying web.xml, for optimization?所以,在另一个注意事项中:是否有一个选项可以在不修改 web.xml 的情况下将控件动态添加到控件树中以进行优化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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