一个参数多个动作的action
                            
LoginAction.java
package hb;

import java.util.List;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

import javax.servlet.ServletContext;
import javax.sql.DataSource;
import javax.servlet.http.*;

public final class LoginAction extends Action{  
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws HibernateException {


   UserForm userform = (UserForm) form;        
String name = userform.getName();

HttpSession session = request.getSession(true);

String action=request.getParameter("action"); //动作类型

List list=null;

session.setAttribute(
"name",name);  

if(action==null)action=
"login";

if(
"login".equals(action)){ //登录操作
  if(UserDAO.chkUserName(name)>0){

   return (mapping.findForward(
"successed"));//登录成功
  }
  
}

   if(
"addUser".equals(action)){ //增加记录操作
  
         UserDAO userDao=new UserDAO();
         userDao.saveName(name);

   return (mapping.findForward(
"successed"));

  }
  
   if(
"getUser".equals(action)){ //取出记录操作
  
         UserDAO userDao=new UserDAO();
         list=userDao.getGbooks();
             session.setAttribute(
"userList",list);
   return (mapping.findForward(
"successed"));//取出成功

}   

      
// /* else {
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("label.deny"));

if (!errors.isEmpty()) {
saveErrors(request, errors);
//request.setAttribute("error",errors);
}
   return  mapping.findForward("failed");  //失败
// }*/


  
// return  mapping.findForward("failed");   }
}
静态的actionForm
UserForm.java
package hb;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;


public class UserForm extends ActionForm{  
private String name = null;
//用户名  
public UserForm(){}
public void setName(String name) {
this.name = name;
}  
public String getName() {
return name;
}  


public void reset(ActionMapping mapping,
HttpServletRequest request) {
this.name = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((name == null) || (name.equals(
""))){
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage(
"error.name.required"));
}

return errors;
}


}

    rs.GLOBAL_MESSAGE,
new ActionMessage(
"error.name.required"));
}

return errors;
}


}
lunzi   2006-05-23 23:40:10 评论:6   阅读:2435   引用:0
已经用了 @2006-11-05 23:46:05  lunzi
早用过了,确实好用,思路清晰,现在学习一种新框架WebWork结构更清晰,大家不妨学学看
DispatchActionTW @2006-11-05 11:57:16  游客
这种做法不好,应该用struts 1.1开始支持的DispatchAction, 一个action对应多个method,条理清楚,可维护性好。

        是的,俺现在也用很多的method,方便,维护也来也更方便,何乐不为呢
@2006-11-05 11:56:55  DispatchActionTOO
这种做法不好,应该用struts 1.1开始支持的DispatchAction, 一个action对应多个method,条理清楚,可维护性好。

        是的,俺现在也用很多的method,方便,维护也来也更方便,何乐不为呢
好的 @2006-05-25 17:15:05  lunzi
我的书上提到过,还没用过.我试一下吧
DispatchAction @2006-05-24 11:52:49  hofman
这种做法不好,应该用struts 1.1开始支持的DispatchAction, 一个action对应多个method,条理清楚,可维护性好。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog