Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点:

  • MVC 2模型的使用
  • 功能齐全的标志库(Tag Library)
  • 开放源代码

但是,所谓“金无赤金,人无完人”,Struts自身也有不少的缺点:

  • 需要编写的代码过多,容易引起“类爆炸”
  • 单元测试困难

这些缺点随着Web的发展越来越明显。这就促生了Struts 2.0,它的诞生能很好的解决上述问题。 好啦,废话就不多说了,现在就让我们感受一下的Struts 2.0的魅力吧。

  1. 搭建开发和运行环境
    1. 到Apache下载Struts 2.0包

      为Struts 2.0做好准备

    2. 打开Eclipse 3.2新建Web工程

      点击菜单File/New/Project,出现如图1所示对话框
      为Struts 2.0做好准备
      图1 新建工程对话框
      选择Web/Dynamic Web Project,点击“Next”,出现图2对话框

      为Struts 2.0做好准备
      图2 新建动态Web工程对话框
      在“Project Name”中键入Struts2_HelloWorld,点击“New”,出现以下对话框

      为Struts 2.0做好准备
      图3 新建服务器运行时对话框
      选择“Apache/Apache Tomat v5.5”,点击“Next”,出现以下对话框

      为Struts 2.0做好准备
      图4新建服务器运行时对话框
      点击“Finish”,关闭对话框。

    3. 将Struts 2.0 lib下的jar文件加到工程的构建路径(build path)

      为Struts 2.0做好准备
      图5 Struts 2.0的lib目录
      按ctr +a全选,复制,再转到Eclipse窗口,在“Project Explorer”子窗口中选中Struts2_HelloWorld/WebContent/WEB-INF/lib,然后粘贴。经过Eclipse自 动刷新“Project Explorer”子窗口,刚才所粘贴的jar文件应该会出现在Struts2_HelloWorld/Java Resources: src/Libraries/Web App Libraries下,如图6所示:

      为Struts 2.0做好准备
      图6 Project Explorer子窗口

    4. 打开web.xml文件,将其修改为以下代码:
      为Struts 2.0做好准备<?xmlversion="1.0"encoding="ISO-8859-1"?>
      为Struts 2.0做好准备
      <!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
      为Struts 2.0做好准备
      <web-app>
      为Struts 2.0做好准备
      <display-name>Struts2.0HelloWorld</display-name>
      为Struts 2.0做好准备
      <filter>
      为Struts 2.0做好准备
      <filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      为Struts 2.0做好准备
      </filter>
      为Struts 2.0做好准备
      <filter-mapping>
      为Struts 2.0做好准备
      <filter-name>struts2</filter-name>
      为Struts 2.0做好准备
      <url-pattern>/*</url-pattern>
      为Struts 2.0做好准备
      </filter-mapping>
      为Struts 2.0做好准备
      <welcome-file-list>
      为Struts 2.0做好准备
      <welcome-file>index.html</welcome-file>
      为Struts 2.0做好准备
      </welcome-file-list>
      为Struts 2.0做好准备
      </web-app>
    5. 新建struts.xml文件

      右键点击,Struts2_HelloWorld/Java Resources: src,出现如图7所示菜单
      为Struts 2.0做好准备
      图7 新建Other菜单
      点击“Other”,出现新建对话框,如图8所示

      为Struts 2.0做好准备
      图8 新建对话框
      点击“Next”,出现新建文件对话框,如图9所示

      为Struts 2.0做好准备
      图9 新建文件对话框
      在“File name”中键入sturts.xml,点击“Finish”,然后将struts.xml的内容修改为:

      为Struts 2.0做好准备<!DOCTYPEstrutsPUBLIC
      为Struts 2.0做好准备"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
      为Struts 2.0做好准备"http://struts.apache.org/dtds/struts-2.0.dtd"
      >
      为Struts 2.0做好准备
      <struts>
      为Struts 2.0做好准备
      <includefile="struts-default.xml"/>
      为Struts 2.0做好准备
      </struts>
    6. 新建index.html文件

      右键点击Struts2_HelloWorld/WebContent,出现如图10所示的菜单
      为Struts 2.0做好准备
      图10 新建Other菜单
      点击“Other”,出现新建对话框,如图11所示

      为Struts 2.0做好准备
      图11 新建对话框
      选择Web/HTML,点击“Next”出现如图12所示的对话框

      为Struts 2.0做好准备
      图12 新建HTML页面对话框
      在“File Name”中键入index.html,点击“Next”,出现如图13所示的对话框

      为Struts 2.0做好准备
      图13 模板选择对话框
      点击“Finish”,将index.html的内容修改为以下内容:

      为Struts 2.0做好准备<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
      为Struts 2.0做好准备
      <html>
      为Struts 2.0做好准备
      <head>
      为Struts 2.0做好准备
      <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
      为Struts 2.0做好准备
      <title>HelloWorld</title>
      为Struts 2.0做好准备
      </head>
      为Struts 2.0做好准备
      <body>
      为Struts 2.0做好准备
      <h3>HelloWorld!</h3>
      为Struts 2.0做好准备
      </body>
      为Struts 2.0做好准备
      </html>
    7. 将应用程序打包到tomcat上

      右键点击Struts_HelloWorld,出现如图14所示的菜单
      为Struts 2.0做好准备
      图14 工程菜单
      点击“Export/WAR file”,出现如图15所示的对话框

      为Struts 2.0做好准备
      图15 输出对话框
      选择“Web/WAR file”,点击“Next”,出现如图16所示的对话框

      为Struts 2.0做好准备
      图16 输出路径对话框
      输入war文件的路径(如%tomcat%/webapps/Struts2_HelloWorld.war),点击“Finish”关闭对话框。

    8. 启动tomcat,运行应用程序

      打开你的Internet Explorer,键入http://localhost:8080/Struts2_HelloWorld/,窗口输出如图17所示
      为Struts 2.0做好准备
      图17 Hello World窗口

  2. 第一个Struts 2.0应用程序——Hello World
    1. 新建类包(package)

      右键点击Struts2_HelloWorld/Java Resources: src,出现如图18所示菜单
      为Struts 2.0做好准备
      图18 新建菜单"
      点击“New/Package”,出现如图19所示对话框

      为Struts 2.0做好准备
      图19新建Java类包对话框
      在“Name”键入tutorial,点击“Finish”关闭对话框。

    2. 新建HelloWorld.java文件

      右键点击Struts2_HelloWorld/Java Resources: src/tutorial,出现如图20所示菜单
      为Struts 2.0做好准备
      图20 新建菜单
      点击“New/Class”,出现如图21所示对话框

      为Struts 2.0做好准备
      图21 新建Java类对话框
      在“Name”中键入HelloWorld,在“Superclass”中键入com.opensymphony.xwork2.ActionSupport,点击“Finish”关闭对话框。将HelloWorld.java的内容修改为:

      为Struts 2.0做好准备packagetutorial;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      importcom.opensymphony.xwork2.ActionSupport;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      publicclassHelloWorldextendsActionSupport{
      为Struts 2.0做好准备
      privateStringname;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      publicStringgetName(){
      为Struts 2.0做好准备
      returnname;
      为Struts 2.0做好准备}

      为Struts 2.0做好准备
      为Struts 2.0做好准备
      publicvoidsetName(Stringname){
      为Struts 2.0做好准备
      this.name=name;
      为Struts 2.0做好准备}

      为Struts 2.0做好准备
      为Struts 2.0做好准备
      publicStringexecute(){
      为Struts 2.0做好准备name
      ="Hello,"+name+"!";
      为Struts 2.0做好准备
      returnSUCCESS;
      为Struts 2.0做好准备}

      为Struts 2.0做好准备}
    3. 在struts.xml中添加action映射(mapping)
      为Struts 2.0做好准备<!DOCTYPEstrutsPUBLIC
      为Struts 2.0做好准备"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
      为Struts 2.0做好准备"http://struts.apache.org/dtds/struts-2.0.dtd"
      >
      为Struts 2.0做好准备
      <struts>
      为Struts 2.0做好准备
      <includefile="struts-default.xml"/>
      为Struts 2.0做好准备
      <packagename="tutorial"extends="struts-default">
      为Struts 2.0做好准备
      <actionname="HelloWorld"class="tutorial.HelloWorld">
      为Struts 2.0做好准备
      <result>HelloWorld.jsp</result>
      为Struts 2.0做好准备
      </action>
      为Struts 2.0做好准备
      </package>
      为Struts 2.0做好准备
      </struts>
    4. 新建SayHello.jsp

      参考“新建index.html文件”步骤,弹出如图22所示对话框
      为Struts 2.0做好准备
      图22 新建对话框
      点击“Next”, 进入下一步,如图23所示

      为Struts 2.0做好准备
      图23 新建JSP对话框
      在“File name”键入SayHello.jsp,点击“Next”进入下一步,如图24所示

      为Struts 2.0做好准备
      图24 模板选择对话框
      点击“Finish”关闭对话框,并将SayHello.jsp的内容修改为:

      为Struts 2.0做好准备<%@pagecontentType="text/html;charset=UTF-8"%>
      为Struts 2.0做好准备
      <%@taglibprefix="s"uri="/struts-tags"%>
      为Struts 2.0做好准备
      <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
      为Struts 2.0做好准备
      <html>
      为Struts 2.0做好准备
      <head>
      为Struts 2.0做好准备
      <title>SayHello</title>
      为Struts 2.0做好准备
      </head>
      为Struts 2.0做好准备
      <body>
      为Struts 2.0做好准备
      <h3>Say"Hello"to:</h3>
      为Struts 2.0做好准备
      <s:formaction="HelloWorld">
      为Struts 2.0做好准备Name:
      <s:textfieldname="name"/>
      为Struts 2.0做好准备
      <s:submit/>
      为Struts 2.0做好准备
      </s:form>
      为Struts 2.0做好准备
      </body>
      为Struts 2.0做好准备
      </html>
    5. 新建HelloWorld.jsp(请参考上一步),HelloWorld.jsp的内容为:
      为Struts 2.0做好准备<%@pagecontentType="text/html;charset=UTF-8"%>
      为Struts 2.0做好准备
      <%@taglibprefix="s"uri="/struts-tags"%>
      为Struts 2.0做好准备
      <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
      为Struts 2.0做好准备
      <html>
      为Struts 2.0做好准备
      <head>
      为Struts 2.0做好准备
      <title>Hello</title>
      为Struts 2.0做好准备
      </head>
      为Struts 2.0做好准备
      <body>
      为Struts 2.0做好准备
      <h3><s:propertyvalue="name"/></h3>
      为Struts 2.0做好准备
      </body>
      为Struts 2.0做好准备
      </html>
    6. 重新打包发布应用程序

      先停止tomcat, 再将tomcat里webapps下的Struts2_HelloWorld.war和Struts2_HelloWorld文件夹删除,参照“将应用程序打包到tomcat上”重新发布应用程序。

    7. 启动tomcat,运行测试

      打开Internet Explorer,键入http://localhost:8080/Struts2_HelloWorld/SayHello.jsp,窗口输出如图25所示
      为Struts 2.0做好准备
      图25 SayHello.jsp
      在“Name”键入字符串(如World),点击Submit,转到HelloWorld.jsp页面,如图26所示

      为Struts 2.0做好准备
      图26 HelloWorld.jsp

  3. 单元测试Hello World

    在文章开始的时候提及,单元测试困难是Struts一大缺点。现在让我们在体验一下,在Struts 2.0中是如何进行测试的。

    1. 新建JUnit单元测试

      右键点击Struts2_HelloWorld/Java Resources: src/tutorial,弹出如图27所示对话框
      为Struts 2.0做好准备
      图27 新建菜单
      点击“Next/Other”

      为Struts 2.0做好准备
      图28 新建对话框
      选择“Java/JUnit/JUnit Test Case”,点击“Next”

      为Struts 2.0做好准备
      图29 新建JUnit 测试用例对话框
      选择“New JUnit 4 test”,在“Name”中键入HelloWorldTest,在“Class under test”键入tutorial.HelloWorld,点击“Next”

      为Struts 2.0做好准备
      图30 选择方法对话框
      选中HelloWorld/execute方法,点击Finish。如果生成的HelloWorldTest.java文件的图标(Icon)出现红色交叉标志,请进行以下步骤添加JUnit 4的jar包。

      右键点击Struts2_HelloWorld,出现如图所示菜单。为Struts 2.0做好准备
      图31 新建菜单
      点击“Build Path/Add Libararis”,弹出图32对话框

      为Struts 2.0做好准备
      图32 添加库对话框
      选中“JUnit”,点击“Next”

      为Struts 2.0做好准备
      图33 选择版本对话框
      选择“JUnit 4”,点击“Finish”关闭对话框,并将HelloWorldTest.java的内容修改为:

      为Struts 2.0做好准备packagetutorial;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      importstaticorg.junit.Assert.assertTrue;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      importorg.junit.Test;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      importcom.opensymphony.xwork2.ActionSupport;
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      publicclassHelloWorldTest{
      为Struts 2.0做好准备
      为Struts 2.0做好准备@Test
      为Struts 2.0做好准备
      publicvoidtestExecute(){
      为Struts 2.0做好准备HelloWorldhello
      =newHelloWorld();
      为Struts 2.0做好准备hello.setName(
      "World");
      为Struts 2.0做好准备Stringresult
      =hello.execute();
      为Struts 2.0做好准备
      为Struts 2.0做好准备assertTrue(
      "Expectedasuccessresult!",ActionSupport.SUCCESS.equals(result));
      为Struts 2.0做好准备
      为Struts 2.0做好准备
      finalStringmsg="Hello,World!";
      为Struts 2.0做好准备assertTrue(
      "Expectedthedefaultmessage!",msg.equals(hello.getName()));
      为Struts 2.0做好准备}

      为Struts 2.0做好准备
      为Struts 2.0做好准备}
    2. 运行单元测试

      右键点击Struts2_HelloWorld/Java Resources: src/tutorial/HelloWorldTest.java,弹出如图34所示菜单
      为Struts 2.0做好准备
      图34 运行为菜单
      点击“Run As/JUnit Test”,出现JUnit子窗口如图35所示

      为Struts 2.0做好准备
      图35 JUnit子窗口
      图35的绿色矩形表示,所有单元测试通过。

  4. 总结

    上 面的例子简单地演示了,Web 应用程序的基本操作,也即是,页面输入->Action处理->再输出到另外页面。Struts 2.0的简单易用、方便测试相信也会给大家留下不错的印象吧。我相信,Struts 2.0作为一个全新的Web架构,将会再次掀起Web开发的热潮。 不过,Struts 2.0还在测试中,正式版的发布还需些时日,所以文档方面可能有所欠缺。请大家继续留意我的博客,我会尽我所能为大家写更多关于Struts 2.0的文章。

相关文章: