listing 10: add.dicon(partial)
<component name="addAction" class="examples.jsf.action.impl.AddActionImpl"
instance="request"/>
Action class received entered value through a property so the instance is set to request.
It is not neccessary separate an interface with an implementation, but it offers a clearer specification.
package examples.jsf.action;
public interface AddAction {
public String calculate();
}
package examples.jsf.action.impl;
import examples.jsf.action.AddAction;
import examples.jsf.dto.AddDto;
import examples.jsf.logic.AddLogic;
public class AddActionImpl implements AddAction {
private AddDto addDto;
private AddLogic addLogic;
public void setAddDto(AddDto addDto) {
this.addDto = addDto;
}
public void setAddLogic(AddLogic addLogic) {
this.addLogic = addLogic;
}
public String calculate() {
int result = addLogic.calculate(addDto);
addDto.setResult(result);
return null;
}
}
To receive entered value, setter method is defined to addDto property.
Right before calling on method specified in MethodBinding, S2JSF checks if there is any property defined by a setter method. When there is a property, S2JSF checks if there is any variable with the same name as the property in request attributes, parameters, session attributes, and S2Containters. When there is a matching variable, S2JSF automatically calls on the setter method and set the value of that variable.
variable named addLogic is not defined anywhere but when property type is an interface, S2Container automatically sets the class implementing that interface.
Class implementing AddLogic is defined in add.dicon as follows:
<component class="examples.jsf.logic.impl.AddLogicImpl"/>
If the method called by MethodBinding returns a null, process is transferred to itself.
This sample demostrates the following topics:
- Repetition using forEach.
- Transferring page when previewing with anchor tag
- Transferring page when previewing with buttonr tag
Following is a part of the sample HTML document:
01:<form>
02: <table border="1">
03: <tr bgcolor="#7777FF">
04: <th>Key</th>
05: <th>Name</th>
06: <th colspan="2">to ResultPage</th>
07: </tr>
08: <span m:inject="s:forEach" m:items="#{forEachDtoList}"
09: m:var="e" m:varIndex="i">
10: <tr>
11: <td><span m:value="#{e.key}">111</span></td>
12: <td><span m:value="#{e.name}">aaa</span></td>
13: <td><a href="forEachResult.html" m:action="forEachResult">to ResultPage
14: <span m:inject="f:param" m:name="index" m:value="#{i}"/>
15: </a>
16: </td>
17: <td>
18: <input type="button" m:action="forEachResult" value="to ResultPage"
19: onclick="location.href='forEachResult.html'">
20: <span m:inject="f:param" m:name="index" m:value="#{i}"/>
21: </input>
22: </td>
23: </tr>
24: </span>
25: </table>
26:</form>
forEach in span element in line 8 is used to specify a loop of lines 8 through 24. items attribute specifies how to loop, var attribute specifies variable name to access value assigned in a loop, varIndex attributes specifies loop control variable.
Value of element in a loop may be obtained through properties of variable name assigned to var attribute. For example, lines 11 and 12 respectively gets values of key and name of elements in a loop.
In line 13, href attribute and action attribute are both specified. In this kind of situation, href is ignored during execution. The href attribute, however, is effective mean to move to another page when the html file is directly opened from a web browser.
Line 14 is an example to passing parameter using param. It is specified in a span element that is a child to the a element. The parameter is passed to the target of the target URI.
Line 18 is a example of a button with action attribute and onclick attribute. onclick attribute is ignored when executed but is used to transfer to page 'forEachResult.html' when the html file is directly opened from the web browser.
Line 20 is used to pass parameter to the transferred page when the button is pressed.
This sample demonstrates the followings:
- Page initialization
- Setting getter method in Action to request property and automatically set session.
Following is a part of the sample HTML document:
01:<html xmlns:m="http://www.seasar.org/maya"
02: m:action="#{forEachResultInitAction.initialize}"
03: m:extends="/WEB-INF/layout/layout.html">
04:<head>
05:<meta http-equiv="Content-Type" content="text/html; charset=Windows-31j" />
06:<title>ForEach</title>
07:</head>
08:<body>
09:<span m:inject="f:param" m:name="layoutTitle" m:value="ForEach"/>
10:<span m:inject="s:insert" m:name="body">
11: Key:<span m:value="#{forEachDto.key}">111</span>
12: Name:<span m:value="#{forEachDto.name}">aaa</span><br />
13: <a href="forEachList.html">previous</a>
14:</span>
15:</body>
16:</html>
action attribute in row 2 specifies the initialization method. The Action class is defined as follows:
package examples.jsf.action.impl;
import java.util.List;
import examples.jsf.action.ForEachResultInitAction;
import examples.jsf.dto.ForEachDto;
public class ForEachResultInitActionImpl implements ForEachResultInitAction {
private int index;
private List forEachDtoList;
private ForEachDto forEachDto;
public void setIndex(int index) {
this.index = index;
}
public void setForEachDtoList(List forEachDtoList) {
this.forEachDtoList = forEachDtoList;
}
public ForEachDto getForEachDto() {
return forEachDto;
}
public String initialize() {
forEachDto = (ForEachDto) forEachDtoList.get(index);
return null;
}
}
Remember that the page that linked to this page had child to a anchor tag and button tag that had an param attribute which passes index. So, by defining setIndex() method, S2JSF will automatically set the parameter value. Furthermore, by defining setForEachDtoList() method, it is possible to refer to forEachDtoList defined in examples/jsf/dicon/foreach.dicon.
By using initMethod tag, it is possible to call on arbitrary method. Thus, objects may be constructed more freely.
<component name="forEachDtoList" class="java.util.ArrayList" instance="session">
<initMethod name="add" >
<arg>
<component class="examples.jsf.dto.ForEachDto" >
<property name="key">"111"</property>
<property name="name">"aaa"</property>
</component>
</arg>
</initMethod>
<initMethod name="add" >
<arg>
<component class="examples.jsf.dto.ForEachDto" >
<property name="key">"222"</property>
<property name="name">"bbb"</property>
</component>
</arg>
</initMethod>
</component>
Set object based on index specified in initialize() method to forEachDto property. After initialize() method is executed, S2JSF automatically sets value of forEachDto property to forEachDto attribute in a request because forEachDto property has a getter method defined.
It is possible to set to a session instead of a request by declaring the following constant:
public static final String property name_EXPORT =
"session";