struts2 action类
public class T_dealer_accountAction extends ActionSupport{
private static Logger log = Logger.getLogger(T_dealer_accountAction.class.getName());
private T_dealer_accountService service;
private List<T_dealer_account> t_dealer_accounts;
private T_dealer_account t_dealer_account;
private int id;
private int[] ids;
PaginationSupport paginationSupport;
//no inject
public T_dealer_accountAction(){
this.service = (T_dealer_accountService)EjbProxy.getInstance().lookup("T_dealer_accountBean#com.xuka.ejb3.T_dealer_account");
}
//inject
public T_dealer_accountAction(T_dealer_accountService service){
this.service = service;
}
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
//HttpServletResponse response = ServletActionContext.getResponse();
//HttpSession session = request.getSession();
//ActionContext.getContext().getSession().put("msg", "Hello World from Session!");
String message ="查找所有记录操作成功!";
int pageSize = 10;
int startIndex = 0;
try{
if(request.getParameter("pageSize")!=null){
pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
}
if(request.getParameter("startIndex")!=null){
startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
}
this.paginationSupport = service.listAll(pageSize, startIndex );
message ="查找所有记录操作成功!";
System.out.println("-------------T_dealer_accountAction list method---------"+message);
}catch(Exception e){
message ="查找所有记录操作失败!";
log.debug("-------------T_dealer_accountAction list method ---------"+message+" "+e);
}
return SUCCESS;
}
public String save() {
String message ="添加操作失败!";
try{
service.add(t_dealer_account);
this.t_dealer_account = new T_dealer_account();
message ="添加操作成功!";
System.out.println("-------------T_dealer_accountAction add method ---------"+message);
}catch(Exception e){
message ="添加操作失败!";
log.error("-------------T_dealer_accountAction add method ---------"+message+" "+e);
return ERROR;
}
return SUCCESS;
}
public String update(){
String message ="修改操作失败!";
int rows = 0;
try{
rows = service.edit(t_dealer_account);
message ="修改操作成功!";
System.out.println("-------------T_dealer_accountAction edit method ---------"+message);
}catch(Exception e){
message ="修改操作失败!";
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+" "+e);
return ERROR;
}
if(rows ==0){
return ERROR;
}else{
return SUCCESS;
}
}
public String trade(){
HttpServletRequest request = ServletActionContext.getRequest();
String referer = request.getHeader("referer");
if (referer == null||!referer.startsWith("http://"+request.getServerName())){
System.out.println("---------非法请求 trade() 方法---------");
return ERROR;
}
//根据当前登陆的用户session中的信息,进行设置setDealerno,setFromuser
T_user_detail sessionUser = (T_user_detail)request.getSession().getAttribute("user");
if(sessionUser==null){
System.out.println("---------未登录,非法请求 trade() ,不能请求 trade() 方法---------");
return ERROR;
}
t_dealer_account.setDealerno(sessionUser.getDealerno());
t_dealer_account.setFromuser(String.valueOf(sessionUser.getDealerid()));
String message ="操作失败!";
int rows = 0;
try{
t_dealer_account.setAcctip(request.getRemoteAddr());
t_dealer_account.setAmount((int)(t_dealer_account.getAmount()*100)); //元
rows = service.edit(t_dealer_account);
message ="操作成功!";
}catch(Exception e){
message ="操作失败!";
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+" "+e);
return ERROR;
}
if(rows == 1 ){
//修改当前用户的货币数量
sessionUser.setAmount(sessionUser.getAmount()-(int)(t_dealer_account.getAmount())); //点
request.getSession().setAttribute("user", sessionUser);
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+"--------rows="+rows);
return SUCCESS;
}else{
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+"--------rows="+rows);
return ERROR;
}
}
public String bill(){
System.out.println("-------------T_dealer_accountAction bill method ---------");
HttpServletRequest request = ServletActionContext.getRequest();
String referer = request.getHeader("referer");
if (referer == null || !referer.startsWith("http://"+request.getServerName())){
System.out.println("---------非法请求 bill() 方法---------");
return ERROR;
}
//根据当前登陆的用户session中的信息,进行设置setDealerno,setFromuser
T_user_detail sessionUser = (T_user_detail)request.getSession().getAttribute("user");
if(sessionUser==null){
System.out.println("---------未登录,非法请求 bill() ,不能请求 bill() 方法---------");
return ERROR;
}
t_dealer_account.setDealerno(sessionUser.getDealerno());
t_dealer_account.setFromuser(String.valueOf(sessionUser.getDealerid()));
String message ="操作失败!";
int rows = 0;
try{
t_dealer_account.setAcctip(request.getRemoteAddr());
t_dealer_account.setAmount((int)(t_dealer_account.getAmount()*100)); //把人民币转化成点,1元=100点
t_dealer_account.setAr((int)t_dealer_account.getAmount());
t_dealer_account.setDaylimit(365);
t_dealer_account.setTcode("FV0001");
t_dealer_account.setBillno(System.currentTimeMillis()); //纪录关联
t_dealer_account.setCardkind(18);
t_dealer_account.setMno(0);
t_dealer_account.setUserid(t_dealer_account.getTouser());
System.out.println("-------------T_dealer_accountAction bill t_dealer_account.getCztype="+t_dealer_account.getCztype());
System.out.println("--------- InvokeBill selectMethod temp.getGzno()="+t_dealer_account.getGzno());
System.out.println("--------- InvokeBill selectMethod temp.getSysno()="+t_dealer_account.getSysno());
if(t_dealer_account.getCztype()!=null&&t_dealer_account.getCztype().equals("1")){
System.out.println("-------------T_dealer_accountAction bill getCztype=1 method ---------");
t_dealer_account.setSysmno(0);
t_dealer_account.setSysmnoamount(0);
}else if(t_dealer_account.getCztype()!=null&&t_dealer_account.getCztype().equals("2")){
System.out.println("-------------T_dealer_accountAction bill getCztype=2 method ---------");
//获取汇率
T_puser t_puser = new T_puser();
t_puser.setSysno(t_dealer_account.getSysno());
T_puserService t_puserService = new T_puserServiceImpl();
t_puser = (T_puser) t_puserService.findRationBySysno(t_puser);
t_dealer_account.setSysmnoamount(t_dealer_account.getAr()/((int) t_puser.getRatio()));
t_dealer_account.setSysmno(t_puser.getOutmno());
t_dealer_account.setTcode2("FV0002");
System.out.println("--------- t_dealer_account.getAr()="+t_dealer_account.getAr());
System.out.println("--------- (int) t_puser.getRatio()="+(int) t_puser.getRatio());
System.out.println("--------- t_dealer_account.getSysmnoamount()="+t_dealer_account.getSysmnoamount());
}else{
System.out.println("-------------T_dealer_accountAction bill getCztype= null method ---------");
return ERROR;
}
rows = service.bill(t_dealer_account);
message ="操作成功!";
}catch(Exception e){
message ="操作失败!";
System.out.println("-------------T_dealer_accountAction bill method ---------"+message+" "+e);
e.printStackTrace();
return ERROR;
}
if(rows == 1 ){
//修改当前用户的货币数量
sessionUser.setAmount(sessionUser.getAmount()-(int)(t_dealer_account.getAmount())); //点
//request.getSession().setAttribute("user", sessionUser);
System.out.println("-------------T_dealer_accountAction bill method ---------"+message+"--------rows="+rows);
return SUCCESS;
}else{
message ="操作失败!";
System.out.println("-------------T_dealer_accountAction bill method ---------"+message+"--------rows="+rows);
return ERROR;
}
}
public String view(){
setT_dealer_account(find());
return SUCCESS;
}
public String edit(){
setT_dealer_account(find());
return SUCCESS;
}
public List<T_dealer_account> getT_dealer_accounts() {
return t_dealer_accounts;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int[] getIds() {
return ids;
}
public void setIds(int[] ids) {
this.ids = ids;
}
//public void prepare() throws Exception {
// if (id != null)
// T_dealer_account = service.find(id);
//}
public PaginationSupport getPaginationSupport() {
return paginationSupport;
}
public void setPaginationSupport(PaginationSupport paginationSupport) {
this.paginationSupport = paginationSupport;
}
public T_dealer_account getT_dealer_account() {
return t_dealer_account;
}
public void setT_dealer_account(T_dealer_account t_dealer_account) {
this.t_dealer_account = t_dealer_account;
}
}
EJB3 接口、类
import com.common.utils.PaginationSupport;
public interface T_dealer_accountService {
public Object add(Object obj);
public int edit(Object obj);
public int bill(Object obj);
public int deleteByPrimaryKey(Object obj);
public Object findByPrimaryKey(Object obj);
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Remote(T_dealer_accountService.class)
@Stateless(mappedName = "T_dealer_accountServiceBean")
public class T_dealer_accountServiceBean implements
T_dealer_accountService {
private T_dealer_accountServiceImpl service;
public T_dealer_accountServiceBean(){
System.out.println("---------------------start Construct Method --------------------------");
this.service = new T_dealer_accountServiceImpl();
System.out.println("---------------------end Construct Method -----------------------");
}
public Object add(Object obj){
return service.add(obj);
}
public int edit(Object obj){
return service.edit(obj);
}
public int bill(Object obj){
return service.bill(obj);
}
public int deleteByPrimaryKey(Object obj){
return service.deleteByPrimaryKey(obj);
}
public Object findByPrimaryKey(Object obj){
return service.findByPrimaryKey(obj);
}
public PaginationSupport listAll(int pageSize, int startIndex){
return service.listAll(pageSize,startIndex);
}
public T_dealer_accountService getService() {
return service;
}
public void setService(T_dealer_accountServiceImpl service) {
this.service = service;
}
}
public PaginationSupport listAll(int pageSize, int startIndex);
}
持久层 ibaits2.3
public class T_dealer_accountDAOImpl implements T_dealer_accountDAO{
public Object add(Object obj){
Object retObj = null;;
try{
retObj = DaoConfig.getInstance().insert("T_dealer_account.insertT_dealer_account", (T_dealer_account)obj);
}catch(Exception e){
System.out.println("------error="+e);
}
return retObj;
}
//xxxxxxxx操作
public int edit(Object obj) {
//更新结果标量
int rows =0, rows1 = 0, rows2 = 0;
SqlMapClient conn = null;
//账户状态
T_dealer_account temp = (T_dealer_account)obj;
String fromUserLock = null, toUserLock = null;
boolean fromUserLockFlag = false, toUserLockFlag = false;
try {
conn = DaoConfig.getInstance();
fromUserLock = Cache.getLock(temp.getFromuser());
toUserLock = Cache.getLock(temp.getTouser());
} catch (Exception e1) {
System.out.println("------xxxxxxx获取连接失败="+e1);
}
try{
conn.startTransaction();
//如果两个对象有一个或者全部为锁定状态,则要等待,不能对这2个账户进行更新操作
if(fromUserLock.equals("LOCK")||toUserLock.equals("LOCK")){
System.out.println("------账户被锁定,xxxxx失败,清等一会再试-----");
return 0;
}
//同时锁定2账户,如果失败,则返回失败,如果成功,执行下面转卡操作
fromUserLockFlag = Cache.setLock(temp.getFromuser());
toUserLockFlag = Cache.setLock(temp.getTouser());
//如果锁定2账户成功,则执行更新账户操作,否则不执行。
if( fromUserLockFlag && toUserLockFlag){
//更新账户tosuer
rows1 = conn.update("T_dealer_account.updateT_dealer_accountByPrimaryKey", (T_dealer_account)obj);
//更新账户fromuser
rows2 = conn.update("T_dealer_account.updateT_dealer_accountByPrimaryKey2", (T_dealer_account)obj);
System.out.println("------事务rows1="+rows1+"-----事务rows2="+rows2);
//如果2个账户都更新成功,则提交事务,否则,回滚事务
if(rows1==1&&rows2==1){
conn.commitTransaction();
rows =1;
System.out.println("------事务一致----- ");
}
}else{
System.out.println("------锁定账户失败,不能xxxxxx,请等一会再试-----");
return 0;
}
}catch(Exception e){
rows = 0;
System.out.println("------转卡失败="+e);
}finally{
try {
//关闭事务
conn.endTransaction();
} catch (Exception e) {
e.printStackTrace();
System.out.println("------关闭事务失败-----"+e);
}
try {
//插入日志,可以在拦截器做,但考虑程序的清晰,决定放在此处作
T_dealer_log t_dealer_log = new T_dealer_log();
t_dealer_log.setDealerno(temp.getDealerno());
t_dealer_log.setAcctip(temp.getAcctip());
System.out.println("------temp.getAcctip()="+temp.getAcctip());
t_dealer_log.setFromuser(temp.getFromuser());
t_dealer_log.setTouser(temp.getTouser());
t_dealer_log.setAmount((int)temp.getAmount());
t_dealer_log.setType(1);
t_dealer_log.setStatus(rows);
t_dealer_log.setMemo("xxxxxx");
conn.insert("T_dealer_account.insertT_dealer_log",t_dealer_log);
} catch (Exception e) {
System.out.println("------插入日志失败-----"+e);
}
//释放锁定账户,否则造成死锁,如果是前面代码锁定,则释放,如果是别人锁定的,需要别人去释放
//不能释放别人的锁定,否则会造成线程不安全
if(fromUserLockFlag){
Cache.setUnLock(temp.getFromuser());
}
if(toUserLockFlag){
Cache.setUnLock(temp.getTouser());
}
}
return rows;
}
public int deleteByPrimaryKey(Object pk){
int rows = 0;
try{
T_dealer_account key = new T_dealer_account();
key.setId((Integer)pk);
rows = DaoConfig.getInstance().delete("T_dealer_account.deleteT_dealer_accountByPrimaryKey", key);
}catch(Exception e){
System.out.println("------error="+e);
}
return rows;
}
public Object findByPrimaryKey(Object pk){
Object retobj = null;
try{
T_dealer_account key = new T_dealer_account();
key.setId((Integer)pk);
retobj = (T_dealer_account) DaoConfig.getInstance().queryForObject("T_dealer_account.selectT_dealer_accountByPrimaryKey", key);
}catch(Exception e){
System.out.println("------error="+e);
}finally{
return retobj;
}
}
public PaginationSupport listAll(int pageSize, int startIndex){
PaginationSupport ps = null;
try{
T_dealer_account t_dealer_account = new T_dealer_account();
t_dealer_account.setStartNo( startIndex );
t_dealer_account.setEndNo( startIndex + pageSize );
int totalCount = (Integer)DaoConfig.getInstance().queryForObject("T_dealer_account.selectT_dealer_accountCountAll", t_dealer_account);
List items = DaoConfig.getInstance().queryForList("T_dealer_account.selectT_dealer_accountAll", t_dealer_account );
//List items = DaoConfig.getInstance().queryForList("T_dealer_account.selectT_dealer_accountAll",null, startIndex, pageSize);
ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
}catch(Exception e){
System.out.println("------error="+e);
}finally{
return ps;
}
}
}
分享到:
相关推荐
同时,课程还将讨论如何将基于SSH(Struts2+Spring+Hibernate/iBATIS)的应用迁移到WebLogic,以及在WebLogic上的性能优化策略,确保应用在生产环境中运行高效。 讲师具有丰富的Java开发和架构经验,不仅是WebLogic...
5. **框架应用**:开发者熟练使用Struts1、Hibernate、Spring框架,对Struts2、iBatis、Tapestry、JSF等也有一定了解和应用。这显示了其在开发企业级应用时,能够灵活选择和使用合适的框架。 6. **数据库技术**:...
掌握主流的Java开源框架Struts2、Spring、SpringMVC、Hibernate/Mybatis、iBatis等,最好熟悉Freemaker或volecity。 2. 熟悉中间件Tomcat、jboss、Apache、Weblogic、WAS; 3. 熟悉各种Web前端技术,包括JavaScript...
2、你在项目中用到了xml技术的哪些方面?如何实现的? 114 3、用jdom解析xml文件时如何解决中文问题?如何解析? 114 4、编程用JAVA解析XML的方式. 115 5、XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有...
2、你在项目中用到了xml技术的哪些方面?如何实现的? 124 3、用jdom解析xml文件时如何解决中文问题?如何解析? 124 4、编程用JAVA解析XML的方式. 125 5、XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有...
2、你在项目中用到了xml技术的哪些方面?如何实现的? 114 3、用jdom解析xml文件时如何解决中文问题?如何解析? 114 4、编程用JAVA解析XML的方式. 115 5、XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有...
2、你在项目中用到了xml技术的哪些方面?如何实现的? 114 3、用jdom解析xml文件时如何解决中文问题?如何解析? 114 4、编程用JAVA解析XML的方式. 115 5、XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有...
2、你在项目中用到了xml技术的哪些方面?如何实现的? 114 3、用jdom解析xml文件时如何解决中文问题?如何解析? 114 4、编程用JAVA解析XML的方式. 115 5、XML文档定义有几种形式?它们之间有何本质区别?解析XML文档...
2、你在项目中用到了xml技术的哪些方面?如何实现的? 114 3、用jdom解析xml文件时如何解决中文问题?如何解析? 114 4、编程用JAVA解析XML的方式. 115 5、XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有...
二是企业员工绩效考核系统,该项目采用了Struts2框架和EJB3.0组件技术,实现了多层架构,他参与了从设计到编码的各个环节,使用了JavaScript、jQuery、JSON和DWR3进行异步处理。 综上所述,查敏先生是一位拥有扎实...
1. **WebLogic**:BEA WebLogic是成熟的Java应用服务器,包含Web容器和EJB容器。 2. **WebSphere**:IBM的集成软件平台,提供完整的中间件基础设施,支持JavaEE应用。 3. **JBoss**:开源的J2EE应用服务器,支持EJB...
12.5.1. iBATIS 1.x和2.x的概览与区别 12.5.2. iBATIS SQL Maps 1.x 12.5.3. iBATIS SQL Maps 2.x 12.6. JPA 12.6.1. 在Spring环境中建立JPA 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的JPA实现...
12.5.1. iBATIS 1.x和2.x的概览与区别 12.5.2. iBATIS SQL Maps 1.x 12.5.2.1. 创建SqlMap 12.5.2.2. 使用 SqlMapTemplate 和 SqlMapDaoSupport 12.5.3. iBATIS SQL Maps 2.x 12.5.3.1. 创建SqlMapClient 12.5.3.2....
在IT行业中,J2EE(Java 2 Platform, Enterprise Edition)是企业级应用开发的重要标准,涵盖了一系列技术和框架。作为一名J2EE程序员,你需要掌握以下关键领域的知识: 1. **Web开发框架**: - JSF(Facelets,...
2. 谈谈主流的J2EE框架(Spring、Struts、Ibatis、Hibernate等) J2EE框架是Java企业版的核心框架,包括Spring、Struts、Ibatis、Hibernate等,各有其优缺点和应用场景。Spring是一个轻量级的框架,适用于大型企业...
学员将学习如何利用类、对象、继承、封装和多态等概念进行代码设计。同时,还将涉及Java高级特性,如多线程编程和Socket编程,以及如何在Eclipse和NetBeans等集成开发环境中进行有效工作。此外,学员还将学习Linux的...