在页面加载期间自动调用的href/h:link结果/h:button结果方法,无需任何点击 [英] a href / h:link outcome / 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中是否有一种方法可以在页面之间导航,同时使用action方法实际执行某项操作,但是当我不需要调用这些方法时又没有调用这些方法?
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()}">
它们之所以称为值表达式,是因为在JSF渲染响应阶段生成HTML输出期间,需要在其中打印其返回值.它们的返回值在结果HTML输出中用作href
.右键单击Web浏览器中的页面,执行查看源代码,然后查看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>
.
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>
instead.
<h:form>
<h:commandLink ... action="#{bean.gotoMySearches()}" />
</h:form>
但是,这会触发POST请求,而不是GET请求,因此可能会导致许多其他后果.
This however fires a POST request, not a GET request, which may thus have many other consequences.
默认情况下,单击普通链接会发送GET请求而不是POST请求.如果要继续使用GET,请
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.
}
// ...
}
另请参见:
- 在页面加载时调用JSF托管Bean操作
- 何时应使用h:outputLink代替h:commandLink?
- action和actionListener之间的差异
-
See also:
- 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)
这篇关于在页面加载期间自动调用的href/h:link结果/h:button结果方法,无需任何点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!