4、Spring MVC基础配置

烈酒焚心 提交于 2019-12-08 13:58:25

1、Spring MVC
  MVC和三层架构的区别:
  MVC:Model+View+Controller 数据模型+视图+控制器
  三层架构:Controller+Service+Dao 展现层+应用层+数据访问层
2、常用注解
  @Controller:表明这个类是Spring MVC中的控制器,声明为Spring的一个Bean,Dispatcher Servlet会自动扫描注解了此注解的类,并将
    Web请求映射到注解了@RequestMapping的方法上
  @RequestMapping:用来映射Web请求(访问路径和方法)到处理类和方法上;注解在方法上的路径会继承注解在类上的路径;支持Servlet
    Request和Response作为参数,也支持对Request和Response的媒体类型进行配置
  @RequestBody:允许request参数在request体内而不是直接链接到地址后面,此注解放置在参数前
  @ResponseBody:支持将返回值放在response体内而不是返回一个页面;在Ajax请求中,可以以此注解返回数据而不是页面;此注解可以
    放置在返回值前或方法上
  @PathVariable:接收路径参数,此注解放置在参数前
  @RestController:组合@Controller和@ResponseBody功能
3、其他
  @RequestMapping如果不指定请求类型,则默认为get
  原生Ajax默认格式:text/plain
  Jquery Ajax默认格式(Map) + Form表单POST提交格式:application/x-www-form-urlencoded
  字符流 Ajax格式(Map):application/json ( 同时使用JSON.stringify(data)将json对象转换为json字符串 )
  文件上传:multipart/form-data
4、示例

 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     //必须用@ResponseBody指定返回值的形式
18     @RequestMapping(produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
19     public @ResponseBody String index(HttpServletRequest request) {
20         return "url:" + request.getRequestURI();
21     }
22     
23     //http://localhost:8080/spring4x/stu/paathvar/hello
24     //接收路径参数,不指定请求类型则默认为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 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!