struts最早于2002年5月作为jakarta项目的组成部分问世,(主页:http://jakarta.apache.org/struts/index.html) jakata项目由apache foundation.运做。项目的创立者希望该项目的研究改进和提高java server pages (jsps)、servlet、标签库以及面向对象的技术水准。struts为使用mvc体系来编写应用提供了一个架构。struts使用“actionmapping”,它允许servlet将用户的请求转变为应用行为。actionmapping通常指定一个请求的路径、在请求上进行动作的对象类型,以及指定其它需要的属性。
action对象作为actionmapping的一个部分使用,负责处理请求,并且发送响应回适合的视图(通常是一个web浏览器),或者传送请求至适合的模型。
在模型和视图间的桥是一个form bean,它可以通过继承org.apache.struts.action.actionform得到。form bean用来保存处理前的用户数据,或者显示返回给用户的模型数据。struts包含有自定义的标签,它可以由创建的form bean中自动组合字段。
在实践中用户请求jsp页面,控制器servlet接收请求并决定由哪个应用处理它。action对象传送该请求到包含有接收商业逻辑的javabean中。商业逻辑bean将连接数据库并进行查询,接收结果,然后返回结果给action对象。action对象将结果作为请求的一部分存放到一个form bean中。一旦收集完请求的全部数据,就可以格式化结果并且显示。最后是javaserver page以html表格的形式将结果显示出来。
struts的安装配置
我的配置是居于tomcat4.0以上版本讨论,其他的appserver大致相同。
1:得到struts1.1
http://www.apache.org/dist/jakarta/struts/binaries/jakarta-struts-1.1-rc1.zip
到今天(2003-05-11)为止struts最新为struts1.1rc1版.
2:设置
把struts.jar copy到$tomcat_home/common/lib 或你使用struts的appaction下的web-inf/lib下
在你使用struts的appaction下web.xml中增加下列配置
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.actionservlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<taglib>
<taglib-uri>/web-inf/struts-html.tld</taglib-uri>
<taglib-location>/web-inf/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/struts-logic.tld</taglib-uri>
<taglib-location>/web-inf/struts-logic.tld</taglib-location>
</taglib>
<!-- nested tag library descriptor -->
<taglib>
<taglib-uri>/web-inf/struts-nested.tld</taglib-uri>
<taglib-location>/web-inf/struts-nested.tld</taglib-location>
</taglib>
<!-- template tag library descriptor -->
<taglib>
<taglib-uri>/web-inf/struts-template.tld</taglib-uri>
<taglib-location>/web-inf/struts-template.tld</taglib-location>
</taglib>
<!--tiles tag library descriptor-->
<taglib>
<taglib-uri>/web-inf/struts-tiles.tld</taglib-uri>
<taglib-location>/web-inf/struts-tiles.tld</taglib-location>
</taglib>
其中这个配置文件是在tomcat启动的时候把struts加载到内存中,以使这个application能使用struts的设计模式。其中最前面的代码指加载actionservlet,其中其配置文件是在/web-inf/struts-config.xml(这是初学者最重要的配置文件,经常要使用),而后面的代码与前面的也差不多,是为了加载一些标签库,方便重用显示的组件。(这个是比较新的内容,我个人的意见是,如果你你是刚学struts的话,那先把这方面省去。以下是一个有经验的使用者的心得!
1、 如果你的项目非常紧,并且项目组中又没有富有经验的struts开发人员,建议不要冒然采用struts。struts的掌握需要一个过程,对于一个熟练的jsp程序员,自学大概需要半个月左右的时间。如果结合titls,则需要更长的时间。
2、 如果你在网页中大量运用taglib,那么你的美工将做出部分牺牲。当你结合tiles,功能增强的同时,这种牺牲尤为明显。当然,你对功能和美观的取舍由你自己决定。
3、 taglib是一个好东西,但灵活运用它却需要一个过程,如果你不想在taglib上花太多的时间,那么只需理解与form有关的几个标记,其它的标记就放着吧,以后再看,先去研究actionservlet和struts-config.xml,你会觉得很有成就感。
4、 struts的诞生时间虽不长,但与之相关的工具却越来越多,如果你是用jbuilder作为开发工具,那我可以为你推荐几款优秀的open tools,极大的提高开发效率。
以下是一个使用struts的例子,但为了能使初学者迅速走进strust的大门,我先省去 tiles,与taglib部门,这样strust看起来就相当简单了!
reg.jsp---用户注册页面
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>注册窗口</title>
</head>
<body>
<form name="form1" method="post" action="regok.esp">
<p> 用户名:
<input name="username" type="text" id="username">
</p>
<p> 密码:
<input name="password" type="password" id="password">
</p>
<p>
<input type="submit" name="submit" value="提交">
<input type="reset" name="submit2" value="重置">
</p>
</form>
</body>
</html>
与一般的注册页面没有什么区别(当然也是因为我没有用到taglib,tiles而已)
useregactiom.java
package com.test;
import java.io.ioexception;
import java.util.locale;
//servlet import
import javax.servlet.servletexception;
import javax.servlet.http.httpsession;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
//struts import
import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.util.messageresources;
//finance import
import com.test.action.userform;
import com.test.manager.usermanager;
import com.test.entity.user;
public class useregaction extends action {
public actionforward execute(actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws ioexception, servletexception {
try{
'取得userform的数据,当然userform是封装了注册页面上的数据!
userform userform=(userform)form;
'这两段代码是用来向数据库增加数据的,与以前的一样。
usermanager usermanager=new usermanager();
user user=usermanager.formtoentity(userform);
usermanager.add(user);
//insert into database a userinfo
}catch(exception ex){
return mapping.findforward("error");
}
return mapping.findforward("success");
//forward is "/msg.jsp"
}
}
userform.java
package com.test.action;
import java.util.*;
import java.io.serializable;
//servlet import
import javax.servlet.servletexception;
import javax.servlet.http.httpsession;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
//struts import
import org.apache.struts.action.actionmapping;
public class userform extends validatorform implements serializable{
//在struts1.1以前通常form继承actionform
private string username;
private string password;
public userform(){
}
/****/
public string getusername(){
return this.username;
}
/****/
public void setusername(string _username){
this.username=_username;
}
/****/
publi
闽公网安备 35060202000074号