第三十七讲:tapestry Ajax 表单(form)多zone组件

/**
 * 
 */
package corner.app.pages.ajax;

import java.util.Date;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.DateField;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;

/**
 * @author <a href="mailto:xf@bjmaxinfo.com">xiafei</a>
 * 
 */
public class AJAXFormMultipleZoneUpdate {

	// Screen fields

	@Property
	private String firstName;

	@Property
	private String lastName;

	@Property
	private Date birthday;

	// Generally useful bits and pieces

	@Component
	private Form ajaxForm;

	@Component(id = "firstName")
	private TextField firstNameField;

	@Component(id = "lastName")
	private TextField lastNameField;

	@Component(id = "birthday")
	private DateField birthdayField;

	@InjectComponent
	private Zone formZone;

	@InjectComponent
	private Zone outZone;

	@Inject
	private Request request;

	@Inject
	private AjaxResponseRenderer ajaxResponseRenderer;

	// The code

	void setupRender() {
		if (firstName == null && lastName == null && birthday == null) {
			firstName = "Humpty";
			lastName = "Dumpty";
			birthday = new Date(0);
		}
	}

	void onValidateFromAjaxForm() {

		if (firstName == null || firstName.trim().equals("")) {
			ajaxForm.recordError(firstNameField, "First Name is required.");
		}
		if (lastName == null || lastName.trim().equals("")) {
			ajaxForm.recordError(lastNameField, "Last Name is required.");
		}
		if (birthday == null) {
			ajaxForm.recordError(birthdayField, "Birthday is required.");
		} else {
			Date now = new Date();
			if (birthday.after(now)) {
				ajaxForm.recordError(birthdayField,
						"Birthday must be in the past.");
			}
		}
	}

	void onSuccess() {
		if (request.isXHR()) {
			ajaxResponseRenderer.addRender(formZone).addRender(outZone);
		}
	}

	void onFailure() {
		if (request.isXHR()) {
			ajaxResponseRenderer.addRender(formZone);
		}
	}

	public String getName() {
		return firstName + " " + lastName;
	}

	public Date getTime() {
		return new Date();
	}
}

<html t:type="layout" title="tapestryStart Index"  t:sidebarTitle="Framework Version" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<h1>AJAX Form Multiple Zone Update</h1>

	<noscript>
		<span style="color: red;">
			${message:javascript_required}<br/><br/>
		</span> 
	</noscript> 	

	This page demonstrates an AJAX-enabled Form that also updates another zone on success.<br/><br/>
	
	<t:zone t:id="formZone" id="formZone">
		<form t:id="ajaxForm" t:type="form" t:zone="formZone" style="border: 2px solid #eee; padding: 15px">
		
			First Name: <input t:type="TextField" t:id="firstName"/><br/>
			Last Name: <input t:type="TextField" t:id="lastName"/><br/>
			Birthday: <input t:type="datefield" t:id="birthday"/><br/><br/>
			
			<input type="submit" value="Accept"/><br/><br/>
			
			<t:errors/>
			
	 	</form>
 	</t:zone>
 	
 	<t:zone t:id="outZone" id="outZone" style="border: 2px solid #eee; padding: 15px">
		Welcome ${name}. Your birthday is ${birthday}
	</t:zone>
</html>

 

转载于:https://my.oschina.net/shootercn/blog/55511

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值