1. Spring mvc-annotation (annotations) configuration file Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base- Package= "Cn.happy.*" ></context:component-scan> </beans>
01.spring MVC The most basic annotations of the scattered parameters of automatic assembly
@Controller @requestmapping ("/hr") Public class Mycontroller { @RequestMapping ("/hello.do") public string Show (string Name,model Model) { System.out.println ("= =" +name+ "= ="); Model.addattribute ("msg", name+ "Display page"); return "Happy"; }}
Where the parameter in the method is the name property of the interface form and the field name in the entity class remains constant (" three in one "), the model type instead of using Modelandview to load the data and return directly to the JSP interface,
If you are returning happy directly to the diagram, you need to add a view parser to the configuration file
<!--View parser-- class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/web-inf/jsp/" ></property> <property name= "suffix" value= ". JSP "></property> </bean>
if the properties in the interface are inconsistent, then note @requestparam is required to indicate
@RequestMapping (value= "/list.do", method=requestmethod.post) public String list (model model, @RequestParam (value= "uname", required=false) String name) { System.out.println ("= =" +name ); return "Happy"; }
Parameters of the 02.spring MVC annotation are transferred using object, scope, map, generic as the data
// assembly Object Type @RequestMapping (value= "/list.do", method=requestmethod.post) public String list (Model Model,userinfo info) { System.out.println ("= =" +info.getuname () + "\ t Address" +info.getradd (). Getadd () + "\ t Book 1" +info.getbooklist (). Get (0). Getbookname ()); Model.addattribute ("uname", Info.getuname ()); return "Index"; }
03. Get the property parameter value in the requested address bar
@Controller Public classHandlereturn {/** Get property values in the Address bar*/@RequestMapping ("/{rname}/{age}/first.do") PublicString Handlereturn (model, @PathVariable ("Rname") string name, @PathVariableintAge ) {SYSTEM.OUT.PRINTLN (name+"==="+Age ); Model.addattribute ("Name", name); Model.addattribute ("Age", age); return"Handle"; }}
Where the property name in the address bar is inconsistent with the method parameter name, the name of the property in the address bar is indicated by the annotation @PathVariable as shown in the code rname with the name relationship
The return valueof spring MVC annotations Void, Object, string
@Controller Public classHandleajax {@RequestMapping ("/ajax.do") Public voidHandleajax (httpservletresponse response)throwsexception{//Virtual out someDataMap<string, userinfo> map=NewHashmap<string,userinfo>(); UserInfo U1=NewUserInfo (); U1.setage (12); U1.setname ("Congratulations on employment"); UserInfo U2=NewUserInfo (); U2.setage (122); U2.setname ("Smooth Employment"); Map.put ("001", U1); Map.put ("001", U2); //Tool Map----JSON string FastjsonString jsonstring =json.tojsonstring (map); Response.setcharacterencoding ("Utf-8"); //Response Flowresponse.getwriter (). write (jsonstring); Response.getwriter (). Close (); }}
/** Object return value type in place of other types*/@Controller Public classhandleajaxobject {@RequestMapping ("/num.do") @ResponseBody PublicObject Number () {return1; } @RequestMapping (Value= "/nums.do", produces =" Text/html;charset=utf-8 ") @ResponseBody PublicObject NumberS () {returnChinese characters; } //Processor Method-----UserInfo@RequestMapping (value = "/third.do") @ResponseBody PublicObject Dothird () {UserInfo info=NewUserInfo (); Info.setage (12); Info.setname ("Happy"); returninfo; }
using object as the return value requires annotation @responsebody to upload the data back to the JSP interface
----js<script type= "Text/javascript" > $ (function () { $ ("#btn"). Click ( function () { $.ajax ({ URL:"nums.do", // data refers to information that is printed from the server to the Web browser ( database ) });}); </script>-----Body<input type= "button" id= "btn" value= "Ajax"/>
return JSON data back to the JSP interface using void return value
<script type= "Text/javascript" > $ (function () { $ ("#btn"). Click (function () { $.ajax ({ URL:"ajax.do", //data refers to a printout from the server to the browser// jsonstring jsonobject //{"001": {"Age": 122, "name": "Smooth Employment"}} var result= eval ( "(" +data+ ")"); $.each (Result,function (i,dom) { alert (dom.age) } );});}); </script><input type= "button" id= "btn" value= "Ajax"/>
Finally, it is a string type, but it is similar to the return value of type void.
using string as the return value requires annotation @responsebody to support JSON data back to the JSP interface
/** String return value type instead of other type*/@Controller Public classHandleajax {@RequestMapping ("/ajax.do") Public voidHandleajax (httpservletresponse response)throwsexception{//Forgery of dataMap<string, userinfo> map=NewHashmap<string,userinfo>(); UserInfo U1=NewUserInfo (); U1.setage (12); U1.setname ("Congratulations on employment"); UserInfo U2=NewUserInfo (); U2.setage (122); U2.setname ("Smooth Employment"); Map.put ("001", U1); Map.put ("001", U2); //Tool Map----JSON string FastjsonString jsonstring =json.tojsonstring (map); Response.setcharacterencoding ("Utf-8"); returnjsonstring; }
05. Redirect to another method to
/** Forwarding and redirection * Redirect to another method*/@Controller Public classDispatchreturn {/** Redirect to another method dsipatch.do*/@RequestMapping (Value= "Add.do") PublicString Addallinfo () {return"Redirect:dolist.do"; } @RequestMapping (Value= "Dolist.do") PublicString dolist () {return"Redirect:/list.jsp"; }}
Note: "/" Can not write write
Some cases of Spring MVC annotations