springmvc (4) The results are shown to the front

1. Set ModelAndView object. The view and the view name resolver jump to a specific page (prefix + viewName + ViewResolver ViewResolver suffix)

mvc.xml 
<-! configuration view resolver -> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <! - Results prefix view -> <property name="prefix" value="/WEB-INF/jsp/"/> <! - results view suffix -> <property name="suffix" value=".jsp"/> </bean>
Processor class: public class HelloControllerControll the implements the Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView ModelAndView = new new ModelAndView (); // package to be displayed in the data view modelAndView.addObject ( " MSG " , " the Hello SimpleUrlHandlerMapping " ); // attempt name modelAndView.setViewName ( " Hello " ); // / WEB- INF / JSP / hello.jsp return ModelAndView; } }

The code of the page jump ([ViewResolver prefix / WEB-INF / jsp / viewName] + [hello] + [ViewRenderer suffix .jsp]), i.e. /WEB-INF/jsp/hello.jsp

2. achieved by Servlet API, need not view resolver

1) output by HttpServletResponse

 @RequestMapping("/hello")
    public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().println("use HttpServlet API");
    }

Here is a thing that, in addition to / hello can be mapped to the processor, / hello. * And / hello / can be mapped, / hello.jsp not, because the extension .jsp tomcat comes with a higher priority than my match configuration [/], it will be a .jsp hello.jsp match, the match will not be my dispatcherServlet.

2) be redirected through HttpServletResponse

  @RequestMapping("/hello")
    public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("index.jsp");
    }

3) forwarded by HttpServletRequest

 @RequestMapping("/hello")
    public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setAttribute("msg","HttpServletRequest forward");
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

The forward will not be used view resolvers

3. forwarding and redirection springMVC - no view when the parser

1) forwarding achieve

  @RequestMapping("/hello1")
    public String hello(){
        //转发1
        return "index.jsp";
    }

 @RequestMapping("/hello1")
public String hello(){
//转发2
return "forward:index.jsp";
}
 

2) redirected to achieve

  @RequestMapping("/hello1")
    public String hello(){
        return "redirect:index.jsp";
    }

4. forwarding and redirection springMVC - when there ViewResolver

Forwarding of implementation:

   @RequestMapping("/hello2")
    public String hello2(){
        return "hello";
    }

By view resolver will find: /WEB-INF/jsp/hello.jsp

Redirection view resolver not be used, because a path is redefined, corresponding to a restart request corresponding to mvc mapping method into the processor rather than in response to rendering data and the like jsp pages.

 

Guess you like

Origin www.cnblogs.com/yuby/p/11012786.html