1

I found lots of similar questions, but none of the solutions worked for me.

I have this inputText field in which the user types a text, and this text is saved in my bean:

<h:inputText value="#{searchBean.param}"/>

Then there's this button that when clicked, executes a search algorithm and then redirects the user to the results page, like this:

<h:commandButton value="Botao" action="#{searchBean.execute}"/>

The execute method:

public String execute() {
      levenshtein(this.param);
      return "results";
}

This will forward the user to ..../results.xhtml, and i need this link to be like this:

.../results.xhtml?equation="whatever the user intended to search"&pagenumber="current page number"

When I click on a link in this page, I'm redirected outside my site, say to wikipedia, and then when I click the back button on my browser, I need to be able to recover what the I searched for and the page I was.

I spent quite some time trying to find out how to do this and every attempt failed, please help me, thank you.

victor
  • 1,532
  • 1
  • 13
  • 32

2 Answers2

2

You want a GET form. This is easy in combination with <f:viewParam>.

View:

<f:metadata>
    <f:viewParam id="query" name="query" value="#{searchBean.query}" />
    <f:event type="preRenderView" listener="#{searchBean.execute}" />
</f:metadata>
...
<form>
    <label for="query">Query</label>
    <input type="text" name="query" value="#{param.query}" />
    <input type="submit" value="Search" />
    <h:message for="query" />
</form>
...
<h:dataTable value="#{searchBean.results}" var="result" rendered="#{not empty searchBean.results}">
     ...
</h:dataTable>

Bean:

private String query;
private List<Result> results;

@EJB
private SearchService service;

public void execute() {
    results = service.search(query);
}

// ...

Note: this shows the results in the same page. Also note that if you want to add paging links/buttons, they should be normal GET links/buttons and not POST (command) links/buttons.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • When the user leaves the page, and then hit back on browser, the objects are gone, right? So I suppose I must read the current page and the searched query from the url, when the user clicks on next page, but I must also check if the objects are null, so I can know for sure that one's acting over a cache page. How can achieve this? – victor Feb 26 '13 at 17:16
  • So you haven't tested the kickoff example at all? Do so before making incorrect assumptions. – BalusC Feb 26 '13 at 17:23
  • Sorry, I think you misunderstood me. I didn't mean that you example does not work, or isn't enough. Im just trying to get your confirmation that, yes, this is how it should happen for what I need to do, or no, that's not quite it works. Again, sorry. – victor Feb 27 '13 at 15:01
  • I get "com.sun.faces.mgbean.ManagedBeanCreationException", error while injecting – victor Feb 27 '13 at 15:34
  • Please look at the root cause of the exception in the stack trace to learn about the root cause of the exception. – BalusC Feb 27 '13 at 15:39
  • javax.naming.NameNotFoundException: SearchService#SearchService not found. Keep in mind I created this SearchService just to test your example, and the code is just the execute returning a 2 entry list: LinkedList teste = new LinkedList(); teste.add(query); teste.add(query); return teste – victor Feb 28 '13 at 15:14
  • BlausC, how do I do to redirect the user to a results.xhtml with the parameter in the url as opposed to display the results in the same page? – victor Feb 28 '13 at 15:15
  • It seems that you didn't create an EJB (e.g. the `@Stateless` annotation is absent). The EJB in my answer was just examplary. I just assumed that you're following the standard Java EE way of creating service classes. If you aren't using EJBs at all, just use your own way of interacting with the DB and maintaining transactions. That part is in turn completely unrelated to your concrete question. You should just do exactly the same desired job as you already did in command button's `action` method. – BalusC Feb 28 '13 at 15:15
  • Ok, I'm gonna work on it. Can you point me some books or sources you used to study and learn jsf, because you know a lot man, thank you and congratulations. – victor Feb 28 '13 at 16:58
  • My command button `action` called `searchBean.execute()`, which would return a String "return", thus redirecting to ../return.xhtml. I'm trying but I can't get this redirection, I think I did not understand how your example works... I don't see where the parameter value is being read from, I even erased `f event` and `value` from input, but the parameter is correctly set anyway. – victor Feb 28 '13 at 17:15
0

You want GET parameters. The normal way for web applications to retrieve data for read purposes is to use GET parameters. Unfortunately, JSF does not support this easily. JSF2 has view parameters and JSF has a workable equivalent hack. There is a long article on this topic at devzone: http://java.dzone.com/articles/bookmarkability-jsf-2

Gardner Bickford
  • 1,934
  • 1
  • 17
  • 25
  • 1
    Similar but from BalusC (JSF expert): [What can and be used for?](http://stackoverflow.com/q/6377798/1065197). IMO, he covers all that info more concisely. – Luiggi Mendoza Feb 25 '13 at 23:11
  • Actually, fires GET, some componentes fire only POST... when I use , the parameter isn't updated, because it is generated before action... but there must be a way to recover or add the parameter to the url inside the bean... i just dont know how to do it – victor Feb 26 '13 at 02:07