不依赖XML代码粘合创建Struts 2应用程序

在这篇文章中,我们采用"约定优于配置"而抛弃XML代码粘合。利用SmartURLs为Struts 2提供的插件,我们能利用搜索引擎优化URIs自动组装Action到页面模板。

这篇文章包括:

* 采用约定优于配置
* 出去XML代码依赖
* 用注释验证输入

什么是XML代码粘合?
成功孕育成功,成功的应用软件随着时间的推移变的更庞大和越来越复杂。大多数应用程序由于太大,太复杂而导致我们不能立刻理清整个应用。时至今日,我们应该一次只关心一件事。

一种简化复杂应用程序的方法是分离关注点。我们组合类似的任务放在一起,所以我们一次只需要关注应用程序的一个方面。两个我们想要分离的关注点是表现逻辑和业务逻辑。

利用一个模板很容易构建一个表现逻辑,而且很容易设计业务逻辑使用传统的源代码。每个关注点它们自己很容易维护,但是我们需要一种方法把这些关注点再组合到一起。

在一个传统的Struts应用程序中,我们应用XML文档中的声明语句把这些关注点组织起来。在实际项目中,我们写很多XML代码只是为了说明:"对于这个请求,调用这个Java程序和呈现给这个页面模板。"例子1:strtus.xml"显示了一点xml代码粘合。

"例子 1: struts.xml"

<action name="hello-world" class="actions.HelloWorld">

<result>/results/hello-world.jsp</result>

</action>

起初,一点代码粘合可能无所谓,但是当应用程序的规模大到一定的时候,我们可能浪费同等的时间研究这些粘合代码和我们在编写Acton代码和jsp页面所花的时间。

今天,一种流行的方式是避免xml"代码粘合"而使用"约定优于配置"。

什么是约定优于配置? 

取代用写配置文件组装组件的一种策略是使用一致的命名约定,而且使得这些约定成为应用框架的一部份。很多Struts开发者已经使用命名约定来组装不同的组件。我们只需要写一个框架来观察大多我们已经应用的相同种类的约定。

例如,如果一个客户端请求"hello-world.action",框架知道去搜索"HelloWorld.class"或一个"hello-world.jsp"。如果Actoin返回一个"small"代码,框架首先知道去搜索"hello-world-small.jsp"(或者一个"hello-world-small.vm",你可能更喜欢Velogity模板)

这些匹配规则看上去很简单,但是在现实中,一些简单的规则足够应用于我们整个Struts应用程序中,而不需要写一点xml代码!

我们的约定也有一些缺陷,SmartURLs插件在这个基础上同时也提供了annotations,使得我们可以重写actoin或者结果匹配。

Struts 2是否默认是否支持约定优于配置?

Struts 2核心提供很少特性减少配置。但是为了得到所有的"约定优于配置"的好处,我们需要增加一个插件。 "

