简体   繁体   English

JSF-单击<a>元素</a>时调用bean方法

[英]JSF - Calling bean method when clicking <a> element

So I have an anchor element, and it's href attribute is set dynamically from the bean. 所以我有一个anchor元素,它的href属性是从bean动态设置的。 I am using an anchor since it is interacting with fancybox javascript. 我正在使用锚,因为它正在与fancybox javascript交互。 What I need to have happen is to call a method in the bean when the anchor is clicked so that the href will update again. 我需要做的是单击锚点时在Bean中调用一个方法,以便href将再次更新。 There is a textbox above that is also bound to bean. 上面有一个文本框也绑定到了bean。 What needs to happen is to take the text from the text box, and parse that into a query string for the anchor's href. 需要发生的是从文本框中获取文本,并将其解析为锚的href的查询字符串。

I'm trying to figure out the best way to have this happen (if it is possible). 我正在尝试找出发生这种情况的最佳方法(如果可能的话)。 Would I be able to use javascript (or Ajax) to call an updateHref() method? 我可以使用javascript(或Ajax)调用updateHref()方法吗?

The textbox and anchor element (it's ModelBean.ending that needs to be updated): 文本框和锚点元素(需要更新的是ModelBean.ending):

<h:inputTextarea value="#{ModelBean.tweet}" class="textarea.hs-input #{ModelBean.tweetClass}" style="width:200px;" >
                        <f:facet name="label">
                            Tweet
                        </f:facet>
                    </h:inputTextarea>

                </td>
            </tr>
            <tr>
                <td>
                    <h:commandButton value="Analyze Tweet" class="hs-button orange" action="#{ModelBean.tweetAnalysis()}" />
                </td>
                <td>
                    <a href="https://app.hubspot.com/social/#{ModelBean.hubID}/publishing/compose_bookmarklet?body=#{ModelBean.ending}" class="hs-button primary fancybox-iframe" data-fancybox-type="iframe"  >
                        Tweet it!
                    </a>
                </td>
            </tr>

Here's the bit of java that would need to be called: 这是需要调用的Java代码:

ending = URLEncoder.encode(tweet, "UTF-8");
ending = ending.replaceAll("\\+", "%20");

If you think any other part of the code would be useful please let me know! 如果您认为代码的任何其他部分将是有用的,请告诉我! And thanks in advance for any suggestions! 在此先感谢您的任何建议!

If you use JSF 2.0, f:ajax tag may solve your problem. 如果使用JSF 2.0,则f:ajax标记可能会解决您的问题。 Here is sample usege of f:ajax tag, 这是f:ajax标签的示例用法,

    <h:commandLink value="#{..}">
        <f:ajax execute="name" render="output" />
    </h:commandLink>
    <h:inputTextarea id="output" value="#{..}" />

execute =”name” – Indicate the form component with an Id of “name” will be sent to the server for processing. execute =“ name” –表示ID为“ name”的表单组件将被发送到服务器进行处理。 For multiple components, just split it with a space in between, eg execute=”name anotherId anotherxxId”. 对于多个组件,只需在它们之间用空格分隔即可,例如execute =” name anotherId anotherxxId”。 In this case, it will submit the text box value. 在这种情况下,它会提交的文本框的值。

render =”output” – After the Ajax request, it will refresh the component with an id of “output“. render =“输出” –在Ajax请求之后,它将刷新ID为“输出”的组件。 In this case, after the Ajax request is finished, it will refresh the h:inputTextarea component. 在这种情况下,Ajax请求完成后,它将刷新h:inputTextarea组件。

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

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