0

I'm trying to use GET parameters to alter the Model Attribute that's creates a table like object returned to the view. To be specific: I want to send the name of a column as a GET parameter and highlight that column by coloring it differently. My current setup looks like this:

private String highlightedCol;

  @ModelAttribute("model")
  public Model populateModel() {
    Model model = new Model();
    generateModel();
    //Use Highlighted Col
    return model;
  }

  @RequestMapping("/index")
  public String getIndex(@RequestParam(value="ts", required = false, defaultValue="") String col) {
    highlightedCol = col;
    return "index";
  }

I'm having trouble using the "col" parameter in the ModelAttribute because the ModelAttribute gets executed before the RequestMapping. How would i go about using the GET parameter for my Model?

NMLcygni
  • 61
  • 1
  • 7

2 Answers2

1

You can simply use method=RequestMethod.GET . Beware in GET or POST method. Make sure if you want t read/get date, use GET. If you want to use insert data, use POST

@RequestMapping(value="/your_url", method=RequestMethod.GET)
@ModelAttribute("model")
varman
  • 8,704
  • 5
  • 19
  • 53
  • Does this mean to put a @RequestMapping above the populateModel method or to extend the existing one? I can access the col paramteter from the getIndex method, but only after the model is already generated. – NMLcygni Jul 03 '17 at 07:19
  • You can use `@RequestMapping ` for any kind of URL. But here Your can easily call the `populateModel(String col)` and pass parameters' – varman Jul 03 '17 at 07:26
  • How would i tell the controller which html file, i want him to display? currently i'm doing this by returning "index" in the get Index method. if i don't have that method anymore how would i do that? – NMLcygni Jul 03 '17 at 07:33
  • Actually u have to know about `@ModelAttribute ` or ` (@CommandName). ` *https://stackoverflow.com/questions/3423262/what-is-modelattribute-in-spring-mvc* will help you to find. Simply `@ModelAttribute("yourForm")` parameter should be same of `` – varman Jul 03 '17 at 08:10
  • I asked one of my colleagues at work and he showed me this solution to my problem: Instead of using the @ModelAttribute Annotation, he changed my getndex method like this: He added a Map model to the method and added the method formerly accessed by the ModelAttribute to it. Then instead of returning a String "index", I now return a `new ModelAndView("index", model)`. I will answer my own Question with that answer, as it works perfectly fine for me. – NMLcygni Jul 04 '17 at 08:17
0

After asking a colleague about my problem he came up with this answer: I got rid of the @ModelAttribute Annotation and changed the getIndex method to this:

@RequestMapping("/index")
  public ModelAndView getIndex(@RequestParam(value="ts", required = false, defaultValue="") String col) {
    highlightedCol = col;

    Map<String, Object> model = new HashMap<>();
    model.put("model", populateModel());

    return new ModelAndView("index", model);
  }

This way the populateModel method is no longer executed before the getIndex method, allowing me to set the highlightedCol before creating the model, where i need that field.

NMLcygni
  • 61
  • 1
  • 7