Action 中返回ActionMessages

本文介绍了一个基于Struts框架的登录验证Action实现,该Action通过Hibernate进行数据库查询来验证用户名及密码是否正确,并根据验证结果转向不同的页面。

package com.sincole.struts.action;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;
import org.apache.struts.validator.DynaValidatorForm;
import com.sincole.hibernate.*;
/** 
 * Creation date: 03-08-2006
 *
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
 * @struts.action-forward name="success" path="/mainFrame.jsp" redirect="true"
 * @struts.action-forward name="failure" path="/login.jsp"
 */
public class LoginAction extends Action {

 // --------------------------------------------------------- Instance Variables

 // --------------------------------------------------------- Methods

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {
  DynaValidatorForm loginForm = (DynaValidatorForm) form;
  String UserName = loginForm.getString("username");
  String Password = loginForm.getString("password");
  //TUser user;
  HDao dao = new HDao();
  List list= dao.find("from TUser where username='" + UserName + "' and password=" + Password);
  Iterator ite = list.iterator();
  if(ite.hasNext())
      return mapping.findForward("success");
  else{
   ActionMessages msgs = new ActionMessages();
   ActionMessage msg = new ActionMessage("errors.loginfailed");
   msgs.add("loginfailed",msg);
      saveErrors(request,msgs);
   return mapping.findForward("failure");
  }
 }
}

struts 1.2.8 中:
ActionErrors 被 ActionMessages 代替 ,ActionError 相应被 ActionMessage 代替
最后使用 saveErrors(HttpServletRequest ,ActionMessages) 方法保存

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值