当前位置:编程学习 > XML/UML >>

基于XML配置方式实现输入校验

在JSP页面中导入<%@ taglib uri="/struts-tags" prefix="s" %>标签

在页面中显示信息用   验证的信息:<s:fielderror/>

在action中创建一个继承ActionSupport的UserInfoAction.java类

然后再action包下创建一个UserInfoAction-validation.xml

[html]
<SPAN style="FONT-SIZE: 14px"><?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">  
<validators> 
    <field name="txtName"> 
        <field-validator type="requiredstring"> 
            <param name="trim">true</param> 
            <message>用户名不能为空!</message> 
        </field-validator> 
    </field> 
    <field name="email"> 
        <field-validator type="requiredstring"> 
            <param name="trim">true</param> 
            <message>邮箱不能为空!</message> 
        </field-validator> 
        <field-validator type="email"> 
            <message>邮箱格式不争取</message> 
        </field-validator> 
    </field> 
    <field name="telePhone"> 
        <field-validator type="requiredstring"> 
            <param name="trim">true</param> 
            <message>电话不能为空</message> 
        </field-validator> 
        <field-validator type="regex"> 
            <param name="expression"><![CDATA[^1[358][0-9]{9}$]]></param> 
            <message>无效的电话</message> 
        </field-validator> 
    </field> 
</validators></SPAN> 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
<validators>
    <field name="txtName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>用户名不能为空!</message>
        </field-validator>
    </field>
    <field name="email">
     <field-validator type="requiredstring">
      <param name="trim">true</param>
      <message>邮箱不能为空!</message>
     </field-validator>
     <field-validator type="email">
      <message>邮箱格式不争取</message>
     </field-validator>
    </field>
    <field name="telePhone">
     <field-validator type="requiredstring">
      <param name="trim">true</param>
      <message>电话不能为空</message>
     </field-validator>
     <field-validator type="regex">
      <param name="expression"><![CDATA[^1[358][0-9]{9}$]]></param>
      <message>无效的电话</message>
     </field-validator>
    </field>
</validators>
在STRUTS.XML中配置

[html]
<SPAN style="FONT-SIZE: 14px"><?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
 <struts> 
    <package name="base" namespace="" extends="struts-default"> 
        <action name="user_*" method="{1}" class="action.UserInfoAction"> 
            <result name="ok">/index.jsp</result> 
            <result name="input">/index.jsp</result> 
        </action> 
    </package> 
</struts></SPAN> 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 <struts>
 <package name="base" namespace="" extends="struts-default">
  <action name="user_*" method="{1}" class="action.UserInfoAction">
   <result name="ok">/index.jsp</result>
   <result name="input">/index.jsp</result>
  </action>
 </package>
</struts>
 

 

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,