DispatchAction
来源:互联网 发布:自我认知 知乎 编辑:程序博客网 时间:2024/05/16 14:59
DispatchAction的转向,可以将所有的Action合为一个Action
- package com.bjsxt.drp.web.usermgr.actions;
- import java.util.Date;
- import java.util.List;
- 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.actions.DispatchAction;
- import com.bjsxt.drp.business.usermgr.manager.UserManager;
- import com.bjsxt.drp.business.usermgr.model.User;
- import com.bjsxt.drp.web.usermgr.forms.UserActionForm;
- /**
- * 处理用户管理的Action
- * @author Administrator
- *
- */
- public class UserAction extends DispatchAction {
- @Override
- protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- System.out.println("--------UserAction.unspecified()----------");
- return null;
- }//这个方法是在没有指定具体的方法名的情况下会自动调用
- /**
- * 用户添加
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- public ActionForward add(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- //获取从页面表单中提交过来的值
- UserActionForm uaf = (UserActionForm)form;
- //创建User实体对象,并将ActionForm中的内容设置到User对象中
- System.out.println("姓名:"+uaf.getUserName());
- User user = new User();
- user.setUserId(uaf.getUserId());
- user.setUserName(uaf.getUserName());
- user.setPassword(uaf.getPassword());
- user.setEmail(uaf.getEmail());
- user.setContactTel(uaf.getContactTel());
- user.setCreateDate(new Date());
- // User user = new User();
- // BeanUtils.copyProperties(user, uaf);
- // user.setCreateDate(new Date());
- //调用业务逻辑操作
- UserManager.getInstance().addUser(user);
- return mapping.findForward("add_success");
- }
- /**
- * 用户删除
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- * @throws Exception
- */
- public ActionForward del(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- UserActionForm uaf = (UserActionForm)form;
- //取得需要删除的userId的集合
- String[] userIdList = uaf.getSelectFlag();
- //调用业务逻辑操作
- UserManager.getInstance().deleteUsers(userIdList);
- return mapping.findForward("del_success");
- }
- public ActionForward find(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- //获取从页面表单中提交过来的值
- UserActionForm uaf = (UserActionForm)form;
- String userId = uaf.getUserId();
- //调用业务逻辑操作
- User user = UserManager.getInstance().findUserById(userId);
- //将user对象从Action传递到JSP页面
- request.setAttribute("user", user);
- return mapping.findForward("find_success");
- }
- public ActionForward list(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- //调用业务逻辑操作
- List userList = UserManager.getInstance().findAllUserList();
- request.setAttribute("userlist", userList);
- return mapping.findForward("list_success");
- }
- public ActionForward modify(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- //获取从页面表单中提交过来的值
- UserActionForm uaf = (UserActionForm)form;
- // //创建User实体对象,并将ActionForm中的内容设置到User对象中
- // User user = new User();
- // user.setUserId(uaf.getUserId());
- // user.setUserName(uaf.getUserName());
- // user.setPassword(uaf.getPassword());
- // user.setEmail(uaf.getEmail());
- // user.setContactTel(uaf.getContactTel());
- // user.setCreateDate(new Date());
- User user = new User();
- BeanUtils.copyProperties(user, uaf);
- user.setCreateDate(new Date());
- //调用业务逻辑操作
- UserManager.getInstance().modifyUser(user);
- return mapping.findForward("modify_success");
- }
- }
Struts-config.xml文件的具体配置
- <struts-config>
- <form-beans>
- <form-bean name="userForm" type="com.bjsxt.drp.web.usermgr.forms.UserActionForm"></form-bean>
- </form-beans>
- <action-mappings>
- <action path="/user/usermiant"
- type="com.bjsxt.drp.web.usermgr.actions.UserAction"
- name="userForm"
- scope="request"
- parameter="command"
- >
- <forward name="add_success" path="/user/usermiant.do?command=list" redirect="true" />
- <forward name="del_success" path="/user/usermiant.do?command=list" redirect="true" />
- <forward name="find_success" path="/user/user_modify.jsp" />
- <forward name="list_success" path="/user/user_list.jsp" />
- <forward name="modify_success" path="/user/usermiant.do?command=list" redirect="true" />
- </action>
- </action-mappings>
- <controller processorClass="com.bjsxt.drp.CharacterEncode.MyRequestProcessor" />
- <message-resources parameter="MessageResources" />
- </struts-config>
页面调用时个具体
- <%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="gb2312"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
- <title>添加用户</title>
- <link rel="stylesheet" href="../style/drp.css">
- <script src="../script/client_validate.js"></script>
- <script type="text/javascript">
- function addUser() {
- if (document.getElementById("userId").value.length <4) {
- alert("用户代码不能小于4个字符!");
- document.getElementById("userId").focus();
- return;
- }
- var firstChar = document.getElementById("userId").value.charAt(0);
- if (!(firstChar >= 'a' && firstChar <='z')) {
- alert("用户代码首字符必须为字母");
- document.getElementById("userId").focus();
- return;
- }
- if(document.getElementById("userName").value == "") {
- alert("用户名称不能为空!");
- document.getElementById("userName").focus();
- return;
- }
- if (document.getElementById("password").value.length <6) {
- alert("输入的密码不能小于6位字符");
- document.getElementById("password").focus();
- return;
- }
- with (document.getElementById("userForm")) {
- method = "post";
- action = "usermiant.do?command=add";
- submit();
- }
- }
- function goBack() {
- window.self.location ="usermiant.do?command=list"
- }
- function init() {
- document.userForm.userId.focus();
- }
- </script>
- </head>
- <%
- request.setCharacterEncoding("gb2312");
- response.setCharacterEncoding("gb2312");
- %>
- <body class="body1">
- <form name="userForm" target="_self" id="userForm">
- <div align="center">
- <table width="95%" border="0" cellspacing="2" cellpadding="2">
- <tr>
- <td>
- </td>
- </tr>
- </table>
- <table width="95%" border="0" cellspacing="0" cellpadding="0"
- height="25">
- <tr>
- <td width="522" class="p1" height="25" nowrap>
- <img src="../images/mark_arrow_03.gif" width="14" height="14">
- <b>系统管理>>用户维护>>添加</b>
- </td>
- </tr>
- </table>
- <hr width="97%" align="center" size=0>
- <table width="95%" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td width="22%" height="29">
- <div align="right">
- <font color="#FF0000">*</font>用户代码:
- </div>
- </td>
- <td width="78%">
- <input name="userId" type="text" class="text1" id="userId"
- size="10" maxlength="10">
- </td>
- </tr>
- <tr>
- <td height="26">
- <div align="right">
- <font color="#FF0000">*</font>用户名称:
- </div>
- </td>
- <td>
- <input name="userName" type="text" class="text1" id="userName"
- size="20" maxlength="20">
- </td>
- </tr>
- <tr>
- <td height="26">
- <div align="right">
- <font color="#FF0000">*</font>密码:
- </div>
- </td>
- <td>
- <label>
- <input name="password" type="password" class="text1"
- id="password" size="20" maxlength="20">
- </label>
- </td>
- </tr>
- <tr>
- <td height="26">
- <div align="right">
- 联系电话:
- </div>
- </td>
- <td>
- <input name="contactTel" type="text" class="text1"
- id="contactTel" size="20" maxlength="20">
- </td>
- </tr>
- <tr>
- <td height="26">
- <div align="right">
- email:
- </div>
- </td>
- <td>
- <input name="email" type="text" class="text1" id="email"
- size="20" maxlength="20">
- </td>
- </tr>
- </table>
- <hr width="97%" align="center" size=0>
- <div align="center">
- <input name="btnAdd" class="button1" type="button" id="btnAdd"
- value="添加" onClick="addUser()">
- <input name="btnBack" class="button1" type="button" id="btnBack"
- value="返回" onclick="goBack()" />
- </div>
- </div>
- </form>
- </body>
- </html>
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- dispatchAction
- DispatchAction
- dispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- DispatchAction
- php 正则表达式
- 使用System.Diagnostics.Process,调用外部程序(.bat),并传入参数
- jQuery-parent(exp)用法 学习笔记九
- String.Join
- CentOS 5.0如何取得kernel source
- DispatchAction
- 老外写的一个javascript下Date格式化函数
- 何时买笔记本比较合适?
- 使用python做验证码图片
- String.Split()函数
- 程序人生--一个程序员对学弟学妹建议(转)
- 经常看到在一个大的电容上还并联一个小电容,这是为什么?
- 经常看到时钟线/数据线/地址线上串联一个小电阻有什么作用?
- 怎样理解阻抗匹配?