Sunteți pe pagina 1din 14

1

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:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>


<html>
<body>
<%-- include header --%>
<tiles:insert attribute="header"/>

<%-- include body --%>


<tiles:insert attribute="body"/>

<%-- include footer --%>


<tiles:insert attribute="footer"/>
</body>
</html>

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.

Consider this a.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>


<tiles:insert page="/layout.jsp" flush="true">
<tiles:put name="header" value="/header.jsp"/>
<tiles:put name="body" value="/aBody.jsp"/>
<tiles:put name="footer" value="/footer.jsp"/>
</tiles:insert>

Consider this b.jsp:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>


<tiles:insert page="/layout.jsp" flush="true">
<tiles:put name="header" value="/header.jsp"/>
<tiles:put name="body" value="/bBody.jsp"/>
<tiles:put name="footer" value="/footer.jsp"/>
</tiles:insert>

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

However, it increases complexity by introducing another layout page. Understanding and


implementing templating can also be difficult at first.

Solution 6: Struts and Tiles

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:

<?xml version="1.0" encoding="ISO-8859-1"?>


<component-definitions>
<definition name="aDef" path="/layout.jsp">
<put name="header" value="/header.jsp"/>
<put name="footer" value="/footer.jsp"/>
<put name="body" value="/aBody.jsp"/>
</definition>
<definition name="bDef" path="/layout.jsp">
<put name="header" value="/header.jsp"/>
<put name="footer" value="/footer.jsp"/>
<put name="body" value="/bBody.jsp"/>
</definition>
<definition name="cDef" path="/layout.jsp">
<put name="header" value="/header.jsp"/>
<put name="footer" value="/footer.jsp"/>
<put name="body" value="/cBody.jsp"/>
</definition>
</component-definitions>

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>

Tag Name Description


If the requested variable is either null or
empty an empty string then the nested body
content of this tag is evaluated
If the requested variable is equal to the
equal specified value then the nested body
content of this tag is evaluated
forward It forwards the control to the page
specified by the specified
4

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

the request then it generates the nested


body content of this tag .
If the specified value is present in the
present request then it generates the nested body
content of this tag
redirect It renders an HTTP Redirect request.

public class EmptyForm extends ActionForm


{

private String text = "";

public String getText()


{
return text;
}

public void setText(String text)


{
this.text=text;
}
}

<bean:write name="EmptyForm" property="text"/>

<logic:notEmpty name="EmptyForm" property="text" scope="request">


<h4>Using the tag &lt;logic:notEmpty &gt; </h4>
Results:not Empty
</logic:notEmpty>

<logic:empty name="EmptyForm" property="text" scope="request">


<h4>Using the tag&lt;logic:empty &gt;</h4>
Results: Empty
</logic:empty>

<logic:match name="LogicForm" property="text" value="amit">


<h5>Using the tag &lt;logic:match &gt; </h5>
Result : entered text contains "amit"
</logic:match>
6

<logic:notMatch name="LogicForm" property="text" value="abc">


<h5>Using the tag&lt;logic:notMatch&gt;</h5>
Result: entered text does not contains "abc"
</logic:notMatch>

Nested tag
Create a new struts 1.1 project to to understand nested tags.

Object Class Books


Create a class Books with properties id and name in the package roseindia.web.common.
Add a getter and setter method for each property.
Also add a constructor that initialize the properties.

Books.java

package roseindia.web.common;
public class Books {

private int id;


private String name;

//constructors
public Books(){}

public Books(int id, String name){


this.id = id;
this.name = name;
}
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;
}
}

Object Class Authors


Create a second java class Authors in the same package roseindia.web.common.
Add two properties, id of type int and name of type String and one property books of type
7

Collection, which holds a list of books.


Add a getter and setter method for each property.
Also add a constructor that initialize the properties.

Authors.java

package roseindia.web.common;
import java.util.*;
public class Authors {

private int id;


private String name;

//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;
}
}

Action form class AuthorsBooksForm


Create a java class AuthorsBooksForm in the package roseindia.web.struts.form, which
extends the class ActionForm of struts.
Add a property authors of type Authors .
Add a getter and setter method for the property authors .

Implement the reset() method of the ActionForm class.


8

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;

public void setAuthors(Authors authors) {


this.authors = authors;
}
public Authors getAuthors() {
return authors;
}

public void reset(ActionMapping


mapping,HttpServletRequest request) {

//initial a dummy collection of books


Collection books = new ArrayList();

books.add(new Books(1, "Teach Yourself


C++"));
books.add(new Books(2, "Java: The Complete
Reference, J2SE"));
books.add(new Books(3, "Struts: The
Complete Reference"));

//initial a dummy authors


authors = new Authors(1, "Herbert Schildt",
books);

}
}

Action class AuthorsBooksAction

Create a java class AuthorsBooksAction in the package roseindia.web.struts.action, which


extends the class Action of struts.
Return the forward example.

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;

public class AuthorsBooksAction extends Action {

public ActionForward execute(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
AuthorsBooksForm booksForm =
(AuthorsBooksForm) form;

return mapping.findForward("books");
}
}

The JSP file

Create a new JSP file authorsbooks.jsp.


Add the reference to the tag library nested at the top of the file.

authorsbooks.jsp

<%@ page language="java"%>


<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>

<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>

Configure the struts-config.xml

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>

Struts Dispatch Action

The org.apache.struts.actions.DispatchAction class enables a user to collect


related functions into a single Action. It eliminates the need of
creating multiple independent actions for each function.

Let's develop Dispatch_Action class which is a sub class


of org.apache.struts.actions.DispatchAction class. This class does not provide
an implementation for the execute() method because DispatchAction class itself
implements this method. This class manages to delegate the request to one of the
methods of the derived Action class. An Action Mapping is done to select the
particular method (via Struts-Configuration file).

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

**/

public class Dispatch_Action extends DispatchAction

public ActionForward add(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("You are in add function.");
return mapping.findForward("add");
}

public ActionForward edit(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception

System.out.println("You are in edit function.");


return mapping.findForward("edit");

public ActionForward search(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("You are in search function");
return mapping.findForward("search");

}
12

public ActionForward save(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("You are in save function");
return mapping.findForward("save");
}
}

Developing the Action Mapping in the struts-config.xml

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>

Developing jsp page

Code of the jsp (DispatchAction.jsp) to delegate requests to different jsp pages :

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>


<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<HEAD>
<TITLE>Dispatch Action Example</TITLE>
<BODY>

<H3>Dispatch Action Example</H3>


<p><html:link page="/DispatchAction.do?parameter=add">Call Add Section</html:link></p>
<p><html:link page="/DispatchAction.do?parameter=edit">Call Edit Section</html:link></p>
<p><html:link page="/DispatchAction.do?parameter=search">Call Search
Section</html:link></p>
13

<p><html:link page="/DispatchAction.do?parameter=save">Call Save


Section</html:link></p>

</html:html>

Struts Forward Action Example

No need to develop an Action Class

Developing the Action Mapping in the struts-config.xml

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>

Developing a jsp page

Code of the jsp (ForwardAction.jsp) to forward request to a different jsp page :

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>


<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<HEAD>
<TITLE>Forward Action Example</TITLE>
<BODY>

<H3>Forward Action Example</H3>


<p><html:link page="/success.do">Call the Success page</html:link></p>
</html:html>

Add the following line in the index.jsp to call the form.

<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>

S-ar putea să vă placă și