SpringMVC advanced-built-in object and redirection

Source: Internet
Author: User

SpringMVC advanced-built-in object and redirection

Built-in objects and page jumps are an important part of web development. springmvc, as a new generation framework, also provides good support for this aspect, this article focuses on the use of Jump methods and built-in objects.

1 page Jump

Page jump is divided into two types: client jump and server jump. Using jump in springmvc is relatively simple, you only need to write after return.

(1) Client jump

return "redirect:user.do?method=reg5";  return "redirect:http://www.baidu.com"; 

(2) server jump
return "forward:index.jsp";return "forward:user.do?method=reg5"; 


2. built-in objects

Springmvc can also use built-in objects like servlet, and also has its own independent writing method. Let's take a look at springmvc's use of built-in objects in the traditional way.

@RequestMapping(params="method=reg2")public String reg2(String uname,HttpServletRequest req,ModelMap map){req.setAttribute("a", "aa");req.getSession().setAttribute("b", "bb");return "index";}

This code is similar to servlet, but springmvc is easier to write, as shown below:

The functions of the built-in ModelMap and request objects are the same.

@ SessionAttributes you can see that this annotation is related to the session by looking at the name. It is mainly used to put the attribute specified in ModelMap into the session. You can refer to the example code below.

@ Controller @ RequestMapping ("/user. do ") @ SessionAttributes ({" u "," a "}) // put the attributes u and a in ModelMap into the session. In this way, both request and session are available. Public class UserController {@ RequestMapping (params = "method = reg4") public String reg4 (ModelMap map) {System. out. println ("HelloController. handleRequest () "); map. addAttribute ("u", "uuuu"); // put u in the request scope, so that this data can be obtained on the forwarding page. Return "index ";}}

The annotation @ ModelAttribute can be used with @ SessionAttribute. The attribute values in ModelMap can be automatically assigned to specified variables through this annotation.

As shown below

@ RequestMapping (params = "method = reg5") public String reg5 (@ ModelAttribute ("u") String uname, ModelMap map) {// [Assign the value of attribute u to the parameter uname] System. out. println ("HelloController. handleRequest () "); System. out. println (uname); return "index ";}


Finally, let's take a look at the ModelAndView Model View class. We can see from the name that the Model in ModelAndView represents the Model, and the View represents the View. That is, this class stores the data to be displayed in the Model attribute, and the view information to jump to is stored in the view attribute. However, in general, a string is returned. To better understand, I also paste the code that uses ModelAndView to return to the page.


@RequestMapping(params="method=login3")public ModelAndView login3(){ModelAndView mv=new ModelAndView();mv.setViewName("index2");return mv;}


Isn't it all easy? The next time I plan to share the content, I will upload the springmvc file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.