本HelloWorld的需求很简单,只是可以通过英语和汉语两种语言来显示HelloWorld。当然,这不是自动翻译。只是在不同的环境中,读取不同的字符串。
这是Struts2的入门的第一个范例。是在eclipse 3.2.2 + myeclipse 5.2.2环境下开发的。读者可能会问,myeclipse不是自带有struts吗?是的。Myeclipse为了方便开发员,带有struts 1.1和1.2。但是没有Struts 2的开发包。Myeclipse是个很有用的东西,Myeclipse对开发和部署web站点,编辑xml文件,jsp标记很有帮助,因此,我在这提到myeclipse。这不是为myeclipse做广告。当然,你也可以使用eclipse中其他web开发插件。
开发HelloWorld项目的基本步骤是:
1. 建立web项目
2. 导入Struts 2库文件
3. 修改web.xml文件
4. 建立index.html文件。
5. HelloWorld.jsp文件
6. Action文件
7. 建立Struts配置文件
8. properties配置文件
1. 建立web项目
新建的的web项目名称是:jeff-struts2-helloworld。
图表 1
Myeclipse产生一些目录结构。下图是在该项目完成时的目录结构。
图表 2
2. 导入Struts 2库文件
图表 3
这5个文件是必须的。在struts网站下载下来文件包里面。把这5个文件导入到WEB-INF/lib目录里。这5个文件自动的加入到构建路径中了。
3. 修改web.xml文件
2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6
7 <welcome-file-list>
8 <welcome-file>index.html</welcome-file>
9 </welcome-file-list>
10
11 <filter>
12 <filter-name>Struts 2</filter-name>
13 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
14 </filter>
15
16 <filter-mapping>
17 <filter-name>Struts 2</filter-name>
18 <url-pattern>/*</url-pattern>
19 </filter-mapping>
20 </web-app>
21
在web.xml文件中,首先建立首页文件(index.html),这样方便访问。然后加入一个过滤器(filter),过滤器类是:org.apache.struts2.dispatcher.FilterDispatcher 。Struts 2是通过FilterDispatcher这个过滤器(filter)来实现的。
4. 建立index.html文件。
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>index.html</title>
5 <meta http-equiv="refresh" content="0;URL=example/HelloWorld.action" />
6 </head>
7
8 <body>
9 <p>Loading </p>
10 </body>
11 </html>
12
index.html文件只是用于导向作用。这里有个规则:"Link actions not pages."。意思是说:通过action来访问web,而不是jsp文件。所以,我在index.html导向dao一个HelloWorld.action,而不是HelloWorld.jsp。
5. HelloWorld.jsp文件
2 <%@ taglib prefix="s" uri="/struts-tags"%>
3
4 <?xml version="1.0" encoding="utf-8" ?>
5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
6 <html xmlns="http://www.w3.org/1999/xhtml">
7 <head>
8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9 <title><s:text name="helloworld.message"></s:text>
10 </title>
11 </head>
12 <body>
13 <h2>
14 <s:property value="message" />
15 </h2>
16 <ul>
17 <li>
18 <s:url id="url" action="HelloWorld">
19 <s:param name="request_locale">en</s:param>
20 </s:url>
21 <s:a href="${url}">English</s:a>
22 </li>
23 <li>
24 <s:url id="url" action="HelloWorld">
25 <s:param name="request_locale">zh</s:param>
26 </s:url>
27 <s:a href="${url}">Chinese</s:a>
28 </li>
29 </ul>
30 </body>
31 </html>
32
在实际的项目中,页面的内容可能会比较复杂,可以用dreaweaver来先用静态的文本来填充布局,然后,再用struts 标记来代替这些文本。新建的html和jsp文件,尽可能的遵循xhtml的标准。
在文件的开头,为了使用struts 2标记,必须有这样的一行:
在struts 2 中使用文本有两种方法:本项目都采用了,这也是为了演示Struts 2的功能。
从properties配置文件中读取。如:<s:text name="helloworld.message" /> 就会从properties中读取配置文件中helloworld.message这个配置。
从bean的属性中读取。如:<s:property value="message" />,这就会从本页面相关的上下文中查找到message这个属性并显示出来。
<s:url />和<s:a />标记,在这里不打算详细介绍。
6. Action文件
在本项目中,ExampleSupport类继承自ActionSupport类,HelloWorld又继承自ExampleSupport类。在这里ExampleSupport类没有做任何事情。但在实际的项目中,可以把项目中其他Action所需的公共的内容,写在ExampleSupport类中。我这里是尽可能的按项目的方式开发,而完全是按照玩具的方式开发。
ExampleSupport.java文件
2
3 import com.opensymphony.xwork2.ActionSupport;
4
5 public class ExampleSupport extends ActionSupport {
6 }
7
HelloWorld.java文件
2
3public class HelloWorld extends ExampleSupport {
4
5 private String message;
6
7 public String getMessage() {
8 return message;
9 }
10
11 public void setMessage(String message) {
12 this.message = message;
13 }
14
15 @Override
16 public String execute() throws Exception {
17 setMessage(getText("helloworld.message"));
18 return SUCCESS;
19 }
20
21}
22
7. 建立Struts配置文件
在struts2-core-2.0.6.jar文件中,有struts-2.0.dtd文件。Struts就是通过该文件来验证struts配置文件的。在本项目中,有两个配置文件:struts.xml文件和example.xml文件。
struts.xml文件:
2 <! DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7 <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
8 <constant name="struts.devMode" value="true"></constant>
9
10 <include file="example.xml"></include>
11
12 </struts>
13
example.xml文件
2 <! DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7 <package name="example" namespace="/example" extends="struts-default">
8 <action name="HelloWorld" class="jeff.struts2.helloworld.HelloWorld">
9 <result>/example/HelloWorld.jsp</result>
10 </action>
11 </package>
12 </struts>
13
在这里,我再说一句:本项目尽可能的按项目的方式开发,而完全是按照玩具的方式开发。因为在实际项目中,Struts要配置的内容可能有很多,为了方便编辑和维护struts配置文件,把struts.xml文件拆分成多个文件,然后在struts.xml文件中用<include file />标记把这些配置文件加载进来。
8. properties配置文件
我喜欢在页面每加入一个需要该配置文件中的配置时,就往package.properties中加入一项。在适当的时候,再来把package.properties文件翻译成不同语言的配置文件。
package.properties
package_en.properties
package_zh.properties
不能在中文的pakcage_zh.properties配置文件中,直接写汉字。在eclipse中有插件,可以直接转换。Sun公司的jdk自带有个工具转换,叫做native2ascii。我在eclipse中配置了一个外部工具,工具配置如下:
图表 4
native2ascii的命令格式是这样的:native2ascii sourcefile desitationfile。这个工具配置用到了一个技巧。就是在package_zh文件(注意:该文件没有扩展名)中写汉字,然后运行该工具,就生成了package_zh.properites文件。每次改动package_zh文件,并要部署项目前,先运行一下该工具。
package_zh文件的内容如下: