Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_cn
Articles_de
Articles_pt
Articles_zh
TapestryPages




JSPWiki v2.2.33

[RSS]


Hide Menu

ValidationAndListTapestry


Difference between version 15 and version 14:

At line 21 changed 1 line.
Implementing validation with Tapestry is quite simple. Rather than using a [TextField|http://jakarta.apache.org/tapestry/doc/ComponentReference/TextField.html] component, you use a [ValidField|http://jakarta.apache.org/tapestry/doc/ComponentReference/ValidField.html] components. The validation integration for the personForm.html page is already done by AppGen in the personForm.page. If you open web/pages/personForm.page, you should see the following XML. I've highlighted the portions that are relevant to validation.
Implementing validation with Tapestry is quite simple. All you need to do is configure a "validators" property on your [TextField|http://jakarta.apache.org/tapestry/doc/ComponentReference/TextField.html] components. The validation integration for the personForm.html page is already done by AppGen in the personForm.page. If you open web/pages/personForm.page, you should see the following XML. I've highlighted the portions that are relevant to validation.
At line 24 changed 5 lines.
<?xml version="1.0"?>
<!DOCTYPE page-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE page-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
At line 30 added 5 lines.
<inject property="engineService" object="engine-service:page"/>
<inject property="request" object="service:tapestry.globals.HttpServletRequest"/>
<inject property="response" object="service:tapestry.globals.HttpServletResponse"/>
<inject property="personManager" type="spring" object="personManager"/>
At line 31 changed 25 lines.
<span style="background-color: yellow">&lt;bean name="requiredValidator" class="org.apache.tapestry.valid.StringValidator"&gt;</span>
<span style="background-color: yellow">&lt;set-property name="required" expression="true"/&gt;</span>
<span style="background-color: yellow">&lt;set-property name="clientScriptingEnabled" expression="true"/&gt;</span>
<span style="background-color: yellow">&lt;/bean&gt;</span>
&lt;property-specification name="person" type="org.appfuse.model.Person"/&gt;
&lt;property-specification name="personManager" type="org.appfuse.service.PersonManager"&gt;
global.appContext.getBean("personManager")
&lt;/property-specification&gt;
&lt;property-specification name="message" type="java.lang.String"/&gt;
<span style="background-color: yellow">&lt;component id="firstNameField" type="ValidField"&gt;</span>
<span style="background-color: yellow">&lt;binding name="value" expression="person.firstName"/&gt;</span>
<span style="background-color: yellow">&lt;binding name="validator" expression="beans.requiredValidator"/&gt;</span>
<span style="background-color: yellow">&lt;message-binding name="displayName" key="person.firstName"/&gt;</span>
<span style="background-color: yellow">&lt;/component&gt;</span>
<span style="background-color: yellow">&lt;component id="lastNameField" type="ValidField"&gt;</span>
<span style="background-color: yellow">&lt;binding name="value" expression="person.lastName"/&gt;</span>
<span style="background-color: yellow">&lt;binding name="validator" expression="beans.requiredValidator"/&gt;</span>
<span style="background-color: yellow">&lt;message-binding name="displayName" key="person.lastName"/&gt;</span>
<span style="background-color: yellow">&lt;/component&gt;</span>
&lt;/page-specification&gt;
</pre>
&lt;property name="message" persist="flash"/&gt;
At line 38 added 18 lines.
&lt;component id="personForm" type="Form"&gt;
<span style="background-color: yellow">&lt;binding name="delegate" value="ognl:beans.delegate"/&gt;
&lt;binding name="clientValidationEnabled" value="true"/&gt;</span>
&lt;/component&gt;
&lt;component id="firstNameField" type="TextField"&gt;
&lt;binding name="value" value="person.firstName"/&gt;
<span style="background-color: yellow">&lt;binding name="validators" value="validators:required"/&gt;</span>
&lt;binding name="displayName" value="message:person.firstName"/&gt;
&lt;/component&gt;
&lt;component id="lastNameField" type="TextField"&gt;
&lt;binding name="value" value="person.lastName"/&gt;
<span style="background-color: yellow">&lt;binding name="validators" value="validators:required"/&gt;</span>
&lt;binding name="displayName" value="message:person.lastName"/&gt;
&lt;/component&gt;
&lt;/page-specification&gt;</pre>
At line 59 changed 1 line.
There are a [number of different validators|http://jakarta.apache.org/tapestry/doc/ComponentReference/ValidField.html] you can use, this example just shows a way to validate Strings are entered. The userForm.page has examples of an EmailValidator and a PatternValidator. All input fields generated by AppGen are required by default. You can change this by modifying the extras/appgen/src/**/*_page.xdt files.
There are a [number of different validators|http://jakarta.apache.org/tapestry/UsersGuide/validation.html] you can use, this example just shows a way to validate Strings are entered. The userForm.page has examples of an EmailValidator and a PatternValidator. All input fields generated by AppGen are required by default. You can change this by modifying the extras/appgen/src/**/*_page.xdt files.
At line 65 changed 1 line.
if (getValidationDelegate().getHasErrors()) {
if (getDelegate().getHasErrors()) {
At line 71 changed 1 line.
Since validation is configured in the page-specification file (a.k.a. personForm.page), whenever a field uses the ValidField component, it will get validated. For those fields that don't have validation, make sure and use the @Label component for its label. Below is a non-validated field example from web/pages/userForm.html.
Since validation is configured in the page-specification file (a.k.a. personForm.page), whenever a field specifies a "validators" property. For those fields that don't have validation, make sure and use the @Label component for its label. Below is a non-validated field example from web/pages/userForm.html.
At line 201 added 2 lines.
import org.apache.tapestry.engine.RequestCycle;
import org.appfuse.service.Manager;
At line 203 removed 1 line.
import org.appfuse.service.PersonManager;
At line 205 added 3 lines.
import java.util.HashMap;
import java.util.Map;
At line 208 changed 4 lines.
protected void setUp() throws Exception {
super.setUp();
page = (PersonList) getPage(PersonList.class);
protected void onSetUp() throws Exception {
super.onSetUp();
At line 213 changed 2 lines.
page.setPersonManager((PersonManager) ctx.getBean("personManager"));
page.setRequestCycle(getCycle(request, response));
Map map = new HashMap();
map.put("personManager", (Manager) applicationContext.getBean("personManager"));
page = (PersonList) getPage(PersonList.class, map);
At line 217 changed 2 lines.
protected void tearDown() throws Exception {
super.tearDown();
protected void onTearDownAfterTransaction() throws Exception {
super.onTearDown();
At line 223 changed 3 lines.
MockRequestCycle cycle = (MockRequestCycle) page.getRequestCycle();
cycle.addServiceParameter(new Long(1));
RequestCycle cycle = new MockRequestCycle();
cycle.setServiceParameters(new Object[] {new Long("1")});
At line 227 removed 1 line.
At line 232 changed 3 lines.
PageEvent event = new PageEvent(page, page.getRequestCycle());
page.pageBeginRender(event);
assertTrue(page.getPeople().size() >= 1);
assertTrue(page.getPersons().size() >= 1);
At line 242 changed 1 line.
Create src/web/**/action/PersonList.java. It should implement [PageRenderListener|http://jakarta.apache.org/tapestry/doc/api/org/apache/tapestry/event/PageRenderListener.html] and read as follows:
Create src/web/**/action/PersonList.java. It should implement [PageBeginRenderListener|http://jakarta.apache.org/tapestry/tapestry/apidocs/org/apache/tapestry/event/PageBeginRenderListener.html] and read as follows:
At line 251 removed 2 lines.
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.event.PageRenderListener;
At line 257 changed 1 line.
public abstract class PersonList extends BasePage implements PageRenderListener {
public abstract class PersonList extends BasePage {
At line 259 removed 3 lines.
public abstract void setPersonManager(PersonManager manager);
public abstract List getPeople();
public abstract void setPeople(List people);
At line 263 changed 2 lines.
public void pageBeginRender(PageEvent event) {
setPeople(getPersonManager().getPeople(null));
public List getPersons() {
return getPersonManager().getPeople();
At line 266 changed 1 line.
At line 268 changed 2 lines.
Object[] parameters = cycle.getServiceParameters();
Long id = (Long) parameters[0];
Object[] parameters = cycle.getListenerParameters();
Long personId = (Long) parameters[0];
At line 272 changed 1 line.
log.debug("fetching person with id: " + id);
log.debug("fetching person with personId: " + personId);
At line 275 changed 2 lines.
Person person = getPersonManager().getPerson(id.toString());
Person person = getPersonManager().getPerson(personId);
At line 297 removed 4 lines.
Change it in personList.page too:
{{{<property-specification name="people" type="java.util.List"/>}}}
At line 319 changed 1 line.
{{{<verifytitle description="display Main Menu" text=".*${mainMenu.title}.*" regex="true"/>}}}
{{{<verifyTitle description="display Main Menu" text=".*${mainMenu.title}.*" regex="true"/>}}}
At line 323 changed 1 line.
{{{{{{<verifytitle description="Person List appears if save successful" text=".*${personList.title}.*" regex="true"/>}}}}}}
{{{{{{<verifyTitle description="Person List appears if save successful" text=".*${personList.title}.*" regex="true"/>}}}}}}

Back to ValidationAndListTapestry, or to the Page History.