Struts 2 简明教程
Struts 2 & Tiles Integration
在本章中,让我们逐步了解将 Tiles 框架与 Struts2 集成的步骤。Apache Tiles 是一个模板框架,用于简化 Web 应用程序用户界面的开发。
In this chapter, let us go through the steps involved in integrating the Tiles framework with Struts2. Apache Tiles is a templating framework built to simplify the development of web application user interfaces.
首先,我们需要从 Apache Tiles 网站下载 tiles jar 文件。您需要将以下 jar 文件添加到项目的类路径中。
First of all we need to download the tiles jar files from the Apache Tiles website. You need to add the following jar files to the project’s class path.
-
tiles-api-x.y.z.jar
-
tiles-compat-x.y.z.jar
-
tiles-core-x.y.z.jar
-
tiles-jsp-x.y.z.jar
-
tiles-servlet-x.y.z.jar
除了以上内容之外,我们还必须从 Struts2 下载 WEB-INF/lib 中复制以下 jar 文件。
In addition to the above, we have to copy the following jar files from the struts2 download in your WEB-INF/lib.
-
commons-beanutils-x.y.zjar
-
commons-digester-x.y.jar
-
struts2-tiles-plugin-x.y.z.jar
现在,让我们按照以下步骤设置 Struts-Tiles 集成 web.xml 。此处有两个重要需要注意的点。首先,我们需要告诉 tiles 在哪里可以找到 tiles 配置文件 tiles.xml 。在我们的例子中,它将在 /WEB-INF 文件夹中。接下来,我们需要初始化 Struts2 下载附带的 Tiles 监听器。
Now let us setup the web.xml for the Struts-Tiles integration as given below. There are two important point to note here. First, we need to tell tiles, where to find tiles configuration file tiles.xml. In our case, it will be under /WEB-INF folder. Next we need to initiliaze the Tiles listener that comes with Struts2 download.
<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id = "WebApp_ID" version = "2.5">
<display-name>Struts2Example15</display-name>
<context-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>
/WEB-INF/tiles.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
接下来,让我们在 /WEB-INF 文件夹中用以下内容创建 tiles.xml −
Next let us create tiles.xml under /WEB-INF folder with the following contents −
<?xml version = "1.0" Encoding = "UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name = "baseLayout" template="/baseLayout.jsp">
<put-attribute name = "title" value = "Template"/>
<put-attribute name = "banner" value = "/banner.jsp"/>
<put-attribute name = "menu" value = "/menu.jsp"/>
<put-attribute name = "body" value = "/body.jsp"/>
<put-attribute name = "footer" value = "/footer.jsp"/>
</definition>
<definition name = "tiger" extends = "baseLayout">
<put-attribute name = "title" value = "Tiger"/>
<put-attribute name = "body" value = "/tiger.jsp"/>
</definition>
<definition name = "lion" extends = "baseLayout">
<put-attribute name = "title" value = "Lion"/>
<put-attribute name = "body" value = "/lion.jsp"/>
</definition>
</tiles-definitions>
然后,我们在 baseLayout.jsp 中定义一个基本的骨架布局。它有五个可重用/可覆盖区域。即 title, banner, menu, body 和 footer 。我们为 baseLayout 提供默认值,然后创建从默认布局扩展的两个自定义。tiger 布局与基本布局相似,除了它使用 tiger.jsp 作为其正文和文本“Tiger”作为标题。同样,lion 布局与基本布局相似,除了它使用 lion.jsp 作为其正文和文本“Lion”作为标题。
Next, we define a basic skeleton layout in the baseLayout.jsp. It has five reusable / overridable areas. Namely title, banner, menu, body and footer. We provide the default values for the baseLayout and then we create two customizations that extend from the default layout. The tiger layout is similar to the basic layout, except it uses the tiger.jsp as its body and the text "Tiger" as the title. Similarly, the lion layout is similar to the basic layout, except it uses the lion.jsp as its body and the text "Lion" as the title.
让我们看一下各个 jsp 文件。以下是 baseLayout.jsp 文件的内容 −
Let us have a look at the individual jsp files. Following is the content of baseLayout.jsp file −
<%@ taglib uri = "http://tiles.apache.org/tags-tiles" prefix = "tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset = UTF-8">
<title>
<tiles:insertAttribute name = "title" ignore="true" />
</title>
</head>
<body>
<tiles:insertAttribute name = "banner" /><br/>
<hr/>
<tiles:insertAttribute name = "menu" /><br/>
<hr/>
<tiles:insertAttribute name = "body" /><br/>
<hr/>
<tiles:insertAttribute name = "footer" /><br/>
</body>
</html>
在这里,我们只是将具有 tiles 特性的基本 HTML 页面放在一起。我们在需要的地方插入 tiles 特性。接下来,让我们创建 banner.jsp 文件,内容如下 −
Here, we just put together a basic HTML page that has the tiles attributes. We insert the tiles attributes in the places where we need them to be. Next, let us create a banner.jsp file with the following content −
<img src="http://www.tutorialspoint.com/images/tp-logo.gif"/>
menu.jsp 文件有以下几行,它们是链接 - 到 TigerMenu.action 和 LionMenu.action struts 操作。
The menu.jsp file will have the following lines which are the links - to the TigerMenu.action and the LionMenu.action struts actions.
<%@taglib uri = "/struts-tags" prefix = "s"%>
<a href = "<s:url action = "tigerMenu"/>" Tiger</a><br>
<a href = "<s:url action = "lionMenu"/>" Lion</a><br>
lion.jsp 文件会有以下内容 −
The lion.jsp file will have following content −
<img src="http://upload.wikimedia.org/wikipedia/commons/d/d2/Lion.jpg"/>
The lion
tiger.jsp 文件会有以下内容 −
The tiger.jsp file will have following content −
<img src="http://www.freewebs.com/tigerofdarts/tiger.jpg"/>
The tiger
接下来,让我们创建操作类文件 MenuAction.java ,其中包含以下内容 −
Next, let us create the action class file MenuAction.java which contains the following −
package com.tutorialspoint.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class MenuAction extends ActionSupport {
public String tiger() { return "tiger"; }
public String lion() { return "lion"; }
}
这是一个非常简单的类。我们声明了两个分别返回 tiger 和 lion 作为结果的方法 tiger() 和 lion()。让我们将所有内容放在 struts.xml 文件中 −
This is a pretty straight forward class. We declared two methods tiger() and lion() that return tiger and lion as outcomes respectively. Let us put it all together in the struts.xml file −
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name = "default" extends = "struts-default">
<result-types>
<result-type name = "tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name = "*Menu" method = "{1}"
class = "com.tutorialspoint.struts2.MenuAction">
<result name = "tiger" type = "tiles">tiger</result>
<result name = "lion" type = "tiles">lion</result>
</action>
</package>
</struts>
让我们检查我们在上面文件中做了什么。首先,我们声明了一种称为“tiles”的新结果类型,因为我们现在使用 tiles 而不是普通 jsp 作为视图技术。Struts2 已经支持 Tiles 视图结果类型,因此,我们创建结果类型“tiles”,使其成为“org.apache.struts2.view.tiles.TilesResult”类。
Let us check what we did in above file. First of all, we declared a new result type called "tiles" as we are now using tiles instead of plain jsp for the view technology. Struts2 has its support for the Tiles View result type, so we create the result type "tiles" to be of the "org.apache.struts2.view.tiles.TilesResult" class.
接下来,我们想说,如果请求是 /tigerMenu.action,则将用户带到 tiger tiles 页面,如果请求是 /lionMenu.action,则将用户带到 lion tiles 页面。
Next, we want to say if the request is for /tigerMenu.action take the user to the tiger tiles page and if the request is for /lionMenu.action take the user to the lion tiles page.
我们使用一点正则表达式来实现这一点。在我们的操作定义中,我们说,匹配模式“*Menu”的任何内容都将由此操作处理。匹配方法将在 MenuAction 类中调用。也就是说,tigerMenu.action 将调用 tiger(),lionMenu.action 将调用 lion()。然后,我们需要将结果的结果映射到适当的 tiles 页面。
We achieve this using a bit of regular expression. In our action definition, we say anything that matches the pattern "*Menu" will be handled by this action. The matching method will be invoked in the MenuAction class. That is, tigerMenu.action will invoke tiger() and lionMenu.action will invoke lion(). We then need to map the outcome of the result to the appropriate tiles pages.
现在,右键单击项目名称并单击 Export > WAR File 以创建 War 文件。然后,将此 WAR 部署在 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URL http://localhost:8080/HelloWorldStruts2/tigerMenu.jsp 。这将产生以下屏幕 −
Now right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat’s webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/tigerMenu.jsp. This will produce the following screen −
同样,如果您转到 lionMenu.action 页面,您将看到使用相同 tiles 布局的 lion 页面。
Similarly, if you goto the lionMenu.action page, you will see the lion page which uses the same tiles layout.