AndreaNobili
AndreaNobili

Reputation: 42997

How to correctly initialize an object that have to contain the data retrieved by 2 methods of my controller in this Spring MVC application?

I am pretty new in Spring MVC and I have the following doubt about how correctly achieve the following task.

I am working on a web application that implement a user registration process. This registration process is divided into some consecutive steps.

For example in the first step the user have to insert a identification code (it is a code that identify uniquely a user on some statal administration systems) and in the second step it have to compile a form for his personal data (name, surname, birth date, and so on).

So, actually I have the following controller class that handle these steps:

@Controller
public class RegistrazioneController {

    @Autowired
    private LoadPlacesService loadPlacesService;

    @RequestMapping(value = "/iscrizioneStep1")
    public String iscrizioneStep1(Model model) {
        return "iscrizioneStep1";
    }

    @RequestMapping(value = "/iscrizioneStep2", method=RequestMethod.POST)
    public String iscrizioneStep2(Model model, HttpServletRequest request, @RequestParam("cf") String codicFiscale) {

        System.out.println("INTO iscrizioneStep2()");
        //String codicFiscale = request.getParameter("cf");
        System.out.println("CODICE FISCALE: " + codicFiscale);

        model.addAttribute("codicFiscale", codicFiscale);

        return "iscrizioneStep2";
    }

    @RequestMapping(value = "/iscrizioneStep3", method=RequestMethod.POST)
    public String iscrizioneStep3(@ModelAttribute("SpringWeb")Step2FormCommand step2Form, ModelMap model, HttpServletRequest request) {

        System.out.println("INTO iscrizioneStep3()");
        System.out.println("NOME: " + step2FormCommand.getName());

        return "iscrizioneStep3";

}

Into the iscrizioneStep2() it is retrieved the first code (@RequestParam("cf") String codicFiscale).

Into the iscrizioneStep3() it is retrieved a command object containing the data inserted into the form of the view in which this form was submitted, this one:

@ModelAttribute("SpringWeb")Step2FormCommand step2FormCommand

It works fine.

Now my problem is that I have another object named Step3View that have to be initialized with the aggregation of the @RequestParam("cf") String codicFiscale object retrieved into the iscrizioneStep2() method and the @ModelAttribute("SpringWeb")Step2FormCommand step2FormCommand retrieved into the iscrizioneStep3() method.

This Step3View class simply contain the String codicFiscale and all the fields of the Step2FormCommand class.

Now my doubts are: what is the best way to handle this situation? Where have I to declare this Step3View object? at controller level? (so I can use it in all my controller methods?). Have I to annotate this class with @Component (or something like this) to inject it in my controller?

What is the best solution for this situation?

Upvotes: 1

Views: 67

Answers (1)

K.Nicholas
K.Nicholas

Reputation: 11551

I think in order to get an answer you need to understand the question and ask the right question. I think your question is "how do I pass a parameter from one page to another page in SpringMVC?". You specifically want to know how to pass the "cf" param, but readers here will tend to pass over questions that are too specific because it takes too much time to figure out what you want.

In answer to that, see Spring MVC - passing variables from one page to anther as a possible help.

Also, there are many good answers about this question for JSP in general, which can be worked into the SpringMVC architecture. See How to pass value from one jsp to another jsp page? as a possible help.

Upvotes: 1

Related Questions