⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addtreeaction.java

📁 这是本人曾经在公司里用的,内部开发框架,基于struts+hibernate今天分享给大家
💻 JAVA
字号:
/**
 * 
 */
package test.tree;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.bway.common.BwayHibernateException;
import cn.bway.common.LoginConstants;
import cn.bway.common.WebConstant;
import cn.bway.common.action.BaseAction;
import cn.bway.common.dao.HibernateSessionFactory;

/**
 * @author Kson
 *
 */
public class AddtreeAction extends BaseAction {

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	public ActionForward execute(ActionMapping mapping,
				ActionForm form,HttpServletRequest request,	HttpServletResponse response)
				throws BwayHibernateException, UnsupportedEncodingException {
		ActionErrors errors = new ActionErrors();
		String returnStr = WebConstant.FORWARD_SUCCESS;
		Session sess=null;
		Transaction tx=null;
		HttpSession session=request.getSession();
		String staffid="";
		
//		request.setCharacterEncoding("gb2312");
		String menuid = request.getParameter("menuid");
		String treename = request.getParameter("treename");
		
		try {
			sess=HibernateSessionFactory.currentSession();
			tx=sess.beginTransaction();
			staffid=(String) session.getAttribute(LoginConstants.GLOBLE_STAFF_ID);
			
			//求出当前最大的值
			String maxvalue = getMaxValue(sess);
			testtree t = new testtree();
			t.setMenuid(maxvalue);
			t.setMenuname(treename);
			t.setMunefatherid(menuid);
			t.setMenuurl("test url");
			t.setStaffid(staffid);
			
			sess.save(t);
			tx.commit();
			
			returnStr=WebConstant.FORWARD_SUCCESS;
			request.setAttribute(WebConstant.RETURN_Message,"添加成功!");
			
			returnStr="/myoffice/communicattree.jsp?menuid="+maxvalue+"&menufatherid="+menuid;
			
			System.out.println("returnStr:   "+returnStr);
			
		} catch (Exception e) {
			returnStr=WebConstant.FORWARD_FAIL;
			errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("edu.system.excption",e.getMessage()));
			this.saveErrors(request,errors);
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
    	}
		try {
			response.sendRedirect(request.getContextPath()+"/"+returnStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
//		return mapping.findForward(returnStr);
	}
	
	//求出当前最大值
	public static String getMaxValue(Session sess)  throws BwayHibernateException {
		String maxvalue  ="";
		PreparedStatement stat = null;
		ResultSet rs = null;
		String sql = "";
		try{
			sql = " select MAX(id) as maxvalue from testtree  ";
			stat = sess.connection().prepareStatement(sql);
			rs = stat.executeQuery();
			if(rs.next())
				maxvalue = rs.getString("maxvalue");
			else
				maxvalue="0";
		}catch(Exception e){
			e.printStackTrace();
		}
		return maxvalue;
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -