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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 yeah thats why i mentioned in code to use appropriate data type.
As you wrote, I create a new class that has a Map as attribute. Isn't a Map an appropriate data type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.