package cn.edu.hpu.tax.user.entity;
import java.io.Serializable;
public class UserRole implements Serializable {
//联合主键
private UserRoleId id;
public UserRole() {
}
public UserRole(UserRoleId id) {
this.id = id;
}
public UserRoleId getId() {
return id;
}
public void setId(UserRoleId id) {
this.id = id;
}
}
package cn.edu.hpu.tax.user.entity;
import java.io.Serializable;
import cn.edu.hpu.tax.role.entity.Role;
public class UserRoleId implements Serializable {
private String userId;
private Role role;
public UserRoleId(){
}
public UserRoleId(String userId, Role role) {
super();
this.userId = userId;
this.role = role;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((role == null) ? 0 : role.hashCode());
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserRoleId other = (UserRoleId) obj;
if (role == null) {
if (other.role != null)
return false;
} else if (!role.equals(other.role))
return false;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}
}
package cn.edu.hpu.tax.user.action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import cn.edu.hpu.tax.core.action.BaseAction;
import cn.edu.hpu.tax.role.service.RoleService;
import cn.edu.hpu.tax.user.entity.User;
import cn.edu.hpu.tax.user.service.UserService;
public class UserAction extends BaseAction{
@Resource
private UserService userService;
//注入RoleService
@Resource
private RoleService roleService;
//其它参数省略
//跳转到新增页面
public String addUI(){
//加载角色列表
ActionContext.getContext().getContextMap().put("roleList", roleService.findObjects());
return "addUI";
}
//其它方法省略
}
角色:
package cn.edu.hpu.tax.user.action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import cn.edu.hpu.tax.core.action.BaseAction;
import cn.edu.hpu.tax.role.service.RoleService;
import cn.edu.hpu.tax.user.entity.User;
import cn.edu.hpu.tax.user.service.UserService;
public class UserAction extends BaseAction{
//前面的所有参数省略
//用户角色选择的id们
private String[] userRoleIds;
//get和set省略
//保存新增
public String add(){
try {
if(user!=null){
//处理头像
if(headImg!=null){
//1、保存头像到upload/user
//获取保存路径的绝对地址
String filePath=ServletActionContext.getServletContext().getRealPath("upload/user");
//生成带格式的随机文件名称
String fileName=UUID.randomUUID().toString()+headImgFileName.substring(headImgFileName.lastIndexOf("."));
//复制文件
FileUtils.copyFile(headImg, new File(filePath,fileName));
//2、设置用户头像路径
user.setHeadImg("user/"+fileName);
}
//saveUserAndRole方法用来同时保存用户和用户的角色
userService.saveUserAndRole(user,userRoleIds);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
//保存编辑
public String edit(){
try {
if(user!=null){
//处理头像
if(headImg!=null){
//1、保存头像到upload/user
//获取保存路径的绝对地址
String filePath=ServletActionContext.getServletContext().getRealPath("upload/user");
//生成带格式的随机文件名称
String fileName=UUID.randomUUID().toString()+headImgFileName.substring(headImgFileName.lastIndexOf("."));
//复制文件
FileUtils.copyFile(headImg, new File(filePath,fileName));
//如果旧头像存在,把旧头像删除
if(user.getHeadImg()!=null){
String oldfilename=filePath+"\\"+user.getHeadImg().substring(5);
File file=new File(oldfilename);
file.delete();
}
/**/
//2、设置用户头像路径
user.setHeadImg("user/"+fileName);
}
userService.updateUserAndRole(user,userRoleIds);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
//其他方法没有改动,省略
}
@Override
public void saveUserAndRole(User user, String[] userRoleIds) {
//1.保存用户
save(user);
//2.保存用户对于的角色
if(userRoleIds!=null){
for (String roleId:userRoleIds) {
userDao.saveUserRole(new UserRole(new UserRoleId(user.getId(),new Role(roleId))));
}
}
}
@Override
public void updateUserAndRole(User user, String[] userRoleIds) {
//1.根据用户删除该用户的所有角色
userDao.deleteUserRoleByUserId(user.getId());
//2.更新用户
update(user);
//3.保存用户对应的角色
if(userRoleIds!=null){
for (String roleId:userRoleIds) {
userDao.saveUserRole(new UserRole(new UserRoleId(user.getId(),new Role(roleId))));
}
}
}
@Override
public void saveUserRole(UserRole userRole) {
getHibernateTemplate().save(userRole);
}
@Override
public void deleteUserRoleByUserId(String id) {
Query query=getSession().createQuery("DELETE FROM UserRole WHERE id.userId=?");
query.setParameter(0, id);
query.executeUpdate();
}
角色:
//跳转到编辑界面
public String editUI(){
//加载角色列表
ActionContext.getContext().getContextMap().put("roleList", roleService.findObjects());
if(user!=null && user.getId()!=null){
user=userService.findObjectById(user.getId());
//处理角色回显
String[] ids=userService.getRoleIdByUserId(user.getId());
if(ids!=null&&ids.length>0){
userRoleIds=ids;
}
}
return "editUI";
}
@Override
public String[] getRoleIdByUserId(String id) {
return userDao.getRoleIdByUserId(id);
}
@Override
public String[] getRoleIdByUserId(String id) {
Query query=getSession().createQuery("FROM UserRole WHERE id.userId=?");
query.setParameter(0, id);
List list=query.list();
String[] ids=null;
if(list!=null&&list.size()>0){
ids=new String[list.size()];
for (int i = 0; i < list.size(); i++) {
ids[i]=list.get(i).getId().getRole().getRoleId();
}
}
return ids;
}
至此,我们的用户与我们的角色模式正式链接在一块了,下一步我们就要根据用户的角色来调整用户在网站系统中的操作权限了。
转载请注明出处:http://blog.csdn.net/acmman/article/details/49556133