当前访客身份:游客 [ 登录  | 注册加入尚学堂]
启用新域名sxt.cn
新闻资讯

struts2.3+spring3.3+hibernate3.6OA项目实战(三)

goldbin 发表于 2年前  | 评论(0 )| 阅读次数(798 )|   1 人收藏此文章,   我要收藏
摘要 struts2.3+spring3.3+hibernate3.6OA项目实战,Action的写法。

表现层代码实现

Action写法

在spring和struts2整合之后,Action对象交给spring的IOC容器管理。以下代码实现Action的零配置。

新建OrgAction类

package com.bjsxt.oa.web.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bjsxt.oa.manager.OrgManager;
import com.bjsxt.oa.po.Organization;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
@Action("org")
@ParentPackage("struts-default")
@Results({
	@Result(name="list",location="/org/list.jsp"),
	@Result(name="add",location="/org/add.jsp")
})
public class OrgAction {

	//接受请求中的父机构ID
	private int pid;
	//接受请求的中的机构数据
	private Organization org;
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public Organization getOrg() {
		return org;
	}
	public void setOrg(Organization org) {
		this.org = org;
	}
	
	@Autowired
	private OrgManager orgManager;
	/**
	 * 查询机构列表
	 * @return
	 */
	public String execute()throws Exception{
		//获得父机构的父机构的ID
		int ppid=0;//父机构的父机构的ID
		if(pid!=0){
			Organization parent =orgManager.findEntity(Organization.class, pid);
			Organization pparent= parent.getParent();//取得父机构的父机构
			if(pparent!=null){
				ppid=pparent.getId();
			}
		}
		ActionContext.getContext().put("ppid", ppid);
		
		
		ActionContext.getContext().put("datas", orgManager.findOrgByParent(pid));
		return "list";
	}
	/**
	 * 删除机构
	 * @return
	 */
	public void delete()throws Exception{
		HttpServletResponse response =ServletActionContext.getResponse();//获得响应对象,专门给客户端发送数据
		
		boolean f= orgManager.deleteOrg(org.getId());
		PrintWriter out = response.getWriter();
		out.print(f);//直接响应boolean类型数据
		out.flush();
		out.close();
	}
	/**
	 * 进入添加机构页面
	 * @return
	 */
	public String toAdd()throws Exception{
		return "add";
	}
	/**
	 * 添加机构
	 * @return
	 */
	public String add()throws Exception{
		orgManager.saveOrg(org, pid);
		return execute();//跳转到机构列表页面
	}
	
}


使用UUR模版写jsp

机构列表的jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--框架必需start-->
<script type="text/javascript" src="js/jquery-1.4.js"></script>
<script type="text/javascript" src="js/framework.js"></script>
<link href="css/import_basic.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" type="text/css" id="skin" prePath="<%=basePath%>"/>
<!--框架必需end-->
<script>
	$(function(){
		top.Dialog.close()
	})

</script>

<script type="text/javascript">

	
	function openAddOrg(){
		var diag = new top.Dialog();
		diag.Title = "添加机构信息";
		diag.URL = "org!toAdd.action?pid=<s:property value="pid"/>";
		diag.Height=380;
		diag.MessageTitle="添加机构";
		diag.Message="请填写机构名字和机构描述";
		diag.show();
	
	}
	
	function deleteOrg(id){
		top.Dialog.confirm("确认要删除该机构吗?",function(){
				//发送ajax请求	
				$.ajax({
				   type: "POST",
				   url: "org!delete.action",
				   data: "org.id="+id,
				   success: function(data){//服务器响应数据
				     if(data=='true'){
				    	 //top.Dialog.alert("删除成功");//异步弹出
				    	 alert("删除成功");//同步弹出信息提示框
				    	 document.location.reload();
				     }else{
				    	 top.Dialog.alert("该机构下有子机构或者有人员,不能删除!");
				     }
				   }
				}); 
			});
	}

	

</script>
</head>


<body>

<div  id="scrollContent">
	<div class="box2" panelTitle="机构列表" roller="true">
		<a href="javascript:void(0)" onclick="openAddOrg()">添加机构信息</a>
		 &nbsp;&nbsp;<a href="org.action?pid=<s:property value="#ppid"/>">返回</a>
		<table class="tableStyle" >
			<tr>
				<th width="5%">序号</th>
				<th width="15%">机构名称</th>
				<th width="10%">机构编号</th>
				<th width="20%">父机构名称</th>
				<th width="20%">操作</th>
			</tr>
			<s:if test="#datas==null ||#datas.size()==0">
				<tr><td colspan="5" align="center">没有查到任何数据!</td></tr>
			</s:if>
			<s:else>
				<s:iterator value="#datas" var="org" status="ind">
					<tr>
						<td><s:property value="#ind.index+1"/> </td>
						<td><a href="org.action?pid=<s:property value="#org.id"/>"> <s:property value="#org.name"/> </a> </td>
						<td><s:property value="#org.sn"/> </td>
						<td><s:property value="#org.parent==null? '': #org.parent.name"/> </td>
						<td><a href="javascript:void(0);" onclick="deleteOrg(<s:property value="#org.id"/>)"> 删除 </a> </td>
					</tr>
				</s:iterator>
			</s:else>
			
		</table>
	</div>
</div>
</body>
</html>

添加机构的jsp:add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--框架必需start-->
<script type="text/javascript" src="js/jquery-1.4.js"></script>
<script type="text/javascript" src="js/framework.js"></script>
<link href="css/import_basic.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" type="text/css" id="skin" prePath="<%=basePath%>"/>
<!--框架必需end-->

<body>
	<div class="box1" whiteBg="true">
	<form action="org!add.action" method="post" target="frmright">
	<input type="hidden" name="pid" value='<s:property value="pid"/>'/>
	<table class="tableStyle" transMode="true">
		<tr>
			<td>机构名称:</td><td><input type="text" name="org.name"/></td>
		</tr>
		<tr>
			<td>机构描述:</td><td><input type="text" name="org.remark"/></td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value=" 提 交 "/>
				<input type="reset" value=" 重 置 "/>
			</td>
		</tr>
	</table>
	</form>
	</div>	
	

</body>
</html>

分享到:0
关注微信,跟着我们扩展技术视野。每天推送IT新技术文章,每周聚焦一门新技术。微信二维码如下:
微信公众账号:尚学堂(微信号:bjsxt-java)
声明:博客文章版权属于原创作者,受法律保护。如果侵犯了您的权利,请联系管理员,我们将及时删除!
(邮箱:webmaster#sxt.cn(#换为@))
北京总部地址:北京市海淀区西三旗桥东建材城西路85号神州科技园B座三层尚学堂 咨询电话:400-009-1906 010-56233821
Copyright 2007-2015 北京尚学堂科技有限公司 京ICP备13018289号-1 京公网安备11010802015183