Tapestry Study Notes (3) using Forms II

Source: Internet
Author: User

§ 2-3

In study Note 2, we finally implemented this function: In the listening method, obtain the page to be redirected, set a property value for that page, and finally return the Page Object, to achieve the jump and value transfer function. The following describes how to obtain the jump page class in the page component configuration file.
The <inject> label of the page component configuration file is used.
Home. Page

<Page-specification class = "com. simpleform. pages. Home">
<Inject type = "page" Object = "result" property = "resultpage"/>
<Component type = "form" id = "myform">
<Binding name = "listener" value = "listener: onsubmit"/>
</Component>
<Component type = "textfield" id = "username">
<Binding name = "value" value = "ognl: username"/>
</Component>
</Page-Specification>

Modify home. Java

Package com. simpleform. pages;

Import org. Apache. tapestry. ipage;
Import org. Apache. tapestry. irequestcycle;
Import org.apache.tapestry.html. basepage;

Public abstract class home extends basepage ...{
Abstract Public result getresultpage ();
Private string username;

Public String GetUserName ()...{
Return username;
}

Public void setusername (string username )...{
This. Username = username;
}
Public ipage onsubmit (irequestcycle cycle)
...{
System. Out. println ("submitted value:" + username );
Result resultpage = getresultpage ();
Resultpage. setresultvalue (username );
Return resultpage;
}

}

Rewrite the home class to abstract class, and add getresultpage (); abstract method. In the onsubmit method, you can use it to get a result class instance.

<Inject> is it to inject a property of the result class into the home class? Type = Page Object = Result property = resultpage

§ 2-4
The previous example in the dwded tutorial mentions such a bug.
After the results are submitted and displayed, the page will not be closed. Another result page will be displayed, which will be the result of the previous submission. This is not what we want,
Initialization should be empty.
The solution is to add an initialization method in result. Java: protected void initialize ()
...{
Resultvalue = "";
}

In this way, the resultvalue will be initialized for each request. Can't I initialize it in the constructor? No, I tried it.
I am not very familiar with the specific execution process. it seems that the page pool technology is used, and the page is retrieved from the pool every time the page is requested. In this way, the constructor will not be executed.
The above resultvalue should actually make it equal to null.

In fact, this initialize function can be completely handed over to tapestry to write this attribute to a component configuration file for fear.
Result. page <page-specification class = "com. simpleform. pages. Result">
<Property name = "resultvalue"/>
<Component id = "resultvalue" type = "insert">
<Binding name = "value" value = "ognl: resultvalue"/>
</Component>
</Page-Specification>

<Property> the tag will make the application generate a result subclass at runtime, which includes the initialize, get, and set methods. At the same time, you also need to write the result class into the abstract class.
Result. Java

Package com. simpleform. pages;

Import org.apache.tapestry.html. basepage;

Public abstract class result extends basepage ...{
Abstract Public void setresultvalue (string resultvalue );
}

<Property> an attribute is injected into the page .. <property> the tag also has an attribute called init-value, which can initialize a value you need to inject the variable. if no value exists, the initial value of the injected variable is the default value, for example, string is null, int is 0, Boolean is false.
In the above Code, only the set method is used, but the get method is not used because it is in the home. in the onsubmit method of Java, we need the set method after obtaining the result class. The result class cannot call the Set Method of the implicitly generated subclass, so it is necessary to declare it explicitly.

Home. Java has a username attribute. You can also inject pages using injection.

§ 2-5
Below is an advanced implementation of pages and properties injection-annotations, which is rarely used, but I know it can be used to conveniently implement many functions. You need to write the configuration file.
Delete <inject object = "result" type = "page" '> from home. Page...
Shouldn't there be an abstract getresultpage () method in home. Java? Add the following line of code above:
@ Injectpage ("result ")
However, JDK 1.4 does not support such syntax at least...
View the properties of the project in netbeans and change the source code level to 1.5 or higher ..

@ Injectpage is not @ injectproperty .... this is because, in the tapestry. when an unimplemented abstract getter (or its inherited Interface) is found, tapestry automatically adds a property for it .. so, here you can delete the <property> tag...

What should I do with initialization ?...... In Abstract Public String GetUserName (); The following annotations is used:
@ Initialvalue ("username ..

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.