1.如何安装struts:
首先到http://jakarta.apache.org/struts下载struts,建议使用release版,现在最高版本为1.2.6,有多种os版本(windows,linus...),下载后解压开来,可以看到这个目录:lib和webapps,webapps下有一些war文件。假设你的tomcat装在c:tomcat下,则将那些war文件拷贝到c:tomcatwebapps,重新启动tomcat即可。打开浏览器,在地址栏中输入:http://localhost:8080/struts-example/index.jsp,若能见到“powered by struts”的深蓝色图标,即说明成功了。这是struts自带的一个例子,附有详细的说明文档,可以做为初学者的入门教程。另外,struts还提供了一系统实用对象:xml处理、通过java reflection apis自动处理javabeans属性、国际化的提示和消息等
2.练习做一个实例:
    一个用户注册系统,用户通过网页输入相关信息:注册id号,密码,email,若注册成功,则返回成功提示信息,反之出现注册失败提示信息。 
以下是相关文件的部分核心代码。 
项目建立:
  正式开发前,需要在tocmat(我的tomcat装在c: omcat)中建立此项目。比较快的一种建立方式为:在c: omcatwebapps下新建目录test,再将c: omcatwebappsstruts-example下的 
web-inf目录拷贝到test目录下,然后将testweb-inf下的src和classes目录清空,以及struts-config.xml文件中内容清空即可。这样,我们需要的struts类包及相关的配置文件就都齐了。开发时,将jsp文件放在test目录下,java原文件放在testweb-infsrc下,编译后的类文件放在testweb-infclasses下。 
注册页面:reguser.jsp
<%@ page contenttype="text/html;charset=utf-8" language="java" %> 
<%@ taglib uri="/web-inf/struts-bean.tld" prefix="bean" %> 
<%@ taglib uri="/web-inf/struts-html.tld" prefix="html" %> 
<html:html locale="true"> 
<head> 
<title>reguser</title> 
<html:base/> 
</head> 
<body bgcolor="white"> 
<html:errors/> 
<html:form action="/reguseraction" focus="logname"> 
<table border="0" width="100%"> 
<tr> 
<th align="right"> 
logname: 
</th> 
<td align="left"> 
<html:text property="logname" size="20" maxlength="20"/> 
</td> 
</tr> 
<tr> 
<th align="right"> 
password: 
</th> 
<td align="left"> 
<html:password property="password" size="20" maxlength="20"/> 
</td> 
</tr> 
<tr> 
<th align="right"> 
e-mail: 
</th> 
<td align="left"> 
<html:password property="email" size="30" maxlength="50"/> 
</td> 
</tr> 
<tr> 
<td align="right"> 
<html:submit property="submit" value="submit"/> 
</td> 
<td align="left"> 
<html:reset/> 
</td> 
</tr> 
</table> 
</html:form> 
</body> 
</html:html> 
此jsp页面不同于普通的jsp页,因为它大量运用了taglib,这些taglib对初学者而言,可能难于掌握,可这却是struts的精华之一。灵活运用,将大大提高开发效率。
struts-config.xml:
<struts-config> 
<form-beans> 
<form-bean name="reguserform" 
type="org.cjea.struts.example. reguserform "/> 
</form-beans> 
<action-mappings> 
<action path="/reguseraction" 
type=" org.cjea.struts.example.reguseraction " 
attribute=" reguserform " 
scope="request" 
validate="false"> 
<forward name="failure" path="/ messagefailure.jsp"/> 
<forward name="success" path="/ messagesuccess.jsp"/> 
</action> 
</action-mappings> 
</struts-config> 
struts的核心是controller,即actionservlet,而actionservlet的核心就是struts-config.xml,struts-config.xml集中了所有页面的导航定义。对于大型的web项目,通过此配置文件即可迅速把握其脉络,这不管是对于前期的开发,还是后期的维护或升级都是大有裨益的。掌握struts-config.xml是掌握struts的关键所在。
formbean:reguserform
package org.cjea.struts.example;
import javax.servlet.http.httpservletrequest; 
import org.apache.struts.action.actionform; 
import org.apache.struts.action.actionmapping; 
public final class reguserform extends actionform{
private string logname; 
private string password; 
private string email; 
public reguserform(){ 
logname = null; 
password = null; 
email = null; 
} 
public string getlogname() { 
return this.logname; 
} 
public void setlogname(string logname) { 
this.logname = logname; 
} 
public void setpassword(string password) { 
this.password = password; 
} 
public string getpassword() { 
return this.password; 
} 
public void setemail(string email) { 
this.email = email; 
} 
public string getemail() { 
return this.email; 
} 
public void reset(actionmapping mapping, httpservletrequest request) 
{ 
logname = null; 
password = null; 
email = null; 
} 
} 
每一个formbean 都必须继承actionform类,formbean是对页面请求的封装。即把http request 封装在一个对象中,需要说明的一点就是多个http request可以共用一个formbean,便于维护和重用。
actionbean:reguseraction
package org.cjea.struts.example;
import javax.servlet.http.*; 
import org.apache.struts.action.*; 
public final class reguseraction extends action 
{ 
public actionforward perform(actionmapping mapping, 
actionform form, httpservletrequest req, 
httpservletresponse res) 
{ 
string title = req.getparameter("title"); 
string password = req.getparameter("password"); 
string email = req.getparameter("email"); 
/* 
取得用户请求,做相应数据库操作,略 
*/ 
} 
} 
formbean的产生是为了提供数据给actionbean,在actionbean中可以取得formbean中封装的数据,经相应的逻辑处理后,调用业务方法完成相应业务要求。
servlet的演变:在常规的 jsp,servlet,javabean三层结构中,jsp实现view的功能,servlet实现controller的功能,javabean实现model的实现。
在struts中,将常规情况下的servlet拆分与actionservlet、formbean、actionbean三个部分。actionservlet配合struts-config.xml,专职完成页面导航,而不再负责具体的数据获取与相应逻辑,这两部分功能由formbean和actionbean来完成。
3.struts优缺点
优点:
struts跟tomcat、turbine等诸多apache项目一样,是开源软件,这是它的一大优点。使开发者能更深入的了解其内部实现机制。
除此之外,struts的优点主要集中体现在两个方面:taglib和页面导航。taglib是struts的标记库,灵活动用,能大大提高开发效率。另外,就目前国内的jsp开发者而言,除了使用jsp自带的常用标记外,很少开发自己的标记,或许struts是一个很好的起点。
关于页面导航,我认为那将是今后的一个发展方向,事实上,这样做,使系统的脉络更加清晰。通过一个配置文件,即可把握整个系统各部分之间的联系,这对于后期的维护有着莫大的好处。尤其是当另一批开发者接手这个项目时,这种优势体现得更加明显。
缺点:
taglib是struts的一大优势,但对于初学者而言,却需要一个持续学习的过程,甚至还会打乱你网页编写的习惯,但是,当你习惯了它时,你会觉得它真的很棒。
struts将mvc的controller一分为三,在获得结构更加清晰的同时,也增加了系统的复杂度。
struts从产生到现在还不到半年,但已逐步越来越多运用于商业软件。虽然它现在还有不少缺点,但它是一种非常优秀的j2ee mvc实现方式,如果你的系统准备采用j2ee mvc架构,那么,不妨考虑一下struts。
4.struts实施经验:
1)、基于struts架构的项目开发,首先需要有一个很好的整体规划,整个系统中包括哪几个模块,每个模块各需要多少formbean和actionbean等,而且最好有专人负责struts-config.xml的管理。开发基于struts的项目的难点在于配置管理,尤其是对struts-config.xml的管理
2)、如果你的项目非常紧,并且项目组中又没有富有经验的struts开发人员,建议不要冒然采用struts。struts的掌握需要一个过程,对于一个熟练的jsp程序员,自学大概需要半个月左右的时间。如果结合titls,则需要更长的时间
3)、如果你在网页中大量运用taglib,那么你的美工将做出部分牺牲。当你结合tiles,功能增强的同时,这种牺牲尤为明显。当然,你对功能和美观的取舍由你自己决定
4)、taglib是一个好东西,但灵活运用它却需要一个过程,如果你不想在taglib上花太多的时间,那么只需理解与form有关的几个标记,其它的标记就放着吧,以后再看,先去研究actionservlet和struts-config.xml,你会觉得很有成就感
5)、struts是否只适合于大型项目呢?no!struts适合于各种大小的项目,当然,对于大型项目,它所体现出来的优势更加明显。
中国最大的web开发资源网站及技术社区,新闻热点
疑难解答