OA项目设计的能力③

1.然后来了一个,写在我们的主要要求之一,有回波数据还需要添加的方法,我们需要知道,事实上,页被传递id演出id通讯实体name,所以想要回显就是须要得到privilegeIds,假设像上一篇在jsp利用checkbox来显示的话,尽管能显示可是不方便不好换行,显示树状结构也非常麻烦。所以我们改善一下利用自己在jsp中写一段代码来实现相同的功能,代码Jsp代码例如以下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



<html>
<head>
<title>配置权限</title>
      <%@ include file="/WEB-INF/jsp/public/common.jspf" %>
      <script language="javascript" src="${pageContext.request.contextPath}script/jquery_treeview/jquery.treeview.js"></script>
	<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}style/blue/file.css" />
	<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}script/jquery_treeview/jquery.treeview.css" />
     
     <script type="text/javascript">
         $(function{
           $("#root").treeview();
         })
     </script>
     
      
</head>
<body>

<!-- 标题显示 -->
<div id="Title_bar">
    <div id="Title_bar_Head">
        <div id="Title_Head"></div>
        <div id="Title"><!--页面标题-->
            <img border="0" width="13" height="13" src="${pageContext.request.contextPath}style/images/title_arrow.gif"/> 配置权限
        </div>
        <div id="Title_End"></div>
    </div>
</div>

<!--显示表单内容-->
<div id=MainArea>
    <s:form action="roleAction_setPrivilege">
     <s:hidden name="id"></s:hidden>
        <div class="ItemBlock_Title1"><!-- 信息说明 --><div class="ItemBlock_Title1">
        	<img border="0" width="4" height="7" src="${pageContext.request.contextPath}style/blue/images/item_point.gif" /> 正在为【${role.name}】配置权限 </div> 
        </div>
        
        <!-- 表单内容显示 -->
        <div class="ItemBlockBorder">
            <div class="ItemBlock">
                <table cellpadding="0" cellspacing="0" class="mainForm">
					<!--表头-->
					<thead>
						<tr align="LEFT" valign="MIDDLE" id="TableTitle">
							<td width="300px" style="padding-left: 7px;">
								<!-- 假设把全选元素的id指定为selectAll,而且有函数selectAll()。就会有错。由于有一种使用方法:能够直接用id引用元素 -->
								<input type="CHECKBOX" id="cbSelectAll" onClick="selectAll(this.checked)"/>
								<label for="cbSelectAll">全选</label>
							</td>
						</tr>
					</thead>
                   
			   		<!--显示数据列表-->
					<tbody id="TableData">
						<tr class="TableDetail1">
							<!-- 显示权限树 -->
							<td>
							<!--  
							<s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id"  listValue="name"></s:checkboxlist>
							-->
							<span style="color:#ff0000;"><s:iterator value="#privilegeList"> 
							 <input type="checkbox" value="${id}" id="cb_${id}" name="privilegeIds" 
							  <s:property value="id in privilegeIds ? 'checked' : '' " />
							 
							 >
							    <label for="cb_${id}">${name}</label><br/>
							
							
							</s:iterator></span>

</td>
						</tr>
					</tbody>
                </table>
            </div>
        </div>
        
        <!-- 表单操作 -->
        <div id="InputDetailBar">
            <input type="image" src="${pageContext.request.contextPath}style/images/save.png"/>
            <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}style/images/goBack.png"/></a>
        </div>
    </s:form>
</div>

<div class="Description">
	说明:<br />
	1,选中一个权限时:<br />
	     a,应该选中 他的全部直系上级。

<br />      b。应该选中他的全部直系下级。<br /> 2,取消选择一个权限时:<br />      a,应该取消选择 他的全部直系下级。

<br />      b,假设同级的权限都是未选择状态,就应该取消选中他的直接上级,并递归向上做这个操作。<br /> 3。全选/取消全选。<br /> 4。默认选中当前岗位已有的权限。<br /> </div> </body> </html>

