最近一段开始研究OA,开始了大量写代码,锻炼一下,收获还是挺大的,我弄懂了servlet的流程:
servlet的工作模式
客户端发送请求至服务器服务器启动并调用Servlet,Servlet根据客户端请求生成响应内容并将其传给服务器
服务器将响应返回客户端
以下是一个具体的查找并显示用户的实例:
1.先编写页面
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%> <!--struts标签-->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String flowstepid=request.getParameter("flowstepid");
String flowmoduleid = request.getParameter("flowmoduleid");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>检索用户--searchuser.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script>
function add()
{
selform.action="searchuser.do?method=addUser&flowstepid=<%=flowstepid%>&flowmoduleid=<%=flowmoduleid%>"; //获取页面传递来的参数
selform.submit();
}
</script>
</head>
<body>
<html:form action="searchuser.do?method=searchUser" styleId="selform"> <!--在进入此页面时执行此方法-->
<table width="60%" align=center border="1" bordercolor="#FFFFFF" >
<tr>
<td width="30%">全选</td>
<td width="50%">用户名</td>
</tr>
<logic:empty name="searchuserlist" >
<tr>
<td class=listcellrow valign="top" align="center" colspan="2">
没有查到符合查询条件的记录! <!--判断为空否-->
</td>
</tr>
</logic:empty>
<logic:notEmpty name="searchuserlist" >
<logic:present name="searchuserlist" >
<logic:iterate id="ppp" name="searchuserlist"> <!--此处id =ppp与下面的ppp相对应-->
<tr>
<td>
<input type="checkbox" name="czlist" value="<bean:write name="ppp" property="UCode" />"/> <!--此处UCode/UName对应form中的-->
</td>
<td>
<bean:write name="ppp" property="UName" />
</td>
</tr>
</logic:iterate>
</logic:present>
</logic:notEmpty>
<tr><td valign="top" align="center" colspan="2">
<html:button property="OK" value="确 定" onclick="add()"></html:button>
</td>
</tr>
</table>
</html:form>
</body>
</html>
2.配置WEB-INF中system.xml文件
1)在<action-mappings> 中添加
<!-- 添加 根据资源号查找用户表单实现 -->
<action attribute="flowuserForm" name="flowuserForm" parameter="method" path="/searchuser" scope="request"
type="cn.ac.sict.ietd.system.action.FlowUserAction">
<forward name="searchuser" path="/Web/Users/searchuser.jsp" />
<forward name="selectedusers" path="/Web/Users/SelectedUsers.jsp"/>
<forward name="show" path="/Web/Users/showSelectedUsers.jsp" />
<forward name="showweituo" path="/Web/Users/showSelectedWTUsers.jsp"/>
<forward name="selectnextuser" path="/Web/Users/selectnextuser.jsp"/>
<forward name="searchAlluser" path="/Web/Users/selectcommission.jsp" />
</action>
2)在 <form-beans>中添加
<form-bean name="flowuserForm" type="cn.ac.sict.ietd.system.form.FlowUserForm" />
</form-beans>
3.在src这编写后台java代码,分三层
1). Action:
/**
* @author monica
* @time 2009-10-26
*/
import java.io.FileNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import cn.ac.sict.ietd.system.business.FlowUserService;
public class FlowUserAction extends DispatchAction {
/**
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
//显示已选用户
public ActionForward selectedUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.selectUserlist(form, request);
return mapping.findForward("selectedusers");
}
//点击新增按钮执行查询选择操作
public ActionForward searchUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.searchUserlist(form, request);
return mapping.findForward("searchuser");
}
//点击确定按钮执行选择操作
public ActionForward addUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.addUserlist(form, request);
//return mapping.findForward("selectedusers");
return selectedUser(mapping, form, request, response); //重新执行一遍选择操作
}
//点击删除按钮执行删除操作
public ActionForward deleteUser(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.deleteUserlist(form, request);
return selectedUser(mapping, form, request, response); //重新执行一遍选择操作
}
/**
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward selectRunStepUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.selectRunStepUser(form, request);
return mapping.findForward("show");
}
/**
* 选择要委托的用户
* @author **
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward selectRunStepUserWT(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String wtzt = request.getParameter("wtzt");
FlowUserService fus = new FlowUserService();
if("1".equals(wtzt))//自由流程,查询流程所有有权限的人
{
fus.selectRunStepUser(form, request);
}
if("2".equals(wtzt))//固定流程,查询流程步骤模型对应的人
{
fus.selectwt(form, request);
}
return mapping.findForward("showweituo");
}
/**显示下一个用户
* @author monica
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward selectNextUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.selectUserlist(form, request);
return mapping.findForward("selectnextuser");
}
/**执行查询所有拥有此权限的用户的操作
* @author monica
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward searchAllUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FlowUserService fus = new FlowUserService();
fus.searchAllUserlist(form, request);
return mapping.findForward("searchAlluser");
}
}
2).Service
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import cn.ac.sict.ietd.dao.DAO;
import cn.ac.sict.ietd.orm.FlowEvent;
import cn.ac.sict.ietd.orm.FlowForm;
import cn.ac.sict.ietd.orm.FlowFormContext;
import cn.ac.sict.ietd.orm.FlowFormList;
import cn.ac.sict.ietd.orm.FlowModule;
import cn.ac.sict.ietd.orm.FlowSort;
import cn.ac.sict.ietd.orm.FlowStepHandleMan;
import cn.ac.sict.ietd.orm.FlowStepModule;
import cn.ac.sict.ietd.orm.RunFlowStep;
import cn.ac.sict.ietd.orm.SysResourceOwner;
import cn.ac.sict.ietd.orm.SysUserRoles;
import cn.ac.sict.ietd.orm.SysUsers;
import cn.ac.sict.ietd.test.Test;
import cn.ac.sict.ietd.util.DateUtil;
import cn.ac.sict.ietd.workflow.business.FlowCore;
import cn.ac.sict.ietd.system.form.FlowUserForm;
/**
* 查找/添加资源用户算法函数
* @author monica
*@time 2009-10-26
*/
public class FlowUserService {
/**
* 根据flowstepid查询已经选择的用户
* @param id
* @return
* @throws Exception
*/
public ArrayList selectUserlist(String id) throws Exception
{
ArrayList userlist = new ArrayList();
DAO dao = new DAO();
ArrayList list1 = new ArrayList();
FlowCore fc = new FlowCore();//工作流核心类
FlowStepModule fsm = fc.getFlowStepModuleById(id);
String sql ="from cn.ac.sict.ietd.orm.FlowStepHandleMan where flowstepid='"+fsm.getFlbh()+"'";
ArrayList list = dao.select(sql);
if(list!=null && list.size()>0)
{
for(int i=0;i<list.size();i++)
{
FlowStepHandleMan fshm=(FlowStepHandleMan)list.get(i);
String uId=fshm.getUserId();
String sql1 ="from cn.ac.sict.ietd.orm.SysUsers where UCode='"+uId+"'";
list1 = dao.select(sql1);
userlist.addAll(list1);
}
}
return userlist;
}
/**
* 根据flowstepid查询已经选择的用户
* @param af
* @param request
* @return
* @throws Exception
*/
public boolean selectUserlist(ActionForm af,HttpServletRequest request) throws Exception
{
String id=request.getParameter("flowstepid");
ArrayList list1 = selectUserlist(id);
if(list1!=null && list1.size()>0)
{
request.setAttribute("selecteduserlist", list1);
}
return true;
}
/**
* 根据flowId查询已经选择的用户
* @param id
* @return
* @throws Exception
*/
public ArrayList selectRunStepUser(String flowId) throws Exception
{
DAO dao = new DAO();
ArrayList userlist = new ArrayList();
//根据FlowId查询拥有资源的用户
String sql0 = "from cn.ac.sict.ietd.orm.SysResourceOwner where RCode='"+flowId+"'";
ArrayList list0 = dao.select(sql0);
if(list0!=null && list0.size()>0)
{
//request.setAttribute("flowformlist", list);
for(int i=0;i<list0.size();i++){
SysResourceOwner sro=(SysResourceOwner)list0.get(i);
String owner=sro.getOwner();
if(owner.length()<10){
String sql2 = "from cn.ac.sict.ietd.orm.SysUserRoles where roleid='"+owner+"'";
ArrayList list2 = dao.select(sql2);
if(list2!=null && list2.size()>0)
{
for(int j=0;j<list2.size();j++)
{
SysUserRoles sur=(SysUserRoles)list2.get(j);
String ucode=sur.getUCode();
String sql3 = "from cn.ac.sict.ietd.orm.SysUsers where UCode='"+ucode+"'";
ArrayList list3 = dao.select(sql3);
if(list3!=null && list3.size()>0)
{
userlist.addAll(list3);
}
}
}
}
else {
String sql4 = "from cn.ac.sict.ietd.orm.SysUsers where UCode='"+owner+"'";
ArrayList list4 = dao.select(sql4);
if(list4!=null && list4.size()>0)
{
userlist.addAll(list4);
}
}
}
}
return userlist;
}
/**
* 根据flowId查询已经选择的用户
* @param af
* @param request
* @return
* @throws Exception
*/
public boolean selectRunStepUser(ActionForm af,HttpServletRequest request) throws Exception
{
String wh=request.getParameter("wh");
FlowCore fc = new FlowCore();//工作流核心类
FlowEvent fe = fc.getFlowEvent(wh);
if(fe==null)
{
return false;
}
FlowModule fm = fc.getFlowModule(fe.getFlowModuleId());
if(fm==null)
{
return false;
}
String flowid = fm.getFlowId();
ArrayList list1 = selectRunStepUser(flowid);
if(list1!=null && list1.size()>0)
{
request.setAttribute("selecteduserlist", list1);
}
return true;
}
/**
* 根据flowId查询已经选择的用户
* @param af
* @param request
* @return
* @throws Exception
*/
public boolean selectwt(ActionForm af,HttpServletRequest request) throws Exception
{
String runstepid=request.getParameter("runstepid");
FlowCore fc = new FlowCore();//工作流核心类
RunFlowStep rfs = fc.getRunFlowStep(runstepid);
if(rfs==null)
{
return false;
}
FlowStepModule fsm = fc.getFlowStepModuleById(rfs.getFlowStepId());
if(fsm==null)
{
return false;
}
/*
* flowstepid存的是模型步骤的分类编号
*/
String sql = "from cn.ac.sict.ietd.orm.FlowStepHandleMan where flowstepid = '"+fsm.getFlbh()+"'";
DAO dao = new DAO();
ArrayList list = dao.select(sql);
if(list!=null && list.size()>0)
{
String userid = "(";
for(int i=0;i<list.size();i++)
{
FlowStepHandleMan fshm = (FlowStepHandleMan)list.get(i);
userid +="'"+fshm.getUserId()+"'";
if(i==list.size()-1)
{
break;
}
userid +=",";
}
userid +=")";
String sql1 = "from cn.ac.sict.ietd.orm.SysUsers where UCode in"+userid;
ArrayList userlist = dao.select(sql1);
if(userlist!=null && userlist.size()>0)
{
request.setAttribute("selecteduserlist", userlist);
}
}
return true;
}
/**
*根据Flowid查询资源用户列表
* @param fff
* @param request
* @throws Exception
*/
public ArrayList select(String id) throws Exception
{
ArrayList userlist = new ArrayList();
DAO dao = new DAO();
/*
* 根据stepId查询flowId
*/
String sql ="from cn.ac.sict.ietd.orm.FlowStepModule where flowstepid='"+id+"'";
ArrayList list = dao.select(sql);
if(list!=null && list.size()>0)
{
FlowStepModule fsm=(FlowStepModule)list.get(0);
String flowModuleId=fsm.getFlowModuleId();
String sql1 ="from cn.ac.sict.ietd.orm.FlowModule where flowModuleId='"+flowModuleId+"'";
ArrayList list1 = dao.select(sql1);
if(list1!=null&&list1.size()>0)
{
FlowModule fm=(FlowModule)list1.get(0);
String flowId=fm.getFlowId();
//根据FlowId查询拥有资源的用户
String sql0 = "from cn.ac.sict.ietd.orm.SysResourceOwner where RCode='"+flowId+"'";
ArrayList list0 = dao.select(sql0);
if(list0!=null && list0.size()>0)
{
//request.setAttribute("flowformlist", list);
for(int i=0;i<list0.size();i++){
SysResourceOwner sro=(SysResourceOwner)list0.get(i);
String owner=sro.getOwner();
if(owner.length()<10){
String sql2 = "from cn.ac.sict.ietd.orm.SysUserRoles where roleid='"+owner+"'";
ArrayList list2 = dao.select(sql2);
if(list2!=null && list2.size()>0)
{
for(int j=0;j<list2.size();j++)
{
SysUserRoles sur=(SysUserRoles)list2.get(j);
String ucode=sur.getUCode();
String sql3 = "from cn.ac.sict.ietd.orm.SysUsers where UCode='"+ucode+"'";
ArrayList list3 = dao.select(sql3);
if(list3!=null && list3.size()>0)
{
userlist.addAll(list3);
}
}
}
}
else {
String sql4 = "from cn.ac.sict.ietd.orm.SysUsers where UCode='"+owner+"'";
ArrayList list4 = dao.select(sql4);
if(list4!=null && list4.size()>0)
{
userlist.addAll(list4);
}
}
}
//request.setAttribute("searchuserlist", userlist);
}
}
}
return userlist;
}
public boolean searchAllUserlist(ActionForm af,HttpServletRequest request) throws Exception
{
String id=request.getParameter("flowstepid");//获取flowstepid
ArrayList list = select(id);
if(list!=null && list.size()>0)
{
request.setAttribute("searchuserlist", list);
}
return true;
}
/*
* 移除所有已选用户
*/
public ArrayList unselectUserlist(String id)throws Exception
{
ArrayList list1= select(id);
ArrayList list2=selectUserlist(id);
list1.removeAll(list2);
return list1;
}
/**
* 根据flowstepid查询FlowId
* 再根据FlowId,查询拥有资源的用户
* @param af
* @param request
* @return
* @throws Exception
*/
public boolean searchUserlist(ActionForm af,HttpServletRequest request) throws Exception
{
String id=request.getParameter("flowstepid");//获取flowstepid
ArrayList list = unselectUserlist(id);
if(list!=null && list.size()>0)
{
request.setAttribute("searchuserlist", list);
}
return true;
}
/*
* 新增已选用户
*/
public boolean addUserlist(ActionForm form,HttpServletRequest request)throws Exception
{
FlowUserForm fuf=(FlowUserForm)form;
DAO dao = new DAO();
String id=request.getParameter("flowstepid");//获取flowstepid
String userid[] = fuf.getCzlist();
ArrayList list = new ArrayList();
if(userid!=null && userid.length>0){
for(int i=0;i<userid.length;i++){
FlowStepHandleMan fshm=new FlowStepHandleMan();
fshm.setFlowstepid(id);
fshm.setUserId(userid[i]);
list.add(fshm);
}
return dao.save(list);
}
return false;
}
//从FlowStepHandleMan中删除选中用户
public boolean deleteUserlist(ActionForm form,HttpServletRequest request)throws Exception
{
FlowUserForm fuf=(FlowUserForm)form;
DAO dao = new DAO();
String userid[] = fuf.getCzlist();
if(userid!=null && userid.length>0) {
for(int i=0;i<userid.length;i++){
String sql = "from cn.ac.sict.ietd.orm.FlowStepHandleMan where userId='"+userid[i]+"'";
ArrayList slist = new ArrayList();
dao.select(sql,slist);
dao.delete(slist);
}
}
return false;
}
}
3) .Form
import org.apache.struts.action.ActionForm;
public class FlowUserForm extends ActionForm{
private String[] czlist;
private String UCode;
private String UName;
public String getUCode() {
return UCode;
}
public void setUCode(String code) {
UCode = code;
}
public String getUName() {
return UName;
}
public void setUName(String name) {
UName = name;
}
public String[] getCzlist() {
return czlist;
}
public void setCzlist(String[] czlist) {
this.czlist = czlist;
}
}