h:link / h:在页面加载期间自动调用的按钮结果方法,没有任何点击 [英] h:link/h:button outcome methods automatically called during page load without any clicks
问题描述
我有3个页面,其中有3个链接我尝试使用 h:link
或只是< a>
标签。但在这两种情况下,只要我导航到页面,就会调用连接到这些链接的方法。
I have 3 pages with 3 links that I tried implementing with h:link
or simply an <a>
tag. But in both cases the methods connected to these links are getting called whenever I navigate to the page.
这是< a>
标签实施:
<a href="#{bean.gotoMySrchie()}">
<h:graphicImage library="images" name='vo2/FavPlus.png' />
</a>
<a href="#{bean.gotoMySearches()}">
<h:graphicImage library="images" name='vo2/SearchesPlus.png' />
</a>
<a href="#{bean.gotoMyBids()}">
<h:graphicImage library="images" name='vo2/BidsPlus.png' />
</a>
h:link
实现如下所示:
<h:link title="Searches" id="searchesLogo" value="" outcome="#{bean.gotoMySearches()}">
<h:graphicImage library="images" name='vo2/SearchesPlus.png' />
</h:link>
问题是这些操作方法中的每一种都在调用我的数据库,这完全浪费了呼叫。那么JSF2中是否有一种方法可以在页面之间导航,同时使用动作方法实际执行某些操作,但是当我不需要调用它们时,没有这些方法被调用?
The problem is that each of these action methods is calling my database and this is a complete waste of a call. So is there a way in JSF2 to navigate between pages, while using the action methods for actually doing something, but without these methods getting called when I don't need them to get called?
推荐答案
<a href="#{bean.gotoMySrchie()}">
<h:link ... outcome="#{bean.gotoMySearches()}">
它们被称为值表达式,因为在生成HTML输出期间需要在那里打印它们的返回值在JSF渲染响应阶段。它们的返回值在生成的HTML输出中用作 href
。在webbrowser中右键单击页面,执行查看源并查看 href
属性的值。如果这些方法返回 String
,你会看到那个字符串正好在那里打印。
They are called as value expressions because their return value needs to be printed in there during generating the HTML output during the JSF render response phase. Their return value is used as href
in the resulting HTML output. Rightclick the page in webbrowser, do View Source and look at the value of the href
attribute. If those methods returned String
, you'll see exactly that string being printed there.
它们是,在与您的预期相反,在JSF调用应用程序阶段期间未作为回发结果调用方法表达式。这只是一个根本的误解。要在回发期间调用操作,您需要< h:commandXxx action>
。然而,这会触发POST请求,而不是GET请求,因此可能会产生许多其他后果。
They are, on the contrary to what you expected, not called as method expressions during JSF invoke application phase as result of a postback. This is just a fundamental misunderstanding. To invoke actions during a postback, you need <h:commandXxx action>
. This however fires a POST request, not a GET request, which may thus have many other consequences.
默认情况下单击普通链接会发送GET请求而不是POST请求。如果你想继续使用GET,在这个特定情况下这是一件好事,那么你应该在 @PostConstruct
方法中执行数据库作业支持与目标页面关联的bean,而不是像在那里那样加载包含这些链接的页面。
Clicking a plain link by default sends a GET request not a POST request. If you want to keep using GET, which is a good thing in this specific case, then you should be doing the database job in the @PostConstruct
method of the backing bean associated with target page instead of during loading the page containing those links as you did there.
<h:link ... outcome="/searches">
<h:graphicImage ... />
</h:link>
@Named
@RequestScoped // Or @ViewScoped
public class SearchesBean {
@PostConstruct
public void init() {
// Here.
}
// ...
}
< h3>参见:
- 在页面加载时调用JSF托管bean操作
- 我应该何时使用h:outputLink而不是h:commandLink?
- action和actionListener之间的差异
- 如何在JSF中导航?如何使URL反映当前页面(而不是之前的页面)
- Invoke JSF managed bean action on page load
- When should I use h:outputLink instead of h:commandLink?
- Differences between action and actionListener
- How to navigate in JSF? How to make URL reflect current page (and not previous one)
See also:
这篇关于h:link / h:在页面加载期间自动调用的按钮结果方法,没有任何点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!