到写本文为止,Struts的最新版本是 2.0.6。Struts 2.0.6的下载地址是: 本HelloWorld的需求很简单,只是可以通过英语和汉语两种语言来显示HelloWorld。当然,这不是自动翻译。只是在不同的环境中,读取不同的字符串。 | |
读者可能会问,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文件
1
<?
xml version="1.0" encoding="UTF-8"
?>
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文件。
1
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
2 < html xmlns ="http://www.w3.org/1999/xhtml" > 3 < head > 4 < title > index.html </ title > 5 < meta http-equiv ="refresh" content ="0;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文件
1
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=utf-8
"
pageEncoding
=
"
UTF-8
"
%>
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 ="" > 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 ="" > Chinese </ s:a > 28 </ li > 29 </ ul > 30 </ body > 31 </ html > 32 在实际的项目中,页面的内容可能会比较复杂,可以用dreaweaver来先用静态的文本来填充布局,然后,再用struts 标记来代替这些文本。新建的html和jsp文件,尽可能的遵循xhtml的标准。 在文件的开头,为了使用struts 2标记,必须有这样的一行:
<%
@ taglib prefix
=
"
s
"
uri
=
"
/struts-tags
"
%>
在struts 2 中使用文本有两种方法:本项目都采用了,这也是为了演示Struts 2的功能。
<s:url />和<s:a />标记,在这里不打算详细介绍。 6. Action文件在本项目中,ExampleSupport类继承自ActionSupport类,HelloWorld又继承自ExampleSupport类。在这里ExampleSupport类没有做任何事情。但在实际的项目中,可以把项目中其他Action所需的公共的内容,写在ExampleSupport类中。我这里是尽可能的按项目的方式开发,而完全是按照玩具的方式开发。 ExampleSupport.java文件
1
package
jeff.struts2.helloworld; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class ExampleSupport extends ActionSupport { 6 } 7
HelloWorld.java文件
2 3 public 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 |
Struts 2 学习笔记:HelloWorld(转载)
最新推荐文章于 2024-09-22 16:53:37 发布