如何仅在 onclick/oncomplete/on... 事件发生而不是页面加载时调用 JSF 支持 bean 方法 [英] How to call JSF backing bean method only when onclick/oncomplete/on... event occurs and not on page load

查看:18
本文介绍了如何仅在 onclick/oncomplete/on... 事件发生而不是页面加载时调用 JSF 支持 bean 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图像按钮:

<div class="sidebarOptions">
    <input type="image" src="images/homeButton.jpg" onclick="#{home.setRendered(1)}"/>
</div>
<div class="sidebarOptions">
    <input type="image" src="images/memberButton.jpg" onclick="#{home.setRendered(2)}"/>
</div>

但是,当页面加载值为 1 和 2 时,这两种方法都会立即被调用.此外,当我单击它时,这两种方法都会被调用.

However, the both methods are immediately invoked when the page loads with values 1 and 2. Also when I click it, the both methods are invoked.

如何实现只在实际点击图片按钮时才调用bean方法的功能?

How can I achieve the desired functionality of only calling the bean method when the image button is actually clicked?

推荐答案

这种方法行不通.您似乎混淆/混淆了服务器端"和客户端"的基本 Web 开发概念,并且误解了 JSF 和 EL 的作用.

This approach will not work. You seem to be confusing/mixing the basic web development concepts of the "server side" and "client side" and to be misunderstanding the role of JSF and EL.

JSF 是一种服务器端语言,它根据 HTTP 请求在网络服务器上运行,并生成 HTML/CSS/JS 代码,这些代码与 HTTP 响应一起返回.${}#{} 形式的所有 EL 表达式将在生成 HTML 输出期间在服务器端执行.JavaScript 是一种客户端语言,它在网络浏览器上运行并在 HTML DOM 树上工作.HTML onclick 属性应指定一个 JavaScript 函数,该函数将在客户端针对特定 HTML DOM 事件执行.

JSF is a server side language which runs on the webserver upon a HTTP request and produces HTML/CSS/JS code which get returned with the HTTP response. All EL expressions in form of ${} and #{} will be executed in the server side during generating the HTML output. JavaScript is a client side language which runs on the webbrowser and works on the HTML DOM tree. The HTML onclick attribute should specify a JavaScript function which will be executed in the client side on the particular HTML DOM event.

为了调用 JSF 托管 bean 方法,您需要 action*listener 属性.JSF 提供组件来生成所需的 HTML 并指定将更改服务器端状态的所需 ajax 操作.<input type="image"> 可以使用 <h:commandButton image> 生成.bean 方法可以由该组件的 action 属性调用.该组件可以通过嵌入 <f:ajax> 标记来进行 ajaxified.

In order to invoke a JSF managed bean method, you need the action or *listener attribute. JSF provides components to generate the desired HTML and specify the desired ajax actions which would change the server side state. An <input type="image"> can be generated using a <h:commandButton image>. A bean method can be invoked by the action attribute of that component. That component can be ajaxified by embedding the <f:ajax> tag.

所以,以下应该为你做:

So, the following should do it for you:

<h:form>
  <div class="sidebarOptions">
    <h:commandButton image="images/homeButton.jpg" action="#{home.setRendered(1)}">
      <f:ajax execute="@this" render=":sidebar" />
    </h:commandButton>
  </div>
  <div class="sidebarOptions">
    <h:commandButton image="images/memberButton.jpg" action="#{home.setRendered(2)}">
      <f:ajax execute="@this" render=":sidebar" />
    </h:commandButton>
  </div>
</h:form>

<!-- The below is just a guess of what you're really trying to achieve. -->
<h:panelGroup id="sidebar" layout="block">
  <h:panelGroup rendered="#{home.rendered eq 1}">
    Home
  </h:panelGroup>
  <h:panelGroup rendered="#{home.rendered eq 2}">
    Member
  </h:panelGroup>
</h:panelGroup>

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