Struts2的各种技术
来源:百度文库 编辑:神马文学网 时间:2024/05/11 12:38:14
开发工具: MyEclipse6
Web服务器: Tomcat6
Struts版本: Struts2.0.11.1
JDK版本: JDK1.5.0_12
J2EE版本: Java EE5.0
在本系列教程中 Web工程的上下文路径都是 struts2,如果在 Web根目录有一个 index.jsp文件,则访问路径如下:
http://localhost:8080/struts2/index.jsp
由于 MyEclipse6目前并不支持 Struts2,所以我们需要到 struts.apache.org去下载 Struts2安装包。要想正常使用 Struts2,至少需要如下五个包(可能会因为 Struts2的版本不同,包名略有差异,但包名的前半部是一样的)。
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
Struts2虽然在大版本号上是第二个版本,但基本上在配置和使用上已经完全颠覆了 Struts1.x的方式(当然, Struts2仍然是基于 MVC模式的,也是动作驱动的,可能这是唯一没变的东西)。 Struts2实际上是在 Webwork基础上构建起来的 MVC框架。我们从 Struts2的源代码中可以看到,有很多都是直接使用的 xwork(Webwork的核心技术 )的包。既然从技术上来说 Struts2是全新的框架,那么就让我们来学习一下这个新的框架的使用方法。
如果大家使用过 Struts1.x,应该对建立基于 Struts1.x的 Web程序的基本步骤非常清楚。让我们先来回顾一下建立基于 Struts1.x的 Web程序的基本步骤。
1. 安装 Struts。由于 Struts的入口点是 ActionServlet,所以得在 web.xml中配置一下这个 Servlet。
2. 编写 Action类(一般从 org.apache.struts.action.Action类继承)。
3. 编写 ActionForm类(一般从 org.apache.struts.action.ActionForm类继承),这一步不是必须的,如果要接收客户端提交的数据,需要执行这一步。
4. 在 struts-config.xml文件中配置 Action和 ActionForm。
5. 如果要 采集用户录入的数据,一般需要编写若干 JSP页面,并通过这些 JSP页面中的 form将数据提交给 Action。
下面我们就按着编写 struts1.x程序的 这五步和 struts2.x程序的编写过程一一对应,看看它们谁更“酷”。下面我们来编写一个基于 Struts2的 Web程序。这个程序的功能是让用户录入两个整数,并提交给一个 Struts Action,并计算这两个数的代数和,如果代码和为非负数,则跳转到 positive.jsp页面,否则跳转到 negative.jsp页面。
【第 1 步】 安装 Struts2
这一步对于 Struts1.x和 Struts2都是必须的,只是安装的方法不同。 Struts1的入口点是一个 Servlet,而 Struts2的入口点是一个过滤器 (Filter)。因此, Struts2要按过滤器的方式配置。下面是在 web.xml中配置 Struts2的代码:
< 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 >
【第 2 步】 编写 Action类
这一步和 Struts1.x 也必须进行。只是 Struts1.x 中的动作类必须从 Action 类中继承,而 Struts2.x 的动作类需要从 com.opensymphony.xwork2.ActionSupport 类继承。下面是计算两个整数代码和的 Action 类,代码如下:
package action;
import com.opensymphony.xwork2.ActionSupport;
public class FirstAction extends ActionSupport
{
private int operand1;
private int operand2;
public String execute() throws Exception
{
if (getSum() >= 0 ) // 如果代码数和是非负整数,跳到positive.jsp页面
{
return " positive " ;
}
else // 如果代码数和是负整数,跳到negative.jsp页面
{
return " negative " ;
}
}
public int getOperand1()
{
return operand1;
}
public void setOperand1( int operand1)
{
System.out.println(operand1);
this .operand1 = operand1;
}
public int getOperand2()
{
return operand2;
}
public void setOperand2( int operand2)
{
System.out.println(operand2);
this .operand2 = operand2;
}
public int getSum()
{
return operand1 + operand2; // 计算两个整数的代码数和
}
}
从上面的代码可以看出,动作类的一个特征就是要覆盖 execute方法,只是 Struts2的 execute方法没有参数了,而 Struts1.x的 execute方法有四个参数。而且 execute方法的返回值也不同的。 Struts2只返回一个 String,用于表述执行结果(就是一个标志)。上面代码的其他部分将在下面讲解。
【第 3 步】 编写 ActionForm类
在本例中当然需要使用 ActionForm了。在 Struts1.x中,必须要单独建立一个 ActionForm类(或是定义一个动作 Form),而在 Struts2中 ActionForm和 Action已经二合一了。从第二步的代码可以看出,后面的部分就是应该写在 ActionForm类中的内容。所以在第 2步,本例的 ActionForm类已经编写完成(就是 Action类的后半部分)。
【第 4 步】 配置 Action类
这一步 struts1.x 和 struts2.x 都是必须的,只是在 struts1.x 中的配置文件一般叫 struts-config.xml (当然也可以是其他的文件名),而且一般放到 WEB-INF 目录中。而在 struts2.x 中的配置文件一般为 struts.xml ,放到 WEB-INF"classes 目录中。下面是在 struts.xml 中配置动作类的代码:xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="struts2" namespace ="/mystruts"
extends ="struts-default" >
< action name ="sum" class ="action.FirstAction" >
< result name ="positive" > /positive.jsp result >
< result name ="negative" > /negative.jsp result >
action >
package >
struts >
在
http://localhost:8080/struts2/mystruts/sum.action
注 : Struts1.x的动作一般都以 .do结尾,而 Struts2是以 .action结尾。
【第 5 步】 编写用户录入接口( JSP页面)
1. 主界面( sum.jsp )
在 Web根目录建立一个 sum.jsp,代码如下:
<% @ page language = " java " import = " java.util.* " pageEncoding = " GBK " %><% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > 输入操作数 title >
head >
< body >
求代数和
< br />
< s:form action ="mystruts/sum.action" >
< s:textfield name ="operand1" label =" 操作数1" />
< s:textfield name ="operand2" label =" 操作数2" />
< s:submit value ="代数和" />
s:form >
body >
html >
在 sum.jsp中使用了 Struts2带的 tag。在 Struts2中已经将 Struts1.x的好几个标签库都统一了,在 Struts2中只有一个标签库 /struts-tags。这里面包含了所有的 Struts2标签。但使用 Struts2的标签大家要注意一下。在
... ...
求代数和
操作数 1:
操作数 2:
... ...
提示一下,在 2. positive.jsp 3. negative.jsp 这两个 jsp页面的实现代码基本一样,只使用了一个 <% com.opensymphony.xwork2.util.OgnlValueStack ovs = (com.opensymphony.xwork2.util.OgnlValueStack)request.getAttribute("struts.valueStack"); out.println(ovs.findString("sum")); %> 启动 Tomcat后,在 IE中输入如下的 URL来测试这个例子: 定位。
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > 显示代数和 title >
head >
< body >
代数和为非负整数 < h1 >< s:property value ="sum" /> h1 >
body >
html >
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > 显示代数和 title >
head >
< body >
代数和为负整数 < h1 >< s:property value ="sum" /> h1 >
body >
html >
http://localhost:8080/struts2/sum.jsp