• 表单校验功能的完整实现步骤


    表单校验功能的实现步骤

    1在actionform中重写校验函数:

             public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

           // TODO Auto-generated method stub

           System.out.println("==validate begin==");

           ActionErrors errors=new ActionErrors();

           if(userName==null||userName.equals("")){

               errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionError("html.errors.error1"));

           }

           System.out.println("==validate end==");

           return errors;

        }

    将错误信息添加到一个全局变量ActionMessages.GLOBAL_MESSAGE中保存 在跳转的jsp页面捕获错误信息,只要errors对象中不为空就说明有错误即校验成功程序立即跳转到input指定页面不再执行action

    其中html.errors.error1是在配置文件中的一个键值对的key可以在配置文件中设置其value值。

    Struts-config.xml的配置信息:

        <action path="/test" name="userForm" type="com.inspur.controller.UserAction" parameter="method" scope="request"

        validate="true" input="/userTest.jsp"

       

        >

            <forward name="success" path="/success.jsp"></forward>

            <forward name="deleteSuccess" path="/deleteSuccess.jsp"></forward>

        </action>

    Input指定有错误后跳转到的页面,validate指定校验。

    在message-rescource标签中配置如下:

    <message-resources parameter="com.yourcompany.struts.ApplicationResources" key="error" />

       <message-resources key="" parameter="" />

    在捕获错误信息的页面源代码如下:

    首先导入标签库:<%@ taglib uri=" /tags/struts-html " prefix="html" %>

    捕获错误语句:<html:errors bundle="error"/>

    在导入标签库时对应的web.xml文件中的配置如下所示:

             <taglib>

           <taglib-uri>/tags/struts-html</taglib-uri>

           <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

        </taglib>

    这样就是可以在jsp页面中使用html:errors标签了 uri在web.xml中映射到/WEB-INF/struts-html.tld标签库。

    另外在添加<taglib>部分时在web.xml中会出错,此时应该删除<web-app>标签的所有版本信息仅保留<web-app></web-app>

    <message-resources parameter="com.yourcompany.struts.ApplicationResources" key="error" />

       <message-resources key="" parameter="" />中的参数key="error"对应到错误信息捕获部分的bundle="error"必须对应才能成功捕获错误信息

    在配置文件中:

    html.errors.error1=do not allow null是错误信息键值对,在配置文件中可以存在多组键值对,可以在对表单多个元素校验时分别校验并且获得错误信息。

     

  • 相关阅读:
    DLL注入实践
    程序编译保护机制
    加密配置文件总结
    PCL点云配准(3)
    PCL点云分割(3)
    ubuntu Ros环境halcon的一个程序
    opencv2/nonfree/nonfree.hpp:没有那个文件或目录
    深度图像计算三维点云
    利用深度学习的点云语义分割(一)
    3D 特征点概述(2)
  • 原文地址:https://www.cnblogs.com/moonfans/p/2716896.html
Copyright © 2020-2023  润新知