Before learning Springmvc feeling his value is very magical: simple, fast, efficient.
Write a few simple values today to share with you, hope to be helpful to everyone.
One
To pass forward from the back:
(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 what you want to pass in the AddObject (String,object), the value is type Object, everything can be put.
Setviewname () is the setting to which page (success.jsp page) to jump to.
Use ${requestscope} or ${ok in the success.jsp page to remove it. is not very simple 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:
<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 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
Back in 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 this way:
<a href= "Hello/ant/tom/topic/cat" >ant</a>
Corresponds to the 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 is this way:
<a href= "hello/regex/100-111" >regex (regular) </a>
(2) This is a key transfer value:
@RequestMapping (value={"/ok1"}) public
string Ok1 (@RequestParam (value= "username") string username) throws Exception {
System.out.println (username);
Return ' show ';
}
The front end is this way:
<a href= "User/ok1?username=tom" > has key transfer value </a>
This is a no key transfer value:
@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 is this way:
<a href= "user/ok2?username=tom&password=111" > No key transfer </a>
The interesting thing is that it can accurately correspond to two values.
The above Springmvc simple transfer value (implementation code) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.