1, Spring MVC
difference between MVC and three-tier architecture:
MVC: Model View + + + the Controller data model view controller +
three-tier architecture: Controller + Service + Dao presentation layer + + application layer data access layer
2, commonly used annotation
@Controller : indicates that the class is a Spring MVC in the controller, is declared as a Bean Spring's, Dispatcher Servlet automatically scans comment this annotated classes, and
map Web requests to comment on the method @RequestMapping
@RequestMapping: used to map Web requests (access path and methods) to the processing classes and methods; annotations will inherit annotation on the class path path method; support the Servlet
request and Response as parameters, also supports request and Response media type configuration
@ requestBody: request parameters in the request to allow the body rather than directly to the rear link address, the notes placed in front of the parameter
@ResponseBody: support return values in the body instead of returning a response page; request in Ajax, this annotation can return data instead of the page; the annotation can be
placed on the front return value or method
@Pa thVariable: receive path parameters, placed in front of this annotation parameters
@RestController: @Controller composition and function @ResponseBody
3, other
@RequestMapping request type if not specified, it defaults to get
The default format native Ajax: text / Plain
Jquery Ajax default format (Map) + Form POST form submission format: application / x-www-form -urlencoded
character stream format Ajax (Map): application / json (use JSON.stringify (data ) converting json json string object)
file upload: multipart / form-Data
. 4, example
1 import javax.servlet.http.HttpServletRequest; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 import com.ning.StudentRequestVO; 10 11 // @RequestMapping("/stu")指定映射此类的路径为/stu或/stu/xxx 12 @Controller 13@RequestMapping ( "/ STU" ) 14 public class StudentController { 15 16 // HTTP: // localhost : 8080 / spring4x / STU . 17 // must return the value specified by the form @ResponseBody 18 is @RequestMapping (Produces = "text / Plain ; charset = UTF-. 8 ", Method = RequestMethod.GET) . 19 public @ResponseBody String index (the HttpServletRequest Request) { 20 is return " URL: "+ Request.getRequestURI (); 21 is } 22 is 23 is // HTTP: // localhost : 8080 / spring4x / STU / paathvar / Hello 24 // reception path parameter is not specified the default request type to get 25 @RequestMapping(value = "/paathvar/{str}", produces = "text/plain;charset=UTF-8") 26 public @ResponseBody String stuPathVariable(@PathVariable String str, 27 HttpServletRequest request) { 28 return "url:" + request.getRequestURI() + "; str=" + str; 29 } 30 31 //http://localhost:8080/spring4x/stu/param/?str=hello 32 //如果参数不是str,比如改为strdd,则str为null,输出为:url:/spring4x/stu/param/; str=null 33 @RequestMapping(value = "/param", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET) 34 public @ResponseBody String stuRequestParamNormal(String str, HttpServletRequest request) { 35 return "url:" + request.getRequestURI() + "; str=" + str; 36 } 37 38 //http://localhost:8080/spring4x/stu/obj/?id=11&name=helloword 39 //没有传递值的参数则为null 40 @RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8", method = RequestMethod.GET) 41 public @ResponseBody String stuRequestParamObj(StudentRequestVO obj, 42 HttpServletRequest request) { 43 return "url:" + request.getRequestURI() + "; StudentRequestVO id=" + obj.getId() + ";name=" 44 + obj.getName() + ";mail=" + obj.getMail(); 45 } 46 47 //http://localhost:8080/spring4x/stu/url01 48 //http://localhost:8080/spring4x/stu/url02 49 @RequestMapping(value = {"/url01", 50 "/url02"}, produces = "text/plain;charset=UTF-8", method = RequestMethod.GET) 51 public @ResponseBody String stuMultip(HttpServletRequest request) { 52 return "url:" + request.getRequestURI(); 53 } 54 55 //http://localhost:8080/spring4x/stu/getjson 56 @RequestMapping(value = "/getjson", produces = "application/json;charset=UTF-8", method = RequestMethod.GET) 57 public @ResponseBody StudentRequestVO stuGetJson(HttpServletRequest request) { 58 StudentRequestVO stuObj = new StudentRequestVO(); 59 stuObj.setId(11); 60 stuObj.setName("helloword"); 61 return stuObj; 62 } 63 64 //http://localhost:8080/spring4x/stu/getxml 65 //返回的数据格式为xml 66 @RequestMapping(value = "/getxml", produces = "application/xml;charset=UTF-8", method = RequestMethod.GET) 67 public @ResponseBody StudentRequestVO stuGetXml(HttpServletRequest request) { 68 StudentRequestVO stuObj = new StudentRequestVO(); 69 stuObj.setId(11); 70 stuObj.setName("helloword"); 71 return stuObj; 72 } 73 }