2

Here is the commandLink in Home.xhtml:

<h:commandLink action="#{booksBean.selectBook()}">
    <h:graphicImage library="images/books" name="s1.jpg"/>
    <f:param name="isbn" value="25413652" />
</h:commandLink>

But when i want to click on it, the browser address bar is :

http://localhost:8080/OBS2/Home.xhtml

How can i add the isbn value (25413652) at the and of address, and retrieve it at the next page(jsf or bean).

When i am using h:outputLink every thing is good, but since h:outputLink hasn't action() method, i can't call selectBook() of bean.

Sajad
  • 2,273
  • 11
  • 49
  • 92

1 Answers1

4

In other words, you want a GET link instead of a POST link? Use <h:link> instead of <h:commandLink> in the source page, and use in the target page <f:viewParam> to set a bean property based on the request parameter, if necessary along with a Converter which converts from a String representing an ISBN number to a conrete Book instance.

E.g. in Home.xhtml

<h:link outcome="Books.xhtml">
    <h:graphicImage name="images/books/s1.jpg" />
    <f:param name="isbn" value="25413652" />
</h:link>

and in Books.xhtml

<f:metadata>
    <f:viewParam name="isbn" value="#{booksBean.book}" converter="isbnToBookConverter" />
</f:metadata>

with this converter

@FacesConverter("isbnToBookConverter")
public class IsbnToBookConverter {

    @Override
    public Object getAsString(FacesContext context, UIComponent component, Object modelValue) {
        Book book = (Book) modelValue;
        return (book != null) ? book.getIsbn() : "";
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        if (submittedValue == null || submittedValue.isEmpty()) {
            return null; // Let required="true" or @NotNull handle this condition.
        }

        String isbn = submittedValue;
        Book book = someBookService.getByIsbn(isbn);
        return book;
    }

}

Thanks to the converter, you don't need any action to set the selected book. In case you needed to perform another action based on the set book, just add a <f:viewAction> to the <f:metadata>.

<f:metadata>
    <f:viewParam name="isbn" value="#{booksBean.book}" converter="isbnToBookConverter" />
    <f:viewAction action="#{booksBean.initializeBasedOnSelectedBook}" />
</f:metadata>

See also:


Unrelated to the concrete problem, note that I also fixed the improper usage of library attribute of the image component. For the proper usage, head to What is the JSF resource library for and how should it be used? And, you'd better lowercase the XHTML filenames, as URLs are case sensitive and this would fail when an user attempts to type the URL from top of head and wouldn't expect to use capitals ( == bad for UX thus).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    It's just a fictive example of a custom JSF converter. Never created one? I updated the answer with a kickoff example. – BalusC Apr 13 '15 at 11:47
  • Every things good, But why the link value not displayed in address bar? –  Apr 13 '15 at 12:16
  • It just displays `http://localhost:8080/OBS2/Books.xhtml` in address bar after clicking on link: `http://localhost:8080/OBS2/Home.xhtml?isbn=25413652` –  Apr 13 '15 at 12:20
  • It should display something like `http://localhost:8080/OBS2/Books.xhtml?isbn=25413652` . –  Apr 13 '15 at 12:21
  • I return `"Books?faces-redirect=true?`at the end of the `selectBook()` method. –  Apr 13 '15 at 12:24
  • Are you using navigation cases with a redirect or so? You shouldn't be doing that. The answer was as-is. Moreover, in your initial question you wanted to navigate to `Home.xhtml` (the same page) and not `Books.xhtml`. Anyway, I've updated the answer again to show how to really navigate to a different page. – BalusC Apr 13 '15 at 12:25
  • Please help me [Here](http://stackoverflow.com/questions/29614395/hdatatable-value-position) –  Apr 13 '15 at 22:18