为Struts 2.0做好准备

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

MVC 2模型的使用
功能齐全的标志库(Tag Library)
开放源代码
但是,所谓“金无赤金,人无完人”,Struts自身也有不少的缺点:

需要编写的代码过多,容易引起“类爆炸”
单元测试困难
这些缺点随着Web的发展越来越明显。这就促生了Struts 2.0,它的诞生能很好的解决上述问题。 好啦,废话就不多说了,现在就让我们感受一下的Struts 2.0的魅力吧。

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

打开Eclipse 3.2新建Web工程
点击菜单File\New\Project,出现对话框

选择Web\Dynamic Web Project,点击“Next”,出现对话框

在“Project Name”中键入Struts2_HelloWorld,点击“New”,出现对话框

选择“Apache\Apache Tomat v5.5”,点击“Next”,出现对话框

点击“Finish”,关闭对话框。

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

按ctr+a全选,复制,再转到Eclipse窗口,在“Project Explorer”子窗口中选中Struts2_HelloWorld\WebContent\WEB-INF\lib,然后粘贴。经过Eclipse自动刷新“Project Explorer”子窗口,刚才所粘贴的jar文件应该会出现在Struts2_HelloWorld\Java Resources: src\Libraries\Web App Libraries下


打开web.xml文件,将其修改为以下代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Struts 2.0 Hello World</display-name>
<filter>
<filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


新建struts.xml文件
右键点击,Struts2_HelloWorld\Java Resources: src,出现菜单
点击“Other”,出现新建对话框
点击“Next”,出现新建文件对话框
在“File name”中键入sturts.xml,点击“Finish”,然后将struts.xml的内容修改为:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
</struts>


新建index.html文件
右键点击Struts2_HelloWorld\WebContent,出现菜单
点击“Other”,出现新建对话框
选择Web\HTML,点击“Next”出现对话框
在“File Name”中键入index.html,点击“Next”,出现对话框
点击“Finish”,将index.html的内容修改为以下内容:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h3>Hello World!</h3>
</body>
</html>


将应用程序打包到tomcat上
右键点击Struts_HelloWorld,出现菜单
点击“Export\WAR file”,出现对话框
选择“Web\WAR file”,点击“Next”,出现对话框
输入war文件的路径(如%tomcat%\webapps\Struts2_HelloWorld.war),点击“Finish”关闭对话框。
启动tomcat,运行应用程序
打开你的Internet Explorer,键入http://localhost:8080/Struts2_HelloWorld/,窗口输出Hello World窗口

第一个Struts 2.0应用程序——Hello World
新建类包(package)
右键点击Struts2_HelloWorld\Java Resources: src,出现菜单
点击“New\Package”,出现对话框
在“Name”键入tutorial,点击“Finish”关闭对话框。

新建HelloWorld.java文件
右键点击Struts2_HelloWorld\Java Resources: src\tutorial,出现菜单
点击“New\Class”,出现对话框
在“Name”中键入HelloWorld,在“Superclass”中键入com.opensymphony.xwork2.ActionSupport,点击“Finish”关闭对话框。将HelloWorld.java的内容修改为:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String execute() {
name = "Hello, " + name + "!";
return SUCCESS;
}
}

在struts.xml中添加action映射(mapping)

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>


新建SayHello.jsp
参考“新建index.html文件”步骤,弹出对话框
点击“Next”, 进入下一步
在“File name”键入SayHello.jsp,点击“Next”进入下一步
点击“Finish”关闭对话框,并将SayHello.jsp的内容修改为:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Say Hello</title>
</head>
<body>
<h3>Say "Hello" to: </h3>
<s:form action="HelloWorld">
Name: <s:textfield name="name" />
<s:submit />
</s:form>
</body>
</html>


新建HelloWorld.jsp(请参考上一步),HelloWorld.jsp的内容为:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="name" /></h3>
</body>
</html>


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

启动tomcat,运行测试
打开Internet Explorer,键入http://localhost:8080/Struts2_HelloWorld/SayHello.jsp,窗口输出SayHello.jsp
在“Name”键入字符串(如World),点击Submit,转到HelloWorld.jsp页面

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

新建JUnit单元测试
右键点击Struts2_HelloWorld\Java Resources: src\tutorial,弹出点击“Next\Other”
选择“Java\JUnit\JUnit Test Case”,点击“Next”
选择“New JUnit 4 test”,在“Name”中键入HelloWorldTest,在“Class under test”键入tutorial.HelloWorld,点击“Next”
选中HelloWorld\execute方法,点击Finish。如果生成的HelloWorldTest.java文件的图标(Icon)出现红色交叉标志,请进行以下步骤添加JUnit 4的jar包。

右键点击Struts2_HelloWorld,出现菜单。
点击“Build Path\Add Libararis”,弹出对话框
选中“JUnit”,点击“Next”
选择“JUnit 4”,点击“Finish”关闭对话框,并将HelloWorldTest.java的内容修改为:

package tutorial;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldTest {

@Test
public void testExecute() {
HelloWorld hello = new HelloWorld();
hello.setName("World");
String result = hello.execute();

assertTrue("Expected a success result!", ActionSupport.SUCCESS.equals(result));

final String msg = "Hello, World!";
assertTrue("Expected the default message!", msg.equals(hello.getName()));
}

}


运行单元测试
右键点击Struts2_HelloWorld\Java Resources: src\tutorial\HelloWorldTest.java,弹出菜单

点击“Run As\JUnit Test”,出现JUnit子窗口

绿色矩形表示,所有单元测试通过。

总结
上面的例子简单地演示了,Web 应用程序的基本操作,也即是,页面输入->Action处理->再输出到另外页面。Struts 2.0的简单易用、方便测试相信也会给大家留下不错的印象吧。我相信,Struts 2.0作为一个全新的Web架构,将会再次掀起Web开发的热潮。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值