线上实习 扬帆起航——添加权限

//UserController

@RequestMapping("/login.do")
public ModelAndView login(User user, HttpSession session){
int id = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView=new ModelAndView();
if(id!=-1){
List roleIds = roleService.findRoleId(id);
session.setAttribute(“roleIds”,roleIds);
session.setAttribute(“user”,user);
modelAndView.setViewName(“main”);
}else {
modelAndView.setViewName("…/failer");
}
return modelAndView;
}

@RequestMapping("/toAddRole.do")
public ModelAndView toAddRole(int id){
List roleList = roleService.findRoleByUserId(id);
ModelAndView mv = new ModelAndView();
mv.addObject(“roles”,roleList);
mv.addObject(“id”,id);
mv.setViewName(“user-role-add”);
return mv;
}

@RequestMapping(“addRole.do”)
@ResponseBody
public String add(String roleList,String userId){
String[] strs = roleList.split(",");
List ids=new ArrayList<>();
for(String s:strs){
ids.add(Integer.parseInt(s));
}
roleService.add(ids,userId);
return “”;
}

//RoleMapper.xml

select roleId from user_role where userId=#{userId} SELECT * FROM tb_role WHERE id NOT IN (SELECT roleId FROM user_role WHERE userId=#{id}) insert into user_role(userId,roleId) values(#{userId},#{roleId})

//user-role-add.jsp

function addRoles() {
var roleList=new Array();
KaTeX parse error: Expected '}', got 'EOF' at end of input: … roleList.push((this).val())
}
);
var userId=$(“input[name=userId]”).val();
alert(roleList);
alert(userId);
KaTeX parse error: Expected '}', got 'EOF' at end of input: …st", url: "{pageContext.request.contextPath}/user/addRole.do",
data:{roleList:roleList.toString(),userId:userId},
success:function () {
alert(“添加角色成功”);
location.href="${pageContext.request.contextPath}/user/findAll.do";

			},
			error:function () {
				alert("添加角色失败");
			}
		});

	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值