Каталог статей
Меню сайта


Форма входа


Категории раздела
WebLogic administration and programming [7]
JSF and Primefaces [1]
Java general programming [12]
Other Java problems [11]
JPA and Hibernate [2]
Spring [2]
Spring


Поиск


Друзья сайта
  • Официальный блог
  • Сообщество uCoz
  • FAQ по системе
  • Инструкции для uCoz


  • Статистика

    Онлайн всего: 1
    Гостей: 1
    Пользователей: 0


    Приветствую Вас, Гость · RSS 20.05.2024, 09:29
    Главная » Статьи » Java » JSF and Primefaces

    JSF: button and commandButton difference

    http://stackoverflow.com/questions/13070537/difference-between-hbutton-and-hcommandbutton

    The <h:button> generates a HTML <input type="button"> which uses JavaScript to navigate to URL as represented by the given navigation case outcome by a HTTP GET request.

    E.g.

    <h:button value="GET button" outcome="otherpage" />

    will generate

    <input type="button" onclick="window.location.href='/contextpath/otherpage.xhtml'; return false;" value="GET button" />

    Even though this ends up in a (bookmarkable) URL change in the browser address bar, this is not SEO-friendly. Searchbots won't follow the URL in the onclick. You'd better use a <h:outputLink> or <h:link> if SEO is important on the given URL. You could if necessary throw in some CSS on the generated HTML <a> element to make it to look like a button.


    The <h:commandButton> generates a HTML <input type="submit"> button which submits by default the parent <h:form> using HTTP POST method and invokes the actions attached to action, actionListener and/or <f:ajax listener>, if any. The <h:form> is required.

    E.g.

    <h:form id="form">
     <h:commandButton id="button" value="POST button" action="otherpage" />
    </h:form>

    will generate

    <form id="form" name="form" method="post" action="/contextpath/currentpage.xhtml" enctype="application/x-www-form-urlencoded">
     <input type="hidden" name="form" value="form" />
     <input type="submit" name="form:button" value="POST button" />
     <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="...." autocomplete="off" />
    </form>

    Note that it thus submits to the current page (the form action URL will show up in the browser address bar). It will afterwards forward to the target page, without any change in the URL in the browser address bar. You could add ?faces-redirect=true parameter to the outcome value to trigger a redirect after POST (as per the Post-Redirect-Get pattern) so that the target URL becomes bookmarkable.

    The <h:commandButton> is usually exclusively used to submit a POST form, not to perform page-to-page navigation. Normally, the action points to some business action, such as saving the form data in DB, which returns a String outcome.

    <h:commandButton ... action="#{bean.save}" />

    with

    public String save() {
     // ...
     return "otherpage";
    }

    Returning null or void will bring you back to the same view. Returning an empty string also, but it would recreate any view scoped bean. These days, with modern JSF2 and <f:ajax>, more than often actions just return to the same view (thus, null or void) wherein the results are conditionally rendered by ajax.

    Категория: JSF and Primefaces | Добавил: basil (22.12.2014)
    Просмотров: 1171 | Рейтинг: 0.0/0
    Всего комментариев: 0
    Имя *:
    Email *:
    Код *:
    Бесплатный конструктор сайтов - uCoz