SmartURLs插件(http://cwiki.apache.org/S2PLUGINS/smarturls-plugin.html),提供一个对约定优于配置的支持。一个请求"/my-action被自动匹配到一个MyActoin而返回结果匹配my-actoin.jsp页面。为了保持系统的灵活性,当找不到MyActoin和my-actoin.jsp,将启动默认的搜索器去匹配。匹配规则在"Sidebar 1: SmartURLs Rules""中有。

"Sidebar 1: SmartURLs rules"

URL to Action class (/my/package/hello-world):

* A-1 Extract the final URL path segment (hello-world).
* A-2 Upper-case the initial letter, and if any hyphen appears within the URL path, upper-case any following letter, and remove the hyphen (HelloWorld).
* A-3 Convert the rest of the path to lowercase, and substitute slashes with dots (my.package.).
* A-4 Check the base action packages for a class matching package + action (actions.my.package.HelloWorld).
* actoin(actions.my.package.HelloWorld)。
* A-5 If a matching Action is not found, use the package's default Action class (ActionSupport).

Result code to Path:

* B-1 Append the result-code returned by an Action to original URL path (/my/package/hello-world-success).
* (/my/package/hello-world-success)。
* B-2 Check the base result folders for a matching JSP, Freemarker Template, or Velocity Template (/WEB-INF/results/my/package/hello-world-success.jsp).
* B-3 If the template is not found, remove the response code, and try again (/WEB-INF/result/my/package/hello-world.jsp).
* B-4 Raise a standard 404 error is a matching template is not found.

SmartURLs提供的特性有:

* 扩张 URIs.
* Actoin UTLs自动绑定到约定的类和页面。
* "搜索引擎能很好的优化Action URI。
* Annotations能指定不同的action名称,甚至多个名称
* 自动支持JSP,Freemarker和Velocity作为返回类型
* 强健的页面指向(/products 将匹配actions.Products和actions.products.Index)

我们如何写一个SmartURLs"Hello World"页面?

框架,像Struts把一个应用的处理流程分解成一系列的actions。每个action可能关联几个关注点,包括输入验证,业务逻辑,持久逻辑,消息资源,文本格式化和输出资源。每个action有他们自己的上下文,该上下文的提供给定的action的,可能随着环境的变化而改变。

一个action最简单的用力就是转到一个页面模板而没有指定任何的关注点。(只是展示@%#$^页面!)我们的SmartURLs hello world就是一个例子,让我们先试一个最简单的例子,然后再加一些其它的关注点。"Sidebar 2:展示一个页面"输出用力。

"Sidebar 2: Render page template"

系统展示一个页面模板没有一个自定义Action

1. 客户端请求一个action资源
2. 系统确定没有一个自定义Actoin配置为请求的资源。
3. 系统利用默认的Action展现页面模板资源。
4. 客户端渲染HTML并展示

我们可以配置SmartURLs使用预期的页面资源(jsp,freemarker,或者Velocity)在"/WEB-INF/results"下。这个位置Struts 2框架式可以访问的,但是不能被浏览器直接访问。这个页面资源不能被访问除了通过Struts2(或者其它服务端组件)。

在"WEB-INF/results"文件夹下 ,我们能放一个简单的jsp,如"Example 2: hello-world.jsp"所示:

"Example 2: hello-world.jsp"

<html>

<body>

<p>

Hello World!

</p>

<p>

It is now <%= new java.util.Date() %>.

</p>

</body>

</html>

当安装了SmartURLs插件后,我们启动web容器并输入URL http://localhost:8080/smartapp/hello-world(你的主机名和端口在你的容器里可能不同)

SmartURLs将展示"WEB-INF/results/hello-world.jsp",如图"Figure 1: Hello World!" 所示。

"Figure 1: Hello World!"

如果看到页面,那说明SmartURLs正常工作了。

SmartURLs向我们展示了:

利用扩张URIs

保存模板在安全的WEB-INF文件夹下

没有任何XML代码

如何显示我们的数据在页面上?

相对于简单的显示系统时间,我们更想融入我们的数据在一个页面模板里。典型的,我想设置一个属性在Struts Action里并在页面里显示该属性。

属性的值可能来自数据库,和经过一系列的业务规则的过滤,但是页面不需要知道这些。无论怎样,页面所要知道的是这个属性可用和显示它的值。相反,Action

立刻,我们的关注点是是否Action和页面模板被框架自动组装。所以我们需要做的是用一个值设置一个Action的属性,并看页面是否按我们的期望显示该值。

为了测试SmartURL的自动组装,让我们增加一个HelloWorld类在我们应用程序的"actions"包下。如"Example 3: HelloWorldAction.java". 所示:

"Example 3: HelloWorldAction.java"

package actions;

public class HelloWorldAction {

private String greeting;

public String getGreeting() {

return greeting;

}

public String execute() {

greeting = "The server time is " + new java.util.Date().toString();

return "success";

}

}

注意 ,这是一个"POJO"Action类(一个标准的Struts 2特性).

我们不需要任何框架的专用服务,所以我们不用继承基类或实现一个特定的接口。最多是一个特定的execute方法!另外一个让步就是我们需要在一个类名后加上"Actoin"后缀。

同时注意我们改变一些信息,所以我们能确定我们的页面被更新。更新页面如:"Example 4: hello-world.jsp (2)"所示

"Example 4: hello-world.jsp (2)"

<html>

<body>

<p>

Hello World!

</p>

<p>

${greeting}

</p>

</body>

</html>

当然,我们能用Struts 2 标签代替JSTL表达式, 但是<s:property value="greeting"/>比"${greeting]"冗余。

"Figure 2: Hello World! (2)"

SmartURLs能处理一个数据输入验证?

一个健壮的请求处理框架,像Struts2就是,依赖运行时环境,一个action可能改变处理流程通过选择不同的输出页面。例如,我们可能想要收集信息在一个页面。如果输入是正确的,我们可能想要到一个输出页面。如果输入不正确,我们可能想要返回输入页面。

由于验证工作框架提供的特殊服务之一,在"Example 5: HelloWorld.java (2)",,我们的类扩张了ActionSupport类。

"Example 5: HelloWorld.java (2)"

package actions;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.validator.annotations.*;

@Validation()

public class HelloWorld extends ActionSupport {

private String greeting;

@RequiredStringValidator(message="Please enter a greeting!")

public String getGreeting() {

return greeting;

}

public void setGreeting(String value) {

greeting = value;

}

}

由于我们现在继承了Action接口,SmartURLs规则同时让我们从类名中去掉"Action"后缀

在例5中,我们使用验证通过annotation(另一种标准的Struts2特性)来确保信息被录入。Struts 2 和 the SmartURLs插件利用annotation来绑定一个RequiredStringValidator到一个消息属性。核心框架提供了annotations对所有其它常用的验证。

为了对收集输入信息验证,我们可以增加另一个页面,如" Example 6: hello-world-input.jsp" 所示:

"Example 6: hello-world-input.jsp"

<%@ taglib uri="/struts-tags" prefix="s" %>

<html>

<body>

<p>

What would you like to say to the world?

</p>

<s:form action="hello-world">

<s:textfield label="Greeting" name="greeting" />

<s:submit />

</s:form>

</body>

</html>

图3:Hello World Input!"

"Figure 3: Hello World Input!"

如果没有添信息,验证将会失败,HelloWorld Action将返回"input",而且SmartURLs将会转发到"hello-world-input.jsp",如"Figure 4: Hello World Input! (2)".所示:

"Figure 4: Hello World Input! (2)"

如果通过验证,页面将如图 "Example 5: Howdy".所示:

"Figure 5: Hello World Input! (3)!"

注意我们实现这个通用的处理流程没有任何的XML代码!在这个处理流程中,唯一的元数据是RequiredStringValidator annotation.剩下都是约定优于配置所驱动!

如果提交表单我们想重定向会怎样?

如果表单验证通过,而且我们已经更新了数据库,我们经常会重定向一个确认对话框。一个重定向更新了浏览器里的地址。还有,一个重定向确保人们不会刷新而重复提交一个表单。

Struts2 支持重定向通过@Result annotation。为了重定向到另一个位置"success",我们要加一个"@Result" annotation到Action类。如"Example 7: HelloWorld.java (3)"展示了"@Result" annotation。

"Example 7: HelloWorld.java (3)"

@Result(name="success", type="redirect", location="hello-world-view")

public class HelloWorld extends ActionSupport

Example 7里的注释告知有一个hello-world-view action。为了创建一个,我们所要做的是重命名hello-world.jsp 为 hello-world-view.jsp.

成功的话,系统将会从Hello Action重定向到"hello-world-view.jsp",并展示我们从"hello-world-input"输入的问候语。

默认的,Actoin的保持的数据是request范围内的。如果我们重定向,我们将会失去第一个request会话,并创造地二个。现在,最简单的解决方案是把问候语保存在session范围内。修改后的代码如Example 8: HelloWorld.java (4)所示:

"Example 8: HelloWorld.java (4)"

package actions;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.validator.annotations.*;

import org.apache.struts2.interceptor.SessionAware;

import org.texturemedia.smarturls.Result;

@Validation()

@Result(name="success", type="redirect-action", location="hello-world-view")

public class HelloWorld extends ActionSupport implements SessionAware {

private Map<String, String> session;

public void setSession(Map value) {

session = value;

}

protected Map<String, String> getSession() {

Map<String, String> value = session;

return value;

}

public static String GREETING_KEY = "greeting";

@RequiredStringValidator(message="Please enter a greeting!")

public String getGreeting() {

return (String) getSession().get(GREETING_KEY);

}

public void setGreeting(String value) {

getSession().put(GREETING_KEY,value);

}

}

现在,我们打开"hello-world-input"并输入问候语,并提交到"hello-world"。如果我们成功的输入一个问候语,"hello-world"存储属性在session范围内,并且重定向到"hello-world-view"。为了能够返回输入页面,我们可以加一个连接到输入页面。如"Example 9: hello-world-view.jsp"所示:

"Example 9: hello-world-view.jsp"

<html>

<body>

<p>

Hello World!

</p>

<p>

${greeting}

</p>

<p>

<a href="hello-world-input.do">Try again!</a>

</p>

</body>

</html>

到现在,我们完成了一个完整的数据输入处理的Hello World程序。

* "Example 6: hello-world-input.jsp",
* "Example 7: HelloWorld.java (3)", and
* "Example 4: hello-world-view.jsp"

没有一行XML代码。

如何安装SmartURLs插件?

首先把SmartURLs JAR包放到web应用程序的lib文件夹下,还有其它Struts JARs包。为了很好的利用SmartURLs,我们需要在配置文件里做两个小的改动。就这样,这些改变,我们就能利用扩张的URLs。

"Example 8: web.xml"

<filter>

<filter-name>

struts2

</filter-name>

<filter-class>

org.texturemedia.smarturls.SmartURLsFilter

</filter-class>

</filter>

<filter-mapping>

<filter-name>

struts2

</filter-name>

<url-pattern>

/*

</url-pattern>

</filter-mapping>

其次,在应用的struts.xml文件里,增加"constant"节点,如"Example 9: struts.xml"所示:

"Example 9: struts.xml"

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="false" />

<constant name="struts.action.extension" value="" />

<constant name="smarturls.action.packages" value="actions" />

<constant name="smarturls.base.result.location" value="/WEB-INF/results/" ></constant>

<constant name="struts.custom.i18n.resources" value="support.package" ></constant>

</struts>

A larger application may need to define some other one-time settings or package defaults. But not gluecode!

一个更大的应用可能需要定义一些其它的一次设置的或者默认包的设置。但是没有代码粘合。

What's the minimum installation we need for a SmartURLs application?

If you are using MyEclipse or Eclipse with Web Tools (WTP), you can create a Dynamic Web Application, and then

1. Drop the usual Struts 2 JARs (freemarker, ognl, struts2-core, xwork2) under the standard "lib" folder, along with the SmartURLs plugin JARs (java-net-commons and smarturls-s2).
2. Under "WebContent/WEB-INF", create a "results" folder for page templates.
3. Under the "src" folder, create a "struts.xml" based on Example 9, and a "actions" package for Action classes.

And you are ready to go!

应用SmartURLs我们最少需要安装什么?

如果你使用MyEclipse 或者 Eclipse with Web Tools (WTP),你可以创建一个动态web应用,然后

4. 把Struts2经常用的包(freemarker, ognl, struts2-core, xwork2)和SmartURLs plugin JARs (java-net-commons and smarturls-s2)放到lib目录下
5. 在"WebContent/WEB-INF"下创建一个"results"文件夹。
6. 在"src"文件夹下,创建一个"struts.xml",内容如Example 9所示,并且加一个"actions"包放Actoin类。

就这些吗?

我们已经展示了最简单的方式应用SmartURLs。我们也展示了最简单的方式应用少量的注释。除了我们演示的注释应用外,SmartURLs还支持其它的注释。例如,我们可以给一个Action多个名称,或者除了execute的其它方法。

SmartURLs详细文档有多种注释和配置选择。更多SmartURLS实战,一个完整的SmartURLs MailReader例子也是可用的(如下的资源所示)

利用SmartURLs约定,我们可以少写很多代码并可以创建强大的,企业级的应用。我们可以用节省下来的时间用来改善我们的用户接口,或者应用的其它部分,使应用更好用。

现在,SmartURLs是一个第三方插件,但是标准的Struts 2.1插件编码工作正在启动。对Struts 2.0的插件编码提供了相似的功能,但是很少的特性。

Resources

* SmartURLs plugin site (http://code.google.com/p/smarturls-s2/)
* SmartURLs MailReader Example for Java 5 (http://husted.com/smart-urls/smart-urls.war)
* Apache Struts site (http://struts.apache.org)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值