首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

struts2 xml 校验 初记

2013-07-11 
struts2 xml 校验 小记struts的配置文件xml校验有很多前辈都说过了,我这里就不赘述了[java]action代码pack

struts2 xml 校验 小记

struts的配置文件xml校验有很多前辈都说过了,我这里就不赘述了

[java]

action代码

package com.koolsee.cdn.action;import java.util.HashMap;import java.util.Map;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.rest.DefaultHttpHeaders;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import com.koolsee.cdn.service.SystemConfigService;import com.opensymphony.xwork2.ModelDriven;@Controller@Results({@Result(name = "success", type = "redirectAction", params = {"actionName", "system-config"}),        @Result(name = "input", type = "redirectAction", params = {"actionName", "system-config"})})public class SystemConfigAction extends BaseAction implements ModelDriven<Map<String, Object>> {    /**     *      */    private static final long serialVersionUID = 1L;    private Map<String, Object> systemConfigMap = new HashMap<String, Object>();    @Autowired    private SystemConfigService systemConfigService;    private Integer id;    /**     * 进入系统参数配置主页面     * @return DefaultHttpHeaders     * @throws Exception     * @see [类、类#方法、类#成员]     * @since [产品/模块版本]     */    public DefaultHttpHeaders edit() throws Exception {        this.systemConfigMap.clear();        this.systemConfigMap = this.systemConfigService.selectAll();        return new DefaultHttpHeaders(EDIT).disableCaching();    }    /**     * 修改系统参数配置信息     * @return String     * @throws Exception     * @see [类、类#方法、类#成员]     * @since [产品/模块版本]     */    public DefaultHttpHeaders update() throws Exception {        this.systemConfigService.updateSystemConfig(this.systemConfigMap);        this.addActionMessage("系统配置信息修改成功");        return this.edit();    }    public Integer getId() {        return this.id;    }    public void setId(Integer id) {        this.id = id;    }    @Override    public Map<String, Object> getModel() {        return this.systemConfigMap;    }}

校验的 xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC         "-//Apache Struts//XWork Validator 1.0//EN"        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">        <validators><!-- Field-Validator Syntax --><!--邮件服务器域名 --><field name="MAILSERVERDOMAIN[0]"><field-validator type="requiredstring">            <param name="trim">true</param>            <message key="messages.system.config.mailserverdomain.required"></message>        </field-validator>        <field-validator type="stringlength">            <param name="maxLength">1024</param>            <message key="messages.system.config.mailserverdomain.length"></message>        </field-validator></field><!-- 邮箱 -->    <field name="SERVERMAIL[0]">        <field-validator type="requiredstring">            <param name="trim">true</param>            <message key="messages.system.config.servermail.required"></message>        </field-validator>         <field-validator type="email">            <message key="messages.system.config.servermail.check"></message>        </field-validator>        <field-validator type="stringlength">            <param name="maxLength">1024</param>            <message key="messages.system.config.servermail.length"></message>        </field-validator>    </field>        <!--服务器邮箱账号 --><field name="SERVERMAILACC[0]"><field-validator type="requiredstring">            <param name="trim">true</param>            <message key="messages.system.config.servermailacc.required"></message>        </field-validator>        <field-validator type="stringlength">            <param name="maxLength">1024</param>            <message key="messages.system.config.servermailacc.length"></message>        </field-validator></field><!--服务器邮箱密码 --><field name="SERVERMAILPWD[0]"><field-validator type="requiredstring">            <param name="trim">true</param>            <message key="messages.system.config.servermailpwd.required"></message>        </field-validator>        <field-validator type="stringlength">            <param name="maxLength">1024</param>            <message key="messages.system.config.servermailpwd.length"></message>        </field-validator></field></validators>
?

对应的需要校验的jsp

?

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" autoFlush="true"%><%@taglib uri="/struts-tags" prefix="s"%><%@ taglib prefix="ks" uri="http://www.koolsee.com/el/functions" %><!DOCTYPE html><html><head><title><s:text name="messages.system.config.edit"/></title><script type="text/javascript"> $(document).ready(function(){ jQuery("#k_form").submit(function(){ var encryptChecked = jQuery("#mailIsEncryptChk").attr("checked"); var verifyChecked = jQuery("#mailIsVerifyChk").attr("checked");  if(encryptChecked && encryptChecked == "checked"){ jQuery("#mailIsEncrypt").val("1"); }else{ jQuery("#mailIsEncrypt").val("0"); } if(verifyChecked && verifyChecked == "checked"){ jQuery("#mailIsVerify").val("1"); }else{ jQuery("#mailIsVerify").val("0"); } }); });</script></head><body><div><ul method="post" id="k_form" action="%{basePath}/system-config/0">   <s:hidden name="_method" value="put" /><fieldset><legend>普通数据</legend><div name="MAILSERVERDOMAIN" maxlength="1024"></s:textfield><span name="SERVERMAIL" maxlength="1024"></s:textfield><span name="SERVERMAILACC" maxlength="1024"></s:textfield><span name="SERVERMAILPWD" maxlength="1024"></s:textfield><span name="MAILISENCRYPT" ></s:hidden><s:if test="#attr.MAILISENCRYPT==1"><input type="checkbox" id="mailIsEncryptChk" checked="checked"/><s:text name="messages.system.config.mailisencrypt"/></s:if><s:else><input type="checkbox" id="mailIsEncryptChk" /><s:text name="messages.system.config.mailisencrypt"/></s:else></div></div><div name="MAILISVERIFY" ></s:hidden><s:if test="#attr.MAILISVERIFY==1"><input type="checkbox" id="mailIsVerifyChk" checked="checked" /><s:text name="messages.system.config.mailisverify"/></s:if><s:else><input type="checkbox" id="mailIsVerifyChk" /><s:text name="messages.system.config.mailisverify"/></s:else></div></div><div value="<s:text name='messages.save' />" /></div>    </fieldset></s:form>   </div></body></html>

?特别注意这里的 <field name="MAILSERVERDOMAIN[0]"> <field name="SERVERMAIL[0]"> <field name="SERVERMAILACC[0]"> <field name="SERVERMAILPWD[0]">;因为在action中ModelDriven<Map<String, Object>> 模型驱动中加载的的是MAP,所以在加载数据的是以数据的形式赋值给相关的KEY的,即我们验证取值的时候验证的是[0];页面上取验证信息的时候当然也是根据field 的name属性值来取的,即 如:<s:fielderror><s:param>MAILSERVERDOMAIN[0]</s:param></s:fielderror>

?

当模型驱动加载的是Map的时候,struts2 的xml验证实例述说完毕,随笔,欢迎各位大牛批评指正,邮箱:xulonghui2011@163.com

顺便提一下,本人网店 http://shop104105266.taobao.com 店名叫"真惠充值" 话费 Q币Q点 游戏点卡充值 有空记得收藏 光顾哦

?

?

?

热点排行