Struts的html:errors的用法

本文介绍Struts框架中如何配置表单验证,包括在struts-config.xml中设置验证规则,利用ActionForm的validate方法实现字段校验,并展示如何在Action中进行二次验证及错误信息的返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 在struts-config.xml必须设置需要验证的ActionForm
    <action
    attribute="findSellForm"
    validate="true"
    input="/index.jsp"
    name="findSellForm"
    path="/findSell"
    scope="request"
    type="ucshop.action.FindSellAction" >
    <forward name="fail" path="/index.jsp" />
    <forward name="success" path="/findsell.jsp" />
    </action>

    validate默认值是true的,所以可以省略;如果不想验证,则设为false,这样的话,后面的就不用看了!

    input要指明,检验流程:当执行validate方法时,返回的ActionErrors检查内部是否存在元素,有则返回到原来的input指定的页面;否则继续前进,执行Action的execute方法!到时候forward的fail和success才起作用!

  2. JSP页面
    <html:form action="/findSell">
    物品分类: <html:select property="productType">
    <html:option value="电脑配件"/>
    <html:option value="数码产品"/>
    <html:option value="运动用品"/>
    <html:option value="生活用品"/>
    <html:option value="户外用品"/>
    <html:option value="其他"/>
    </html:select>
    <br/><br/>
    物品名称: <html:text property="name"/><html:errors property="sellname"/>
    <br/><br/>
    <html:submit value="搜索"/>
    </html:form>

    注意: 加蓝的语句中property的值sellname是对应下面的validate()方法中的"errors.add("sellname", new ActionMessage("errors.productNameEmpty")); "的selllname!
  3. ActionForm的validate
    下面的代码是struts的1.2版本的:
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
    //创建ActionErrors
    ActionErrors errors = new ActionErrors();

    //物品名称的检验
    if (getName() == null || getName().trim().equals(""))
    {
    errors.add("sellname", new ActionMessage("errors.productNameEmpty"));
    }

    //返回ActionErrors
    return errors;
    }
    自struts1.2, ActionError类都Deprecated,不再赞成使用,官方建议使用ActionMessage类,所以在struts1.2以下版本的,上面的"errors.add("sellname", new ActionMessage("errors.productNameEmpty")); "改为errors.add("sellname", new ActionError("errors.productNameEmpty"));
    同时ActionErrors的GLOBAL_ERROR 被Deprecated,被ActionMessages.GLOBAL_MESSAGE 代替!
    ========================================
    有时候需要在Action的execute()方法里面进行例如权限的验证.
    举个例子,validate()是检查用户名和密码数据是否为空,当用户输入了完整信息后,执行execute(),这时若发现"用户名或密码有误",,就需要显示错误信息
    public class ServerValidationAction extends Action {
    public ActionForward execute(ActionMapping actionMapping,
    ActionForm actionForm, HttpServletRequest httpServletRequest,
    HttpServletResponse httpServletResponse) {

    ServerValidationActionForm form = (ServerValidationActionForm) actionForm;

    if (!form.getPassword().equals("password")) {
    ActionErrors errors = new ActionErrors();
    errors.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(
    "errors.passwordwrong"));
    saveErrors(httpServletRequest, errors);
    return new ActionForward(actionMapping.getInput());


    }
    return actionMapping.findForward("success");
    }
    }
  4. WEB-INF文件夹的classes目录下创建一个文件夹xx,添加一个名为gb_ApplicationResources.properties的文件在xx文件夹里面,内容
    errors.productNameEmpty=物品名称必须要填写
    等于号左边的内容名字就是new AcionMessage("erors.productNameEmpty"));的value
    创建文件内容为
    native2ascii gb_ApplicationResources.properties ApplicationResources.properties
    保存为native2ascii java.bat 注意,不要保存为native2ascii.bat,否则会死循环!同时这个文件和gb_ApplicationResources.properties在同一目录!
    然后双击native2ascii java.bat,一会儿就会生成一个名为ApplicationResources.properties的文件!
  5. struts-config.xml添加<message-resources parameter="xx.ApplicationResources"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值