bircastri
bircastri

Reputation: 2167

How to call JSP from a java Controller

I have a simple J2EE application with Spring. Now I want from Java controller call another page. For example, I'm in registrazione.jsp, I click on one button, and I call a method in registrazioneController.java.

Now I want from registrazioneController.java, call another page, for example

home.jsp and I want pass any parameter in get.

It is possible?

this is the method that I use when I click the button

registrazioenControlle.java

public ModelAndView internalLoadPage(HttpServletRequest request, HttpServletResponse response, Map model) throws Exception 
    {

        //to do
        //call another page for example home.html
        request.getRequestDispatcher("home.jsp").forward(request, response);
        return new ModelAndView("home", model); 
    }

I'm try to use this code but no found.

Upvotes: 0

Views: 2466

Answers (1)

Master Slave
Master Slave

Reputation: 28519

In addition to the answer provided in the comments, you can also use RedirectAttributes, and addAttribute method if you want to append a parameter to URL upon redirect. This will also give you the addFlashAttribute that will store the attributes in the flash scope which will make it available to the redirected page. You can also return a simple string as the view name e.g. like

public String internalLoadPage(RedirectAttributes redirectAttributes) throws Exception 
    {
      redirectAttributes.addAttribute("paramterKey", "parameter");
      redirectAttributes.addFlashAttribute("pageKey", "pageAttribute");
      return "redirect:/home";
    }

this assumes that the view suffix is configured in you view resolver configuration

Upvotes: 1

Related Questions