controller的各种注解

@RequestMapping("/***/***")

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

@Autowired

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作

@GetMapping,处理get请求
@PostMapping,处理post请求
@PutMapping,处理put请求
@DeleteMapping,处理delete请求
@RequiresPermissions(**:**:**)

验证用户是否登录,等同于方法subject.isAuthenticated() 结果为true时。
是shiro的常用注解
执行这个方法会判断用户有没有相应的权限
会在某个地方进行配置,比如controller层其xxx1/xxx2/xxx3的url路径访问相应资源就可以正常访问。

@PathVariable

占位符映射
@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
在这里插入图片描述

@Validated

spring-boot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理。
现在实体类bean上声明需要被校验的字段

@NotBlank(message = "姓名不能为空", groups={GroupOne.class, GroupTwo.class})
	private String name;

2.在controller上声明需要对数据进行校验

@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("person:person:add")
	public R save(@Validated(GroupOne.class) PersonDO person1, BindingResult result1,
			@Validated(GroupTwo.class) PersonDO person2, BindingResult result2, HttpServletRequest request) {
		if (StringUtils.equals("2", person1.getPoliticalStatus())) {
			validate(result2);
		} else {
			validate(result1);
		}

		person1.setCreator(getUser().getUserId());
		if (personService.save(person1, getUser(), request) > 0) {
			return R.ok();
		}
		return R.error();
	}

当输入不能满足条件是,就会抛出异常,而后统一由异常中心处理

@RequestParam

将请求参数绑定到你控制器的方法参数上
@RequestParam三个属性:

@RequestParam(value=”参数名”, required=true/false, defaultValue=””)

(1)value:请求参数名(必须配置)单一参数时,可简化如下:

   /**
    * @Description:    url参数中的name必须要和@RequestParam("name")一致
    * @Author:         vdi100
    */
    @GetMapping("edit1")
    public String edit1(@RequestParam("userId") Integer userId, Model model) {
        System.out.println("*******************" + userId);
        model.addAttribute("userId" , userId);
        return "/admin/ronghe/rongheMobileList/edit";
    }
    /**
     * @Description:    url参数中的name必须要和@RequestParam("name")一致
     *                  参数名字不一样
     * @Author:         vdi100
     */
    @GetMapping("edit2")
    public String edit2(@RequestParam("userId") Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }

(2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)

   /**
    * @Description:    required = true时必须有参数id,否则会报错
    *                  required = false时参数id可不传,默认为null,所以此时参数类型不能为int
    * @Author:         vdi100
    */
    @GetMapping("edit3")
    public String edit3(@RequestParam(value = "id", required = true) Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }

(3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false;如果没有传该参数,就使用默认值(可选配置)

   /**
    * @Description:    设置defaultValue值时,required无论设置是何值,都默认为false
    * @Author:         vdi100
    */
    @GetMapping("edit4")
    public String edit4(@RequestParam(value = "id", required = false, defaultValue = "10") Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }

@RequestParam接收前台传递过来的数组:

	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("person:person:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		personService.batchRemove(ids);
		return R.ok();
	}

@RequestParam接收前台传递过来的Map<String, Object>:

@GetMapping("/selection")
	@RequiresPermissions("person:person:person")
	String personSelection(Model model, @RequestParam Map<String, Object> params) {
		model.addAttribute(session_user_flag, JSONObject.toJSON(getUser()).toString());
		model.addAttribute("params", params);
		return "person/person/personSelection";
	}
@Valid

用于验证注解是否符合要求,直接加在变量user之前,在变量中添加验证信息的要求,当不符合要求时就会在方法中返回message 的错误提示信息。

	@ResponseBody
	@PostMapping("/plusSave")
	@RequiresPermissions("person:pioneerIndex:add")
	public R plusSave(@Valid PioneerIndexDO pioneerIndex, BindingResult result){
		validate(result);
		if(pioneerIndexService.save(pioneerIndex)>0){
			return R.ok();
		}
		return R.error();
	}

然后在 PioneerIndexDO 类中添加验证信息的要求:

@NotBlank(message = "月份不能为空")
	private String month;

当然也可以添加其他验证信息的要求:
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值