0

I have a search field in the header, which is basically a inputText and a button (hidden). I have a backing bean, call it search manager, that can hold the search term (string) and exposes a method to perform the actual search (query DB) using the search term. The inputText binds to the search managers search term field, so that the manager knows what is being searched for.

As of scope, I would like the search manager to be either request or viewscoped because anything higher wouldn’t make sense (from a logical point of view). The problem is that if I navigate from the page that contains the search field, let’s say homepage, to the search result page after someone has entered something into the search field, then the search manager bean will be reconstructed and the search word is lost.

I don’t understand how to handle this case in JSF. I don’t see any option. I see many examples on the internet about search examples, but none of them search from a different page and then navigate to the result page. All of them stay on the same page, which does not produce the errors I am dealing with.

Mercious
  • 378
  • 3
  • 25
  • "I would like the search manager to be either request or viewscoped because anything higher wouldn’t make sense". Why? What's wrong with session scoped? – Jasper de Vries Feb 01 '19 at 08:19
  • See https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope – Jasper de Vries Feb 01 '19 at 08:19
  • Different site or different page in the same web app? – Kukeltje Feb 01 '19 at 08:34
  • @JasperdeVries It just doesnt feel right, the search page should be a classic "dump data in, see results and forget it" right? It shouldnt be kept in the session for the user – Mercious Feb 01 '19 at 08:35
  • @jasperdevries: I often use Deltaspike `@viewaccessscoped` for this. – Kukeltje Feb 01 '19 at 08:35
  • @Kukeltje Its a different page on the same webapp. Seriously, I have been sitting at this since 2 hours now, its the most BASIC thing that I can imagine, but I just cant get it to work. The best solution would be for the button to fire a simple GET to the searchPage URL with the content of the input-field (search bar) attached as queryParam, so like /contextPath/searchPage?searchTerm=example. But I have looked at a hundred different links now and it seems to be impossible for JSF to generate a get request (like with a h:button) with the content of a input field. – Mercious Feb 01 '19 at 08:38
  • h:button's target-links are generated at view-creation time, so appending the content of a input field that is only filled later, when the user writes stuff into it, results in a null pointer at view creation time. Seriously, this is extremely frustrating, searching from a different page is the most basic use case and it seems I have to bend backward tenfold to get it to work. – Mercious Feb 01 '19 at 08:39
  • WHICH 100's of links? I don't see them in your question (See ask). And yes Passing it on in a request param is an option too, https://www.google.com/search?client=firefox-b-ab&q=pass+param+via+request+url+jsf – Kukeltje Feb 01 '19 at 09:30
  • Oh and 'complaining' does not really help, it can have the opposite effect, people not wanting to help. – Kukeltje Feb 01 '19 at 09:30
  • Reading all the >25 upvoted Q/A on JSF (and remembering their existens) will answer most of the questions you will ever have on JSF. Most of the things have been asked and answerd. The JSF Framework is that good, stable, well thought, still alive etc... – Kukeltje Feb 01 '19 at 09:32
  • Isn't flashScope a good choice for data to survive redirects? https://stackoverflow.com/questions/11194112/understand-flash-scope-in-jsf2 – Selaron Feb 01 '19 at 10:40
  • @Kukeltje Wasn’t trying to complain, was just frustrated, mostly with myself. Shouldn’t have struck such tone, my bad. The first link you posted tho does not seem like the solution, though. It’s about taking a request parameter for page 1 and then going to page 2. My question was about being one page 1 and going go page 2 with a certain URL parameter that must be read from an input field. I will gather some links and clarify why they don’t work, then edit the original question to make it more clear. – Mercious Feb 01 '19 at 11:08
  • @Selaron It indeed sounds promising and pretty much like the behaviour I want. I will check at home to see if this is what I need. Thanks! – Mercious Feb 01 '19 at 11:12
  • FlashScope needs explicit actions. ViewAccessScoped is easier to use and makes sure it stays available for further ajax requests – Kukeltje Feb 01 '19 at 11:25
  • @Kukeltje Seems like it is an Apache specific scope, though? Sadly I do not have access to anything but standard JSF/CDI stuff – Mercious Feb 01 '19 at 11:29
  • 1
    Seriously? So you cannot use OmniFaces either? Hmmmm.... Fire the person that decided that ;-) Now really serious, you can always download the source and copy/paste it IN your project. People that prevent you from using relevant good external libraries do not understand that the workarounds that get implemented are often worse and come with risks... Maybe you should let that person read this comment – Kukeltje Feb 01 '19 at 11:38

0 Answers0