Simple value transfer for SpringMVC and simple value transfer for SpringMVC
When I learned SpringMVC before, I felt that his passing value was amazing: simple, fast, and efficient.
I want to share with you a few simple values today, hoping to help you.
I,
From back to front:
(1)
@Controller@RequestMapping(value={"/hello"})public class HelloController { @RequestMapping(value={"sub"}) public ModelAndView submit(HttpServletRequest request) throws Exception { // TODO Auto-generated method stub ModelAndView m=new ModelAndView(); m.addObject("ok", "hello"); m.setViewName("success"); return m; }}
Put the Object to be passed in addObject (String, Object). The value is of the Object type and can be put in everything.
SetViewName () is used to set the page to jump to (success. jsp page ).
On the success. jsp page, use $ {requestScope} or $ {OK. Is it very convenient and quick.
It can also be passed in this way:
@Controller@RequestMapping(value={"/user"})public class UserController { @RequestMapping(value={"/get"}) public ModelAndView user(User user) throws Exception { ModelAndView mv=new ModelAndView();
mv.addObject("ok",user.getUsername()+"--"+user.getPassword()); mv.setViewName("success"); return mv; }}
The front end is a simple form:
<Form action ="User/get"Method ="Post">
<Input type ="Text"Name ="Username"Id ="Username">
<Input type ="Text"Name ="Password"Id ="Password">
<Input type ="Submit">
</Form>
(2) The returned value may not be ModelAndView
@RequestMapping(value={"/map"}) public String ok(Map map,Model model,ModelMap modelmap,User user) throws Exception { map.put("ok1", user); model.addAttribute("ok2",user); modelmap.addAttribute("ok3", user); return "show";}
II,
From the beginning to the end:
(1)
@RequestMapping(value={"ant/{username}/topic/{topic}"},method={RequestMethod.GET}) public ModelAndView ant( @PathVariable(value="username") String username, @PathVariable(value="topic") String topic ) throws Exception { // TODO Auto-generated method stub ModelAndView m=new ModelAndView(); System.out.println(username); System.out.println(topic); return m; }
The frontend looks like this:
<A href ="Hello/ant/Tom/topic/Cat"> Ant </a>
Corresponds to value = {"ant/{username}/topic/{topic.
You can also use this form:
@RequestMapping(value={"/regex/{number:\\d+}-{tel:\\d+}"}) public ModelAndView regex( @PathVariable(value="number") int number, @PathVariable(value="tel") String tel ) throws Exception { // TODO Auto-generated method stub ModelAndView m=new ModelAndView(); System.out.println(number); System.out.println(tel); return m; }
The frontend looks like this:
<A href ="Hello/regex/100-111"> Regex (regular) </a>
(2) This is a key-based value:
@RequestMapping(value={"/ok1"}) public String ok1(@RequestParam(value="username") String username) throws Exception { System.out.println(username); return "show"; }
The frontend looks like this:
<A href ="User/ok1? Username = Tom"> Key-based value transfer </a>
This is a value without a key:
@RequestMapping(value={"/ok2"}) public String ok2(@RequestParam String password,@RequestParam String username) throws Exception { System.out.println(username); System.out.println(password); return "show"; }
The frontend looks like this:
<A href ="User/ok2? Username = Tom and password = 111"> Value passing without keys </a>
Interestingly, it can accurately correspond to two values.