...
For example, a Login page class, which collects a user name and a password, might look like:
Code Block | ||||
---|---|---|---|---|
| ||||
package com.example.newapp.pages; import com.example.newapp.services.UserAuthenticator; import org.apache.tapestry5.annotations.*; import org.apache.tapestry5.corelib.components.Form; import org.apache.tapestry5.corelib.components.PasswordField; import org.apache.tapestry5.ioc.annotations.Inject; public class Login { @Persist @Property private String userName; @Property private String password; @Inject private UserAuthenticator authenticator; @InjectComponent("password") private PasswordField passwordField; @Component private Form loginForm; /** * Do the cross-field validation */ void onValidateFromLoginForm() { if (!authenticator.isValid(userName, password)) { // record an error, and thereby prevent Tapestry from emitting a "success" event loginForm.recordError(passwordField, "Invalid user name or password."); } } /** * Validation passed, so we'll go to the "PostLogin" page */ Object onSuccess() { return PostLogin.class; } } |
...
Configuring Fields and Labels
The template for the Login page template below contains a minimal amount of Tapestry instrumentation and references some of the Bootstrap CSS classes (Bootstrap is automatically integrated into each Tapestry page):page by default, starting with Tapestry 5.4).
Code Block | ||||
---|---|---|---|---|
| ||||
<html t:type="layout" title="newapp com.example" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd"> <div class="row"> <div class="span4 offset3"> <t:form t:id="loginForm"> <h2>Please sign in</h2> <t:textfield t:id="userName" t:mixins="formgroup"/> <t:passwordfield t:id="password" value="password" t:mixins="formgroup"/> <t:submit class="btn btn-large btn-primary" value="Sign in"/> </t:form> </div> </div> </html> |
Rendering the page gives a reasonable reasonably pleasing first pass:
The Tapestry Form component is responsible for creating the necessary URL for the form submission (this is Tapestry's responsibility, not yours).
...