解释:红色是改善过的代码

 ①  首先是利用struts2的标签将PrivilegeList迭代出来,由于在后台我们是将它放在Map中所以在前面加一个#代表从map中取出

 ②  然后写经常使用的input  checkbox标签。将须要显示的name动态显示出来,当中的label标签是为了完毕点击名字也能选中的功能,为什么id写成cb_${id}是由于循环一次id必须变化,不能一样cb随便写在加一个动态的id就能够实现点击名字也能选中的效果

 ③  再然后input标签中的value放的是id,跟checkboxlist的listkey相应的,而以下的name跟listValue相应的,上面的name跟checkboxlist的name相应最后在input中又增加的 struts2的标签由于他是在server端解释的,所以在页面我们仅仅看到结果。这个属性的作用是推断当前这个id在全部的权限id中是不是存在假设存在就选中不存在返回空,所         以有的话在页面前面就会回显也就是有勾号,由于我们在设置权限的时候我们点一下他是传过去权限的id了,然后保存到数据库中。所以在回显的时候仅仅须要比較下有么有然后利用checked属性选中就能够实现回显功能了。

 ④  经过改进后效果例如以下


2.给出相应的后台改进过的终于代码:

RoleAction

package com.icss.oa.view.action;

import java.util.HashSet;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.icss.oa.base.BaseAction;
import com.icss.oa.domain.Privilege;
import com.icss.oa.domain.Role;
import com.icss.oa.service.PrivilegeService;
import com.icss.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role>  {
      
	  private Long[] privilegeIds;


		public Long[] getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(Long[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}

		//列表方法
	    public String list() throws Exception {
		   List<Role>  roleList = roleService.findAll();
		   ActionContext.getContext().put("roleList", roleList);
		    return "list";
	    }
	
	    //删除方法
		public String delete() throws Exception {
			roleService.delete(model.getId());
			
			return "toList";
		}

		
		
		//添加页面方法
		public String addUI() throws Exception {
			
			return "addUI";
		}
		
		//添加方法
		public String add() throws Exception {
			//为页面參数设值
			// Role role=new Role();
			//role.setName(role.getName());
			//role.setDescription(role.getDescription());
			//保存到数据库
			roleService.save(model);
			return "toList";
		}
		
		//改动页面方法
		public String editUI() throws Exception {
			  //依据id得到role对象的一条信息并显示
			  Role  role1 = roleService.getById(model.getId());
			  //在edit页面显示数据
			  //this.name=role.getName();
			  //this.description=role.getDescription();
			ActionContext.getContext().getValueStack().push(role1);
			return "editUI";
		}
		
		//改动方法
		public String edit() throws Exception {
			//设置须要改动的值
		Role role2=	roleService.getById(model.getId());
		role2.setName(model.getName());
		role2.setDescription(model.getDescription());
			//update到数据库中
		roleService.update(role2);
			return "toList";
		}

		 //设置权限页面方法
	     public String setPrivilegeUI() throws Exception {
	    	
	    	 
	    	 //准备显示的数据
	    	 Role role=roleService.getById(model.getId());
	    	 ActionContext.getContext().put("role", role);
	    	 List<Privilege> privilegeList=privilegeService.findAll();
	    	 ActionContext.getContext().put("privilegeList", privilegeList);
	    	 
	    	// 准备回显的数据
	 		privilegeIds = new Long[role.getPrivileges().size()];
	 		int index = 0;
	 		for (Privilege privilege : role.getPrivileges()) {
	 			privilegeIds[index++] = privilege.getId();
	 		}
					  
			return "setPrivilegeUI";
		}
				
		//设置权限方法
		public String setPrivilege() throws Exception {
			//从数据库中取出源对象
			Role role=roleService.getById(model.getId());
			//设置须要改动的属性
		    List<Privilege> privileges= privilegeService.getByIds(privilegeIds);  
			role.setPrivileges(new HashSet<Privilege>(privileges));
			//更新到数据库中
			roleService.update(role);
				
			return "toList";
		}

		
}
3.明天给出树状结构的笔记,详细是利用了Jquery官方的一个做好的通用的TreeView来实现的。




版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/zfyouxi/p/4851882.html