0

I need to have a search box on all pages of my website. I know, to have a form I need to initiate it like following method;however, it is useful if user gets to "/search" address, how can I initiate a form that is in all pages like searchbox of tripadvisor that is located on all pages ? should I call the method all the time?

Sample initialize method

@RequestMapping(value = "/search", method = RequestMethod.GET)
public String initCreationForm(Model model) {
    if (!model.containsAttribute(BINDING_RESULT_NAME)) {
        model.addAttribute(ATTRIBUTE_NAME, new Search());
    }
    return "search";
}

Current form

My current form looks like following:

<form:form modelAttribute="search" role="form" method="GET">
.....
</form:form>
Jack
  • 6,430
  • 27
  • 80
  • 151
  • @NeilMcGuigan I am using tiles3 does that do the job? whats your idea about Rembo's answer? – Jack Mar 25 '15 at 00:27
  • tiles has some limitations compared to sitemesh. i'd go w sitemesh 2.4 as that's what grails uses and there's more documentation – Neil McGuigan Mar 25 '15 at 00:37
  • @NeilMcGuigan as I do not know sitemesh much would takes longer to learn, if I want to do it with tiles how would that work? should I have the form in a separate jsp and include that? Whats your idea about Rembo's answer? – Jack Mar 25 '15 at 00:46

3 Answers3

0

Let's suppose that your form is included in a page called search.jsp, you might include it in other pages just putting:

<%@ include file="search.jsp" %>
Michael
  • 3,308
  • 5
  • 24
  • 36
0

how can I initiate a form that is in all pages like searchbox of tripadvisor that is located on all pages ?

Use @ControllerAdvice to add model binding object like:

@ControllerAdvice
class GlobalControllerHandler {

   @ModelAttribute(ATTRIBUTE_NAME)
   public Search bindSearchModel(){
       return new Search();    
   } 
}

In this case @ModelAttribute methods that apply to all @RequestMapping methods. then your current form will work fine.

Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
0

I just manually added the action attribute to the form and it works now. Thanks for your help.

Jack
  • 6,430
  • 27
  • 80
  • 151