When I learned SpringMVC before, I felt that his transfer value was amazing: simple, fast and efficient.
Today I will write a few simple transfers to share with you, hoping that they will be helpful to you.
one,
From behind 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 thing you want to pass in addObject(String,Object), the value is Object type, and you can put anything.
setViewName() is to set which page to jump to (success.jsp page).
Use ${requestScope} or ${ok} in the success.jsp page to remove it. Isn't it very easy and fast?
You can also pass it 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 return value may not be a ModelAndView or not
@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";}two,
From front to back:
(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 front end looks like this:
<a href="hello/ant/Tom/topic/Cat">ant</a>
Corresponds to value={"ant/{username}/topic/{topic}"} one by one.
It can also be in 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 front end looks like this:
<a href="hello/regex/100-111">regex(regular)</a>
(2) This is a key-pass value:
@RequestMapping(value={"/ok1"}) public String ok1(@RequestParam(value="username") String username) throws Exception { System.out.println(username); return "show"; }The front end looks like this:
<a href="user/ok1?username=Tom">Key-transfer value</a>
This is the value that is passed without 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 front end looks like this:
<a href="user/ok2?username=Tom&password=111">No key transmission</a>
Interestingly, it can accurately correspond to two values.
The above article "SpringMVC's simple value transmission (implementation code) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.