论坛首页 Java版 Struts

Struts2学习笔记1

浏览 2434 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-02-01

一.下载struts2.0.1

http://struts.apache.org/downloads.html,下载struts-2.0.1-all.zip,这个压缩包中包含了开发struts2所需的struts2-core.jar核心包以及其它struts2所依赖的JAR文件,另外还有一些struts2的示例程序以及一些HTMLAPI文档。

二.试用struts2.0.1

1. 新建一个WEB工程,将struts-2.0.1-all.zip压缩包中的lib目录下的所有jar文件拷贝到WEB工程的/WEB-INF/lib目录下。

修改WEB-INF下的web.xml文件,加入如下内容:

       <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>

<!-- 这里是设置struts2标签,也可以不用设置,因为在struts-core.jarMETA-INF目录下已经包含了

    这个tld文件,J2EE容器会自动地加载它 -->

    <jsp-config>

       <taglib>

           <taglib-uri>/s</taglib-uri>         <taglib-location>/WEB-INF/tlds/struts-tags.tld</taglib-location>

       </taglib>

    </jsp-config>

web.xml中定义了一个struts2FilterDispathcerfilter,这个FilterDispatcher用来初始化struts2并且处理所有的WEB请求。

 

       2. 新建一个登录页面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<html>

<head>

<title>登录页面</title>

</head>

<body>

<s:form action="login">

    <table align="center">

    <caption><h3>用户登录</h3></caption>

       <tr>

           <td><s:textfield label="用户名" name="username" /></td>

       </tr>

       <tr>

           <td><s:password label="  " name="password" /></td>

       </tr>

       <tr align="center">

           <td><input type="submit" value="登录"/></td><td><input type="reset" value="重填" /></td>

       </tr>

    </table>

</s:form>

</body>

</html>

 

       3.编写Action login

package org.rainlife.struts2.action;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport {

    private String username;

 

    private String password;

 

    @Override

    public String execute() throws Exception {

       if (!(getUsername().equals("rainlife"))

              && !(getPassword().equals("rainl

   
时间:2007-02-01
我晕哪,javaeye的在线编辑器有问题,在word中的文本复制到编辑器就有问题。
   
0 请登录后投票
时间:2007-02-01
Struts2比起1改进了写什么啊?
   
0 请登录后投票
时间:2007-02-01
Struts2与Struts1的对比

Action 类:
• Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽象类编程而不是接口。
• Struts 2 Action类可以实现一个Action接口,也可实现其他接口,使可选和定制的服务成为可能。Struts2提供一个ActionSupport基类去实现 常用的接口。Action接口不是必须的,任何有execute标识的POJO对象都可以用作Struts2的Action对象。
线程模式:
• Struts1 Action是单例模式并且必须是线程安全的,因为仅有Action的一个实例来处理所有的请求。单例策略限制了Struts1 Action能作的事,并且要在开发时特别小心。Action资源必须是线程安全的或同步的。
• Struts2 Action对象为每一个请求产生一个实例,因此没有线程安全问题。(实际上,servlet容器给每个请求产生许多可丢弃的对象,并且不会导致性能和垃圾回收问题)

Servlet 依赖:
• Struts1 Action 依赖于Servlet API ,因为当一个Action被调用时HttpServletRequest 和 HttpServletResponse 被传递给execute方法。
• Struts 2 Action不依赖于容器,允许Action脱离容器单独被测试。如果需要,Struts2 Action仍然可以访问初始的request和response。但是,其他的元素减少或者消除了直接访问HttpServetRequest 和 HttpServletResponse的必要性。

可测性:
• 测试Struts1 Action的一个主要问题是execute方法暴露了servlet API(这使得测试要依赖于容器)。一个第三方扩展--Struts TestCase--提供了一套Struts1的模拟对象(来进行测试)。
• Struts 2 Action可以通过初始化、设置属性、调用方法来测试,“依赖注入”支持也使测试更容易。

捕获输入:
• Struts1 使用ActionForm对象捕获输入。所有的ActionForm必须继承一个基类。因为其他JavaBean不能用作ActionForm,开发者经常创建多余的类捕获输入。动态Bean(DynaBeans)可以作为创建传统ActionForm的选择,但是,开发者可能是在重新描述(创建)已经存在的JavaBean(仍然会导致有冗余的javabean)。
• Struts 2直接使用Action属性作为输入属性,消除了对第二个输入对象的需求。输入属性可能是有自己(子)属性的rich对象类型。Action属性能够通过web页面上的taglibs访问。Struts2也支持ActionForm模式。rich对象类型,包括业务对象,能够用作输入/输出对象。这种ModelDriven 特性简化了taglib对POJO输入对象的引用。

表达式语言:
• Struts1 整合了JSTL,因此使用JSTL EL。这种EL有基本对象图遍历,但是对集合和索引属性的支持很弱。
• Struts2可以使用JSTL,但是也支持一个更强大和灵活的表达式语言--"Object Graph Notation Language" (OGNL).

绑定值到页面(view):
• Struts 1使用标准JSP机制把对象绑定到页面中来访问。
• Struts 2 使用 "ValueStack"技术,使taglib能够访问值而不需要把你的页面(view)和对象绑定起来。ValueStack策略允许通过一系列名称相同但类型不同的属性重用页面(view)。
 
类型转换:
• Struts 1 ActionForm 属性通常都是String类型。Struts1使用Commons-Beanutils进行类型转换。每个类一个转换器,对每一个实例来说是不可配置的。
• Struts2 使用OGNL进行类型转换。提供基本和常用对象的转换器。

校验:
• Struts 1支持在ActionForm的validate方法中手动校验,或者通过Commons Validator的扩展来校验。同一个类可以有不同的校验内容,但不能校验子对象。
• Struts2支持通过validate方法和XWork校验框架来进行校验。XWork校验框架使用为属性类类型定义的校验和内容校验,来支持chain校验子属性

Action执行的控制:
• Struts1支持每一个模块有单独的Request Processors(生命周期),但是模块中的所有Action必须共享相同的生命周期。
• Struts2支持通过拦截器堆栈(Interceptor Stacks)为每一个Action创建不同的生命周期。堆栈能够根据需要和不同的Action一起使用。
   
0 请登录后投票
时间:2007-02-02
使用JavaEye编辑器请BBCODE的Code包含,或者RichEdit的代码输入框包含。
   
0 请登录后投票
时间:2007-02-02
to jessdy:
能否再比较一下Struts2与Webwork有哪些差别?或者说有哪些优势?
   
0 请登录后投票
时间:2007-02-02
To jessdy
您的这方面我本来也写了一些,本来想发上来了,但我的WORD文档通过RichEdit发布时,出现一些问题,像下面这些标签,不知道怎么会出来,<!-- </span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这里是设置</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt">struts2</span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">标签,也可以不用设置,因为在</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt">struts-core.jar</span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt">META-INF</span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">目录下已经包含了</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt"><o:p></o:p></span></p> <p class="MsoNormal" style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align="left"><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt"><span style="mso-tab-count: 1"> </span></span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这个</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt">tld</span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">文件,</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt">J2EE</span><span style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">容器会自动地加载它</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: "Courier New"; mso-font-kerning: 0pt"> -->
呵呵,其实struts2和webwork基本上是一样的,以前javaeye上面也说了struts2只是在webwork上面换了一下包名,xwork还是一样的,webwork已经不更新了,看来都转到struts2上面了
   
0 请登录后投票
论坛首页 Java版 Struts

跳转论坛: