?? userctrlaction.java
字號(hào):
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的導(dǎo)向 ActionForward 字符串
String forward = new String("failure");
try {
DynaActionForm dynaActionForm = (DynaActionForm) actionForm;
//取得當(dāng)前用戶的請(qǐng)求
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);
//創(chuàng)建業(yè)務(wù)操作對(duì)象
user u = new user();
userDto ud = new userDto();
if ("add".equals(action)) { //增加
PropertyUtils.copyProperties(ud,dynaActionForm);//拷貝FormBean=>后臺(tái)的VO對(duì)象(也叫作Dto對(duì)象)
if(u.InsertUser(ud)==true)//調(diào)用具體的業(yè)務(wù)方法
forward = "success_list";//決定轉(zhuǎn)向,也就是你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獲取一個(gè)完整的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)) { //現(xiàn)有用戶列表
System.out.println("你執(zhí)行了嗎?---------------->");
String offset = httpServletRequest.getParameter("pager.offset") == null ?
"0" : httpServletRequest.getParameter("pager.offset");
//取得頁偏移量
int i = Integer.parseInt(offset.trim());
//用戶請(qǐng)求顯示的頁號(hào)
int page = (i / 25) + 1;
Collection userList = null;
//獲取指定頁的數(shù)據(jù)
//這里隨便指定一個(gè)頁的總數(shù),實(shí)際項(xiàng)目中需要?jiǎng)討B(tài)獲得
session.setAttribute("totalItem",new Integer(100));
userList = u.ListUser(25,page);
httpServletRequest.setAttribute("userList", userList);
//下面是通過迭代的方式檢查后臺(tái)是否獲取到數(shù)據(jù)的一種辦法,常常在希望返回給JSP文件一個(gè)Collection時(shí),判斷起后去是否正確
//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) 那么必須使用下面代碼進(jìn)行轉(zhuǎn)換
//ActionForward actionForward = actionMapping.findForward(forward);
//ActionForward actionForward1 = new ActionForward();
//actionForward1.setRedirect(true);
//actionForward1.setName(forward);
//actionForward1.setPath(actionForward.getPath() + "?pager.offset=" +
// offset);
//=============================轉(zhuǎn)換結(jié)束=============================
//return actionForward1;
}
}
catch (Exception e) {
session.setAttribute("errMessage", "意外錯(cuò)誤,請(qǐng)返回重試");
}
//這里是struts真正執(zhí)行轉(zhuǎn)向的地方
return actionMapping.findForward(forward);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -