?? useraddaction.java
字號:
package edu.buptsse.sxjd.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import edu.buptsse.sxjd.common.Constants;
import edu.buptsse.sxjd.model.User;
import edu.buptsse.sxjd.service.BusinessException;
import edu.buptsse.sxjd.service.UserService;
public class UserAddAction extends BaseAction {
@Override
public ActionForward executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws BusinessException {
//從form中取得jsp頁面傳送過來用戶編輯的內容
DynaActionForm frm = (DynaActionForm) form;
//判斷兩次密碼是否一致
//取值方式
//frm.getString("password");
//frm.getString("confirmpwd");
//對比二者是否一致,如果不一致那么生成一個BusinessException拋出MessageKey請自定義
//MessageKey在資源文件中定義
//……你的代碼……
User user = new User();
try {
//BeanUtils.copyProperties的作用請通過互聯網查找
//該類能一定程度的降低編碼量
BeanUtils.copyProperties(user, frm);
} catch (Exception e) {
BusinessException be = new BusinessException();
be.setMessageKey("errors.CopyProperties");
be.setMessageArgs(new String[] { e.getMessage() });
throw be;
}
//插入用戶信息到數據庫
UserService us = new UserService();
us.addUser(user);
return mapping.findForward(Constants.SUCCESS_KEY);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -