tapestry验证textarea
来源:互联网 发布:SQL如何删除默认约束 编辑:程序博客网 时间:2024/05/20 15:53
tapestry验证textarea
ValidTextArea.java:
java代码:
package com.tomxp.web.components.valid;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.form.TextArea;
import org.apache.tapestry.valid.IValidationDelegate;
import org.apache.tapestry.valid.IValidator;
import org.apache.tapestry.valid.ValidatorException;
/**
* @author Peter Butler
*/
public abstract class ValidTextArea extends TextArea {
WriterErrorDecorator myWriter = new WriterErrorDecorator(this);
public ValidTextArea() {
super();
}
public abstract IValidator getValidator();
public abstract String getDisplayName();
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
IForm form = getForm(cycle);
IValidationDelegate delegate = form.getDelegate();
if (delegate == null)
throw new ApplicationRuntimeException(Tapestry.format("ValidTextArea.no-delegate", getExtendedId(), getForm().getExtendedId()), this, null, null);
IValidator validator = getValidator();
if (validator == null)
throw Tapestry.createRequiredParameterException(this, "validator");
boolean rendering = !cycle.isRewinding();
IMarkupWriter superWriter;
if (rendering) {
delegate.writePrefix(writer, cycle, this, validator);
myWriter.init(writer, delegate, validator, cycle);
superWriter = myWriter;
} else {
superWriter = writer;
}
super.renderComponent(superWriter, cycle);
String name = getName();
if (!rendering && !isDisabled()) {
String value = cycle.getRequestContext().getParameter(name);
updateValue(value);
}
if (rendering) {
delegate.writeSuffix(writer, cycle, this, validator);
myWriter.clear();
}
}
protected void updateValue(String value) {
Object objectValue = null;
IValidationDelegate delegate = getForm().getDelegate();
delegate.recordFieldInputValue(value);
try {
objectValue = getValidator().toObject(this, value);
} catch (ValidatorException ex) {
delegate.record(ex);
return;
}
setValue((String)objectValue);
}
}
ValidTextArea.jwc
java代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE component-specification
PUBLIC "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<!-- generated by Spindle, http://spindle.sourceforge.net -->
<component-specification class="com.tomxp.web.components.valid.ValidTextArea" allow-body="no">
<description>
A multi-line valid text area.
</description>
<parameter name="value" type="java.lang.String" required="yes" direction="form"/>
<parameter name="disabled" type="boolean" direction="in"/>
<parameter name="validator" type="org.apache.tapestry.valid.IValidator" required="yes" direction="in">
<description>
Converts value to a string and parses strings back into object values.
</description>
</parameter>
<parameter name="displayName" type="java.lang.String" required="yes" direction="auto">
<description>
Name used by FieldLabel and when generating validation error messages.
</description>
</parameter>
<reserved-parameter name="name"/>
<property-specification name="name" type="java.lang.String"/>
<property-specification name="form" type="org.apache.tapestry.IForm"/>
</component-specification>
java代码:
package com.tomxp.web.components.valid;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.IValidationDelegate;
import org.apache.tapestry.valid.IValidator;
/**
* @author Peter Butler
*/
public class WriterErrorDecorator implements IMarkupWriter {
private IMarkupWriter actual;
private IValidationDelegate delegate;
private IValidator validator;
private IRequestCycle cycle;
private IFormComponent field;
public WriterErrorDecorator(IFormComponent field) {
this.field = field;
}
public void init(IMarkupWriter actual, IValidationDelegate delegate, IValidator validator, IRequestCycle cycle) {
this.actual = actual;
this.delegate = delegate;
this.validator = validator;
this.cycle = cycle;
}
public void clear() {
this.actual = null;
this.delegate = null;
this.validator = null;
this.cycle = null;
}
public void attribute(String name, int value) {
actual.attribute(name, value);
}
public void attribute(String name, boolean value) {
actual.attribute(name, value);
}
public void attribute(String name, String value) {
actual.attribute(name, value);
}
public void attributeRaw(String name, String value) {
actual.attributeRaw(name, value);
}
public void begin(String name) {
actual.begin(name);
validator.renderValidatorContribution(field, this, cycle);
delegate.writeAttributes(this, cycle, field, validator);
}
public void beginEmpty(String name) {
actual.beginEmpty(name);
validator.renderValidatorContribution(field, this, cycle);
delegate.writeAttributes(this, cycle, field, validator);
}
public boolean checkError() {
return actual.checkError();
}
public void close() {
actual.close();
}
public void closeTag() {
actual.closeTag();
}
public void comment(String value) {
actual.comment(value);
}
public void end() {
actual.end();
}
public void end(String name) {
actual.end(name);
}
public void flush() {
actual.flush();
}
public IMarkupWriter getNestedWriter() {
return actual.getNestedWriter();
}
public void print(char[] data, int offset, int length) {
actual.print(data, offset, length);
}
public void print(char value) {
actual.print(value);
}
public void print(int value) {
actual.print(value);
}
public void print(String value) {
actual.print(value);
}
public void println() {
actual.println();
}
public void printRaw(char[] buffer, int offset, int length) {
actual.printRaw(buffer, offset, length);
}
public void printRaw(String value) {
actual.printRaw(value);
}
public String getContentType() {
return actual.getContentType();
}
}
对于TextArea的长度验证,的确只有自己写组件。特别是当页面中已经使用Tapestry的FieldLabel+Validator效验的时候,如果仅仅是简单的将JS写在HTML里面,就会出现无法于Tapestry动态生成的JavaScript整合的问题。。。
我看了一下Ewaves贴的代码,似乎没有贴出.script文件。我自己写的组件虽然和上面的代码完全不一样,但是.script文件却是可以通用的,它能够自动生成动态的javascript。
java代码:
<?xml version="1.0" encoding="GBK"?>
<!-- $Id: DatePicker.script,v 1.5 2004/03/09 18:23:50 hlship Exp $ -->
<!DOCTYPE script PUBLIC
"-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
<script>
<!--
input symbols:
textAreaScript: The TextAreaScript component.
output symbols:
formValidateFunction The name of a form validator function that validates the fields.
-->
<include-script resource-path="/org/apache/tapestry/valid/Validator.js"/>
<input-symbol key="textAreaScript" class="com.cdmcs.tapestry.jwc.TextAreaScript"/>
<set key="name" expression="textAreaScript.name"/>
<set key="required" expression="textAreaScript.required"/>
<set key="minimumLength" expression="textAreaScript.minimumLength"/>
<set key="maximumLength" expression="textAreaScript.maximumLength"/>
<set key="requiredMessage" expression="textAreaScript.requiredMessage"/>
<set key="minimumLengthMessage" expression="textAreaScript.minimumLengthMessage"/>
<set key="maximumLengthMessage" expression="textAreaScript.maximumLengthMessage"/>
<set key="numberFormatEnable" expression="textAreaScript.numberFormatEnable"/>
<set key="numberLengthMessage" expression="textAreaScript.numberLengthMessage"/>
<set key="numberLength" expression="textAreaScript.numberLength"/>
<let key="baseName">
document.${textAreaScript.form.name}.${name}
</let>
<let key="formValidateFunction" unique="yes">
validate_${name}
</let>
<body>
<![CDATA[
function ${formValidateFunction}()
{
var field = ${baseName};
var keycode = window.event.keyCode;
if ( ${numberFormatEnable} && !field.value.match(/^/d{11}$/) )
{
if( field.value.length == 0 )
return true;
return validator_invalid_field(field, "${numberLengthMessage}");
}
if ( ${required} && field.value.length==0 )
{
return validator_invalid_field(field, "${requiredMessage}");
}
if ( ${required} && field.value.length<${minimumLength})
{
return validator_invalid_field(field, "${minimumLengthMessage}");
}
if ( ${required} && field.value.length>${maximumLength})
{
return validator_invalid_field(field, "${maximumLengthMessage}");
}
return true;
}
]]>
</body>
</script>
我这段代码另外还有个功能,是效验11位手机号码,因为11的长度远大于Tapestry提动的NumberValidator范围,没办法,只好自己写了,想偷懒都不行。
ValidTextArea.java:
java代码:
package com.tomxp.web.components.valid;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.form.TextArea;
import org.apache.tapestry.valid.IValidationDelegate;
import org.apache.tapestry.valid.IValidator;
import org.apache.tapestry.valid.ValidatorException;
/**
* @author Peter Butler
*/
public abstract class ValidTextArea extends TextArea {
WriterErrorDecorator myWriter = new WriterErrorDecorator(this);
public ValidTextArea() {
super();
}
public abstract IValidator getValidator();
public abstract String getDisplayName();
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
IForm form = getForm(cycle);
IValidationDelegate delegate = form.getDelegate();
if (delegate == null)
throw new ApplicationRuntimeException(Tapestry.format("ValidTextArea.no-delegate", getExtendedId(), getForm().getExtendedId()), this, null, null);
IValidator validator = getValidator();
if (validator == null)
throw Tapestry.createRequiredParameterException(this, "validator");
boolean rendering = !cycle.isRewinding();
IMarkupWriter superWriter;
if (rendering) {
delegate.writePrefix(writer, cycle, this, validator);
myWriter.init(writer, delegate, validator, cycle);
superWriter = myWriter;
} else {
superWriter = writer;
}
super.renderComponent(superWriter, cycle);
String name = getName();
if (!rendering && !isDisabled()) {
String value = cycle.getRequestContext().getParameter(name);
updateValue(value);
}
if (rendering) {
delegate.writeSuffix(writer, cycle, this, validator);
myWriter.clear();
}
}
protected void updateValue(String value) {
Object objectValue = null;
IValidationDelegate delegate = getForm().getDelegate();
delegate.recordFieldInputValue(value);
try {
objectValue = getValidator().toObject(this, value);
} catch (ValidatorException ex) {
delegate.record(ex);
return;
}
setValue((String)objectValue);
}
}
ValidTextArea.jwc
java代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE component-specification
PUBLIC "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<!-- generated by Spindle, http://spindle.sourceforge.net -->
<component-specification class="com.tomxp.web.components.valid.ValidTextArea" allow-body="no">
<description>
A multi-line valid text area.
</description>
<parameter name="value" type="java.lang.String" required="yes" direction="form"/>
<parameter name="disabled" type="boolean" direction="in"/>
<parameter name="validator" type="org.apache.tapestry.valid.IValidator" required="yes" direction="in">
<description>
Converts value to a string and parses strings back into object values.
</description>
</parameter>
<parameter name="displayName" type="java.lang.String" required="yes" direction="auto">
<description>
Name used by FieldLabel and when generating validation error messages.
</description>
</parameter>
<reserved-parameter name="name"/>
<property-specification name="name" type="java.lang.String"/>
<property-specification name="form" type="org.apache.tapestry.IForm"/>
</component-specification>
java代码:
package com.tomxp.web.components.valid;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.valid.IValidationDelegate;
import org.apache.tapestry.valid.IValidator;
/**
* @author Peter Butler
*/
public class WriterErrorDecorator implements IMarkupWriter {
private IMarkupWriter actual;
private IValidationDelegate delegate;
private IValidator validator;
private IRequestCycle cycle;
private IFormComponent field;
public WriterErrorDecorator(IFormComponent field) {
this.field = field;
}
public void init(IMarkupWriter actual, IValidationDelegate delegate, IValidator validator, IRequestCycle cycle) {
this.actual = actual;
this.delegate = delegate;
this.validator = validator;
this.cycle = cycle;
}
public void clear() {
this.actual = null;
this.delegate = null;
this.validator = null;
this.cycle = null;
}
public void attribute(String name, int value) {
actual.attribute(name, value);
}
public void attribute(String name, boolean value) {
actual.attribute(name, value);
}
public void attribute(String name, String value) {
actual.attribute(name, value);
}
public void attributeRaw(String name, String value) {
actual.attributeRaw(name, value);
}
public void begin(String name) {
actual.begin(name);
validator.renderValidatorContribution(field, this, cycle);
delegate.writeAttributes(this, cycle, field, validator);
}
public void beginEmpty(String name) {
actual.beginEmpty(name);
validator.renderValidatorContribution(field, this, cycle);
delegate.writeAttributes(this, cycle, field, validator);
}
public boolean checkError() {
return actual.checkError();
}
public void close() {
actual.close();
}
public void closeTag() {
actual.closeTag();
}
public void comment(String value) {
actual.comment(value);
}
public void end() {
actual.end();
}
public void end(String name) {
actual.end(name);
}
public void flush() {
actual.flush();
}
public IMarkupWriter getNestedWriter() {
return actual.getNestedWriter();
}
public void print(char[] data, int offset, int length) {
actual.print(data, offset, length);
}
public void print(char value) {
actual.print(value);
}
public void print(int value) {
actual.print(value);
}
public void print(String value) {
actual.print(value);
}
public void println() {
actual.println();
}
public void printRaw(char[] buffer, int offset, int length) {
actual.printRaw(buffer, offset, length);
}
public void printRaw(String value) {
actual.printRaw(value);
}
public String getContentType() {
return actual.getContentType();
}
}
对于TextArea的长度验证,的确只有自己写组件。特别是当页面中已经使用Tapestry的FieldLabel+Validator效验的时候,如果仅仅是简单的将JS写在HTML里面,就会出现无法于Tapestry动态生成的JavaScript整合的问题。。。
我看了一下Ewaves贴的代码,似乎没有贴出.script文件。我自己写的组件虽然和上面的代码完全不一样,但是.script文件却是可以通用的,它能够自动生成动态的javascript。
java代码:
<?xml version="1.0" encoding="GBK"?>
<!-- $Id: DatePicker.script,v 1.5 2004/03/09 18:23:50 hlship Exp $ -->
<!DOCTYPE script PUBLIC
"-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
<script>
<!--
input symbols:
textAreaScript: The TextAreaScript component.
output symbols:
formValidateFunction The name of a form validator function that validates the fields.
-->
<include-script resource-path="/org/apache/tapestry/valid/Validator.js"/>
<input-symbol key="textAreaScript" class="com.cdmcs.tapestry.jwc.TextAreaScript"/>
<set key="name" expression="textAreaScript.name"/>
<set key="required" expression="textAreaScript.required"/>
<set key="minimumLength" expression="textAreaScript.minimumLength"/>
<set key="maximumLength" expression="textAreaScript.maximumLength"/>
<set key="requiredMessage" expression="textAreaScript.requiredMessage"/>
<set key="minimumLengthMessage" expression="textAreaScript.minimumLengthMessage"/>
<set key="maximumLengthMessage" expression="textAreaScript.maximumLengthMessage"/>
<set key="numberFormatEnable" expression="textAreaScript.numberFormatEnable"/>
<set key="numberLengthMessage" expression="textAreaScript.numberLengthMessage"/>
<set key="numberLength" expression="textAreaScript.numberLength"/>
<let key="baseName">
document.${textAreaScript.form.name}.${name}
</let>
<let key="formValidateFunction" unique="yes">
validate_${name}
</let>
<body>
<![CDATA[
function ${formValidateFunction}()
{
var field = ${baseName};
var keycode = window.event.keyCode;
if ( ${numberFormatEnable} && !field.value.match(/^/d{11}$/) )
{
if( field.value.length == 0 )
return true;
return validator_invalid_field(field, "${numberLengthMessage}");
}
if ( ${required} && field.value.length==0 )
{
return validator_invalid_field(field, "${requiredMessage}");
}
if ( ${required} && field.value.length<${minimumLength})
{
return validator_invalid_field(field, "${minimumLengthMessage}");
}
if ( ${required} && field.value.length>${maximumLength})
{
return validator_invalid_field(field, "${maximumLengthMessage}");
}
return true;
}
]]>
</body>
</script>
我这段代码另外还有个功能,是效验11位手机号码,因为11的长度远大于Tapestry提动的NumberValidator范围,没办法,只好自己写了,想偷懒都不行。
- tapestry验证textarea
- Tapestry 数据验证
- Tapestry 验证用户名
- Tapestry 数据验证
- Tapestry
- Tapestry
- Tapestry
- Tapestry
- Tapestry
- Tapestry
- Tapestry
- Tapestry
- tapestry
- Tapestry
- Tapestry
- Tapestry 表单输入与验证,表单数据失败
- javascript通用验证码,textarea及示例
- jQuery对textarea的长度进行验证
- 转贴:Mark Russinovich的Inside Vista Kernel系列文章,讲到了Vista内核的调度,IO,内存管理,缓存,事务处理,安全等众多新特性
- Tapestry数据校验Validator
- SLAMDUNK
- Tapestry的函数的执行顺序 [转]
- PropertySelection组件如何设置的value初始值
- tapestry验证textarea
- 两个路由器上网
- 开始使用javac java
- Jbpm表结构说明
- OpenCms JSP 模板开发——创建一个含有多个可编辑元素的JSP模板
- Tapestry 表单输入与验证,表单数据失败
- 平面设计、网页设计必备的Gadget--边栏取色器
- Gadget汉化教程
- 一分钟学会制作边栏游戏