Documente Academic
Documente Profesional
Documente Cultură
http://www.javaworld.com/javaworld/jw-01-2002/jw-0104-tilestrut.html?page=1
Templating tiles
Using Tiles's templating feature, you can define the following layout (from the layout.jsp file
shown below) as a template. Since this is a layout, you insert placeholders instead of the actual
view components using the Tiles insert tag. Thus, for all components, this page defines one
reusable layout:
Other content pages, like a.jsp and b.jsp, use the above layout for arranging components. In
the actual page, you insert the layout using the Tiles insert tag. Using the Tiles put tag, you can
specify the actual view components for all placeholders specified in the layout.
Solution 5's most significant advantage is that it encapsulates the layout scheme or mechanism,
drastically reducing the coupling between common view components and other content bodies.
2
The above layout page, layout.jsp, contains the HTML and JSP code for organizing the
components. The content pages, a.jsp and b.jsp, do not contain any HTML code; they just
contain the Tiles tags to insert the necessary components. Wouldn't it be nice to specify all the
content pages in one XML configuration file?
Let's name that file tileDefinitions.xml and specify its pages as:
package com.malani.struts.action;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class DoFirst extends Action {
public ActionForward perform(
ActionMapping aMapping,
ActionForm aForm,
HttpServletRequest aRequest,
HttpServletResponse aResponse
) {
return aMapping.findForward("success");
}
}
You cannot invoke a definition directly from the browser, but you can invoke one from Struts as
if it is an actual resource. Define the Struts actions in the struts-config.xml file as shown
below:
3
<action path="/a"
type="com.malani.struts.action.DoFirst"
>
<forward name="success" path="aDef"/>
</action>
<action path="/b"
type="com.malani.struts.action.DoFirst"
>
<forward name="success" path="bDef"/>
</action>
<action path="/c"
type="com.malani.struts.action.DoFirst"
>
<forward name="success" path="cDef"/>
</action>
You cannot invoke a definition directly from the browser, but you can invoke one from Struts as
if it is an actual resource. Define the Struts actions in the struts-config.xml file as shown
below:
<action path="/a"
type="com.malani.struts.action.DoFirst"
>
<forward name="success" path="aDef"/>
</action>
<action path="/b"
type="com.malani.struts.action.DoFirst"
>
<forward name="success" path="bDef"/>
</action>
<action path="/c"
type="com.malani.struts.action.DoFirst"
>
<forward name="success" path="cDef"/>
</action>
ActionForward entry.
If the value of the requested variable is
greater than or equal to the specified
greaterEqual value then the nested body content of
this tag is evaluated
If the value of the requested variable is
greater than the specified value then the
greaterThan nested body content of this tag is
evaluated
It repeats the nested body content of this
iterate tag over a specified collection.
If the value of the requested variable is
less than or equal to the specified value
lessEqual then the nested body content of this tag
is evaluated
If the value of the requested variable is
less than the specified value then the
lessThan nested body content of this tag is
evaluated
Evaluates the nested body content of
this tag if the specified value is an
match appropriate substring of the requested
variable.
If the specified message is not present in
messagesNotPresent the request then it generates the nested
body content of this tag .
If the specified message is present in
messagesPresent this request then it generates the nested
body content of this tag .
If the requested variable is neither null
nor an empty string nor an empty
java.util.Collection (tested by the
notEmpty isEmpty() method on the
java.util.Collection interface) then the
nested body content of this tag is
evaluated
If the requested variable is not equal to
notEqual the specified value then the nested body
content of this tag is evaluated
If the specified value is not an
appropriate substring of the requested
notMatch variable then the nested body content of
this tag is evaluated
notPresent If the specified value is not present in
5
Nested tag
Create a new struts 1.1 project to to understand nested tags.
Books.java
package roseindia.web.common;
public class Books {
//constructors
public Books(){}
Authors.java
package roseindia.web.common;
import java.util.*;
public class Authors {
//books collection
private Collection books;
//constructors
public Authors() {}
public Authors(int id, String name, Collection
books){
this.id = id;
this.name = name;
this.books = books;
}
public Collection getBooks() {
return books;
}
public void setBooks(Collection books) {
this.books = books;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
AuthorsBooksForm.java
package roseindia.web.struts.form;
import java.util.*;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import roseindia.web.common.Authors;
import roseindia.web.common.Books;
public class AuthorsBooksForm extends ActionForm {
Authors authors;
}
}
AuthorsBooksAction.java
package roseindia.web.struts.action;
import roseindia.web.struts.form.AuthorsBooksForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
9
import java.util.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
return mapping.findForward("books");
}
}
authorsbooks.jsp
<html>
<head>
<title>Struts nested tag Example</title>
</head>
<body>
<h1>Struts nested tag Example</h1>
<b>Author and his books:</b>
<html:form action="/example" method="post">
<nested:nest property="authors">
<b><nested:write
property="name"/> </b>
<nested:iterate
property="books">
<ul><li><nested:write
property="name"/></li></ul>
</nested:iterate>
</nested:nest>
</html:form>
10
</body>
</html>
Open the struts-config.xml and add the form bean and action mapping.
struts-config.xml
<form-beans>
<form-bean name="AuthorsBooksForm"
type="roseindia.web.struts.form.AuthorsBooksForm" />
</form-beans>
<action-mappings>
<action
path="/example"
type="roseindia.web.struts.action.AuthorsB
ooksAction"
input="/pages/user/authorsbooks.jsp"
name="AuthorsBooksForm"
scope="request"
validate="false">
<forward name="example"
path="/pages/user/authorsbooks.jsp" />
</action>
</action-mappings>
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
11
/**
* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net
**/
}
12
Here, Action mapping helps to select the method from the Action class for specific requests.
Note that the value specified with the parameter
attribute is used to delegate request to the required method of the Dispath_Action Class.
<action
path="/DispatchAction"
type="roseindia.net.Dispatch_Action"
parameter="parameter"
input="/pages/DispatchAction.jsp"
name="DispatchActionForm"
scope="request"
validate="false">
<forward name="add" path="/pages/DispatchActionAdd.jsp" />
<forward name="edit" path="/pages/DispatchActionEdit.jsp" />
<forward name="search"
path="/pages/DispatchActionSearch.jsp"/>
<forward name="save" path="/pages/DispatchActionSave.jsp" />
</action>
</html:html>
Create seperate action-mapping , for each page you want to link.. Note that the "type" attribute
always take "org.apache.struts.actions.ForwardAction" value. Here "parameter" attribute
specifies the URL to which the request is forwarded .
<action
path="/success"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/Success.jsp"
input="/pages/ForwardAction.jsp"
scope="request"
validate="false">
</action>
<li>
<html:link page="/pages/ForwardAction.jsp">Struts Forward Action</html:link>
14
<br>
Example shows you how to use forward class to forward request to another JSP page.
</li>