h:link/h:button outcome methods automatically called during page load without any clicks

隐身守侯 提交于 2019-12-12 07:27:52

问题


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.

Here's the <a> tag implementation:

<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> 

The h:link implementation looks like this:

<h:link title="Searches" id="searchesLogo" value="" outcome="#{bean.gotoMySearches()}">
    <h:graphicImage library="images" name='vo2/SearchesPlus.png' />
</h:link>

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?


回答1:


<a href="#{bean.gotoMySrchie()}">           

<h:link ... outcome="#{bean.gotoMySearches()}">

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.

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.

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.
    }

    // ...
}

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)


来源:https://stackoverflow.com/questions/35539848/hlink-hbutton-outcome-methods-automatically-called-during-page-load-without-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!