0

Hi I'm new with Spring and I'm having problems in passing data between two pages using two different controllers. I would like to know how I can handle this situations. In my index.html I have a button that should redirect me to a new page passing some data. When i click the button it redirects me to the step2 page but I don't have to objects. How can I solve this? Is the GET method correct? Do I have to use the form just for passing some data between pages and controllers? Below is what I have.

Index.html

<form th:action="@{/step2}" method="GET">
    <input type="hidden" th:value="${mapSelectedServices}" name="mapSelectedServices"/>
    <input type="hidden" th:value="${user}" name="loggedUser"/>
    <div class="form-group d-flex align-items-center justify-content-between">
        <button type="submit" class="btn btn-danger btn-rounded ml-auto" >SEE PRICES
            <i class="fas fa-long-arrow-alt-right ml-2"></i>
        </button>
    </div>
</form>

Step2Controller

@RequestMapping(value="step2", method = RequestMethod.GET)
public ModelAndView step2(ModelAndView modelAndView, @ModelAttribute("user") User user, 
        @ModelAttribute("mapSelectedServices") HashMap<String,List<ServiceOffered>> mapSelectedServices, 
        BindingResult bindingResult){
    modelAndView.addObject("user", user);
    modelAndView.addObject("mapSelectedServices", mapSelectedServices);
    modelAndView.setViewName("step2");
    return modelAndView;
}

Sorry for all the questions, but I'm new to spring development.

andrea
  • 47
  • 1
  • 12
  • hey, I think first you read this tutorial. in this tutorial, you know which method where the use https://www.tutorialspoint.com/http/http_methods.htm – Vipin Pandey Feb 20 '19 at 09:17

1 Answers1

1

HTML page:

<form th:action="@{/step2}" method="POST">
    <input type="hidden" th:value="${mapSelectedServices}" name="mapSelectedServices"/>
    <input type="hidden" th:value="${user}" name="loggedUser"/>
    <div class="form-group d-flex align-items-center justify-content-between">
        <button type="submit" class="btn btn-danger btn-rounded ml-auto" >SEE PRICES
            <i class="fas fa-long-arrow-alt-right ml-2"></i>
        </button>
    </div>
</form>

Controller method:

     public ModelAndView goToPgae2(@ModelAttribute ModelClass aClass)
        {

ModelAndView mv=new ModelAndView("SecondHtlmPageName");//setting view name here
mv.addAttribute("aClass",aClass);
        return mv;
        }

Model Class with the specific variables passed from one page to another:

  class ModelClass {

    public Stirng mapSelectedServices; //use appropriate data type.
    public String loggedUser;

    //create getters and setters
    }

Second page

<div>${aClass.loggedUser}</div>

DONE.

This way you can go to second page . And if you want to redirect to second page and the model attributes should be available there then you need to use flashattribute.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
  • I tried but I'm getting an error of conversion between String and Map. This is the error: `Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'mapSelectedServices': no matching editors or conversion strategy found` – andrea Feb 20 '19 at 13:05
  • @andrea yeah thats why i mentioned in code to use appropriate data type. – Vipin CP Feb 20 '19 at 13:52
  • As you wrote, I create a new class that has a Map as attribute. Isn't a Map an appropriate data type? – andrea Feb 20 '19 at 13:57