栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC学习(二)——注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC学习(二)——注解

SpringMVC学习(二)——注解

出处:PC的个人Blog

1. 主要注解
@Controller
@RequestMapping
@PostMapping
@GetMapping
@PathVariable
1.1 @Controller

@Controller的使用需要进行包扫描


再次进行相对应的控制类

@Controller
@RequestMapping("/r")
public class HelloController {
   @RequestMapping("/hello")
   public String hello(Model model) {
      // 封装数据
      model.addAttribute("msg", "Hello,SpringMVC");
      return "test"; // 被视图解析处理
   }
}
  • 可以使用类@RequestMapping和方法@RequestMapping组合一起
  • 类中的方法可以是多个,映射不同的@RequestMapping
1.2 RestFull风格讲解

对于域名后缀斜杠后面不使用 ? 或者 &这些符号,要与PathVariable 协同,可以使用相同url 对应不同的提交方法,同时简洁高效,个人认为安全可以隐藏参数

使用
@PostMapping
@GetMapping
区分

  • @PathVariable 函数参数变量路径

  • get请求

    // 原来的风格 : http://localhost:8080/add?a=1&b=2
    // RestFul :http://localhost:8080/add1/a/b
    @RequestMapping("/add")
    public String test1(int a, int b, Model model) {
       int res = a + b;
       model.addAttribute("msg", "结果为" + res);
       return "test";
    }
    
    @RequestMapping("/add1/{a}/{b}")
    public String test2(@PathVariable int a,@PathVariable int b, Model model) {
       int res = a + b;
       model.addAttribute("msg", "结果为" + res);
       return "test";
    }
    
  • // 网络请求方法
    public enum RequestMethod {
        GET,
        HEAD,
        POST,
        PUT,
        PATCH,
        DELETE,
        OPTIONS,
        TRACE;
    }
    
  • 指定域名方法,get post等

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    
2. ServletAPI
  • HttpServletRequest 和 HttpServletResponse 进行转发和重定向
@Controller
public class ModelTest1 {
   @RequestMapping("/m1/t1")
   public String test(HttpServletRequest request, HttpServletResponse response) {
      HttpSession session = request.getSession();
      System.out.println(session.getId());
      return "test";
   }
}
3. SpringMVC 进行重定向和转发
  • 视图解析器

    
    
            
            
        
    
3.1 无需视图解析器

重定向 : 跳转地址栏url是会改变的

跳转不会改变地址栏的地址

@Controller
public class ModelTest2 {
   @RequestMapping("/m2/t1")
   public String test1(Model model) {
      //转发
      model.addAttribute("msg", "good");
      return "forward:/WEB-INF/jsp/test.jsp";
   }

   @RequestMapping("/m2/t2")
   public String test2(Model model) {
      //重定向
      model.addAttribute("msg", "good");
      return "redirect:/index.jsp";
   }
}
  • 访问 http://localhost:8080/m2/t2 重定向地址为 http://localhost:8080/index.jsp?msg=good
3.2 有视图解析器

对于转发的情况可以少使用前缀和后缀

@Controller
public class ModelTest2 {
   @RequestMapping("/m2/t1")
   public String test1(Model model) {
      //转发
      model.addAttribute("msg", "good");
      return "test";
   }

   @RequestMapping("/m2/t2")
   public String test2(Model model) {
      //重定向
      model.addAttribute("msg", "good");
      return "redirect:/index.jsp";
   }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490809.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号