Before learning Springmvc, I felt his value was magical: simple, fast and efficient.
Today write a few simple values to share with you, hope to be helpful to everyone.
One
Forward from the back:
(1)
@Controller @requestmapping (value={"/hello"}) Public classHellocontroller {@RequestMapping (value={"Sub"}) PublicModelandview Submit (HttpServletRequest request)throwsException {//TODO auto-generated Method StubModelandview M=NewModelandview (); M.addobject ("OK", "Hello"); M.setviewname ("Success"); returnm; }}
Put what you want to pass in AddObject (String,object), the value is Object type, everything can be put.
Setviewname () is the setting to which page (success.jsp page) to jump to.
${requestscope} or ${ok} can be removed from the success.jsp page. is not very simple and quick.
It can also be transmitted in this way:
@Controller @requestmapping (value={"/user"}) Public class Usercontroller { @RequestMapping (value={"/get"}) publicthrows 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:
<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 Modelandview
@RequestMapping (value={"/map"}) public String throws Exception {map.put ( "Ok1", user); Model.addattribute ("Ok2", user); Modelmap.addattribute ("Ok3", user); return "show";}
Two
From the past:
(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 is 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 (valueint 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 is like this:
<a href="hello/regex/100-111">regex(Regular) </a>
(2) This is a key transfer value:
@RequestMapping (value={"/ok1"}) public String throws Exception { SYSTEM.OUT.PRINTLN (username); return "Show"; }
The front end is like this:
<a href="User/ok1?username=tom"> Key value </a>
This is a non-key pass value:
@RequestMapping (value={"/ok2"}) public String throws Exception { SYSTEM.OUT.PRINTLN (username); SYSTEM.OUT.PRINTLN (password); return "Show"; }
The front end is like this:
<a href="user/ok2?username=tom&password=111"> Non-key transfer value </a>
The interesting thing is that it can accurately correspond to two values.
Springmvc simple pass-through value