Have you ever been tired of starting up an application server and deploy an application just to check some minor modification in a JSP application? Or had trouble communicating with a site designer? If you had, S2JSF is for you. J2JSF allows you to write view templates in HTML so they can be more easily displayed.
S2JSF requirements are the same as Seasar2 requirements. It requires JDK1.4 or higher. Just extract downloaded S2JSFVx.x.x.zip archive file and import unzipped s2jsfdirectory to Eclipse workspace(File -> import -> Existing Project into Workspace)
S2JSF sample files must be downloaded separately. The sample uses Tomcat5 and Tomcat Plugin. Download and setup these software before installing S2JSF samples.
Execute the following steps to used the samples:
Download and extract downloaded archive file
Import the unzipped file into Eclipse workspace.
Start Tomcat
S2JSF samples may be viewed by accesing http://localhost:8080/s2jsf-example/ from your web browser (assuming tomcat was installed on port 8080.)
Enter http://localhost:8080/s2jsf-example/hello/hello.html?message=Hitoshi as a URI in a web browser. "Hello Hitoshi" should be displayed.
This sample show how to do the followings:
The first line, xmlns:m="http://www.seasar.org/maya", is the S2JSF namespace declaration. It is used to associate namespace URI, http://www.seasar.org/maya, to a prefix character. Character "m" is usually used as this character, but it may be any arbitrary character string consistent with the XML namespace specification.
m:extends attribute specifies the layout to extend.
To provide consistent look & feel, there are many sites where the layout of pages are divided into header, menu, body, and footer with pages having a common header, menu, and footer and only differing in the content of the body. To correspond to this layout, S2JSF allows developers to create a base template layout and for each individual pages to extend this template. For example, in the above situation where pages differ only in the body section, only the body section should be individually created.
listing 2 Layout.html is an example of a base template.
Line 7 sets the value to layoutTitle to value of layoutTitle set in layout.html. As is this example, to pass a parameter to the parent page, f:param tag is used inside the body tag.
Lines 8 to 10 overwrite the parent page's body section.
Line 9 is used to output a string to the HTML document. String matching pattern #{...} is called a ValueBinding and is used to dynamically set a value. When S2JSF find a ValueBinding string, it tries to set a value by searching in request, parameter, session, and then S2Container. When #{variable name.property name} is specified, variable name is first resolved and then the value of property is used.
To output a value dynamically, value attribute in a span element is used as in line 9. #{message} specifies to output the content of message parameter in a request. The content of the span tag, hoge, will be ignored during execution. It is only used as a dummy place holder when hello.html file is opened directly from a web browser.
inject attribute is used to specify a JSF's JSP tag within a HTML document. Existing JSF taglib may be used. In the above HTML document, inject attribute in line 4 is set to s:link.
Line 5 specifies the default title of the generated HTML document. Value of the title is set to layoutTitle. The value of this variable should be set by the pages extending this base template.
The prefix "s" is defined in the WEB-INF/classes/jsf.dicon(or WEB-INF/src) file as follows. Note that prefix s is associated with URI http://www.seasar.org.jsf.
Line 17 defines the body section of the document. Naming the document section using the name attribute makes it possible to easily extend the layout and overwrite the content. For example, the body section of the above sample may be overwritten by defining <span m:inject="s:insert" m:name="body">...,</span> in the extended page.
Span element in line 10 in listing 2 inserts header.html page. The inject attribute is set to insert and the src attribute specifies the file to insert. In the sample, header.html contains the following elements and inserts an image seasar.gif.
The page to be inserted has a span tag with inject="s:insert" attribute and without the name attribute. Content to be inserted are specified as child elements of this span tag. Elements above this span element are displayed when the file is directly opened with a web browser but are ignored when the page is generated (i.e. URI to generate the page is used instead of a file name.)
Following is the content of menu.html used in the sample.
The inserted page contains the page name of URI to locate when action attribute in an anchor tag is clicked. Action attribute may specify a JavaBeans method or page name. To specify a JavaBeans method, value of the action attribute is set to ${parameter_name.method.name}. To specify a page name, action attribute is set to a string containing the name of the page name.
In the above listing, page name hello is defined in the following section in the WEB-INF/faces-config.xml file.
Value of from-outcome element is set to the page name to locate. The actual URI to locate is contained the the to-view-id element. redirect tag is used to redirect to the page. When redirect tag is not specified, it is forwarded to the page.
In the sample listing above, the page name hello is associated to URI /hello/hello.html. When the use selects a link, the web browser will be redirected to this /hello/hello.html document.
Furthermore, parameter may be passed to the redirected page by specifying a f:param. In menu.html, there is a span tag with inject set to f:param with name set to message and value set to World. This setting will result in passing a parameter named message with value World.
The content of the Head element from line 2 to line 5 are all ignored during actual runtime but are used when the add.html is directly opened from a web browser.
Line 10 specifies error output operation. When globalOnly is set to true, error messages that are not specific to an input (global error messages) are outputted. When it is set to true, all error messages are outputted.
Line 11 associates input tag with a JavaBeans property. Input values are set to JavaBeans property if it passes the validation conditions. It the value is invalid, it is displayed as is on a page. Unlike in Struts where input values are set to a String by an ActionForm and then validated, JSF validates the input and ony passes valid data.
Value of the value attribute addDdo is defined in the examples/jsf/dicon/add.dicon file as follows:
instance attribute is set to "request" so addTdo is managed in a request scope.
app.dicon which defined the root element includes add.dicon file using the following include statement:
Line 14 in listing 7 specifies to call method calculate() when a button is clicked. Specifying methods name as a value to an action attribute that is to be called when an user clicks on a button or a link is named MethodBinding. The syntax of the value is #{variable name,method name}. Methods called by MethodBinding should not have any argument and should return a String.
addAction in the sample is defined in the add.dicon file as follows:
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.
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.
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";