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

📄 userctrlaction.java

📁 struts的演示代码
💻 JAVA
字号:
package com.hrsoft.ms.webAction;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.hrsoft.ms.user;
import com.hrsoft.ms.ValueObject.userDto;
import java.util.*;
import org.apache.commons.beanutils.PropertyUtils;


public class userCtrlAction
    extends Action {

  public ActionForward execute(ActionMapping actionMapping,
                               ActionForm actionForm,
                               HttpServletRequest httpServletRequest,
                               HttpServletResponse httpServletResponse) {

	//获取session
    HttpSession session = httpServletRequest.getSession();
	//定义struts的导向 ActionForward 字符串
    String forward = new String("failure");

    try {
      DynaActionForm dynaActionForm = (DynaActionForm) actionForm;

      //取得当前用户的请求
      String action = (String) dynaActionForm.get("action"); 
      action = action.trim();
      if (action.equals("")) {
        action = httpServletRequest.getParameter("action");
      }
	  if(action==null || action.equals("") || action.equals("null"))
		  action = "list";
	  System.out.println("action=======================> " + action);

      //创建业务操作对象
	  user u = new user();
	  userDto ud = new userDto();
	  

      if ("add".equals(action)) { //增加
		PropertyUtils.copyProperties(ud,dynaActionForm);//拷贝FormBean=>后台的VO对象(也叫作Dto对象) 
		if(u.InsertUser(ud)==true)//调用具体的业务方法
			forward = "success_list";//决定转向,也就是你struts-config.xml 里面的 actionmapping 里面的 forward 
      }

      if ("del".equals(action)) { //删除
		PropertyUtils.copyProperties(ud,dynaActionForm);
		if(u.DeleteUser(ud)==true)
			forward = "success_list";
      }

      if ("modify_before".equals(action)) { //修改之前  通过传递过来的id获取一个完整的userDto

		PropertyUtils.copyProperties(ud,dynaActionForm);
		Collection list = u.ListUser(ud.getId());
		Iterator it = list.iterator();
		while(it.hasNext()){
			ud = (userDto)it.next();
			break;
		}

		PropertyUtils.copyProperties(dynaActionForm,ud);
		forward = "success_modefy";

      }

      if ("modify".equals(action)) { //修改
		PropertyUtils.copyProperties(ud,dynaActionForm);
		if(u.ModifyUser(ud)==true)
			forward = "success_list";
      }


      if ("list".equals(action)) { //现有用户列表
	    System.out.println("你执行了吗?---------------->");
        String offset = httpServletRequest.getParameter("pager.offset") == null ?
            "0" : httpServletRequest.getParameter("pager.offset");
        //取得页偏移量
        int i = Integer.parseInt(offset.trim());
        //用户请求显示的页号
        int page = (i / 25) + 1;

        Collection userList = null;
		//获取指定页的数据
		//这里随便指定一个页的总数,实际项目中需要动态获得
		session.setAttribute("totalItem",new Integer(100));
        userList = u.ListUser(25,page);

        httpServletRequest.setAttribute("userList", userList);
		//下面是通过迭代的方式检查后台是否获取到数据的一种办法,常常在希望返回给JSP文件一个Collection时,判断起后去是否正确
		//Iterator it = userList.iterator();
		//while(it.hasNext()){
		//	userDto dd = (userDto)it.next();
		//	System.out.println(dd.getId() + "---");
		//}
        forward = "success";
		dynaActionForm.set("action","list");

        //如果 使用 forward 的重定向(redirect=true) 那么必须使用下面代码进行转换
        //ActionForward actionForward = actionMapping.findForward(forward);
        //ActionForward actionForward1 = new ActionForward();
        //actionForward1.setRedirect(true);
        //actionForward1.setName(forward);
        //actionForward1.setPath(actionForward.getPath() + "?pager.offset=" +
        //                       offset);
        //=============================转换结束=============================
        //return actionForward1;

      }
    }
    catch (Exception e) {
      session.setAttribute("errMessage", "意外错误,请返回重试");
    }
	
	//这里是struts真正执行转向的地方
    return actionMapping.findForward(forward);

  }
}

⌨️ 快捷键说明

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