java定焦点_Java:Struts入门初步知识

1.如何安装Struts:

首先到http://jakarta.apache.org/Struts下载Struts,建议使用release版,现在最高版本为1.2.6,有多种OS版本(windows,linus...),下载后解压开来,可以看到这个目录:lib和webapps,webapps下有一些WAR文件。假设你的Tomcat装在c:\Tomcat下,则将那些WAR文件拷贝到C:\Tomcat\webapps,重新启动Tomcat即可。打开浏览器,在地址栏中输入:http://localhost:8080/Struts-example/index.jsp,若能见到“powered by Struts”的深蓝色图标,即说明成功了。这是Struts自带的一个例子,附有详细的说明文档,可以做为初学者的入门教程。另外,Struts还提供了一系统实用对象:XML处理、通过Java reflection APIs自动处理JavaBeans属性、国际化的提示和消息等

2.练习做一个实例:

一个用户注册系统,用户通过网页输入相关信息:注册ID号,密码,EMAIL,若注册成功,则返回成功提示信息,反之出现注册失败提示信息。

项目建立:

正式开发前,需要在Tocmat(我的tomcat装在c:\tomcat)中建立此项目。比较快的一种建立方式为:在C:\tomcat\webapps下新建目录test,再将C:\tomcat\webapps\struts-example下的WEB-INF目录拷贝到test目录下,然后将test\WEB-INF下的src和classes目录清空,以及struts-config.xml文件中内容清空即可。这样,我们需要的Struts类包及相关的配置文件就都齐了。

开发时,将JSP文件放在test目录下,Java原文件放在test\WEB-INF\src下,编译后的类文件放在test\WEB-INF\classes下。

注册页面:reguser.jsp

%>

%>

%>

RegUser

Logname:

Password:

E-mail:

此JSP页面不同于普通的JSP页,因为它大量运用了taglib,这些taglib对初学者而言,可能难于掌握,可这却是Struts的精华之一。灵活运用,将大大提高开发效率。

type="org.cjea.Struts.example. RegUserForm "/>

type=" org.cjea.Struts.example.RegUserAction "

attribute=" regUserForm "

scope="request"

validate="false">

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适合于各种大小的项目,当然,对于大型项目,它所体现出来的优势更加明显。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts Logic标签库中包含的标签列表 Tag name Description empty 如果标签parameter,propertie等属性所指的变量值为null或空字符串,则处理标签包含的内容 equal 如果标签parameter,propertie等属性所指的变量的值等于标签value属性所指的值,则处理标签所包含的内容, 如: <logic:equal value="modify" property="action" name="projectForm"> <bean:message key="project.project_modify"/> </logic:equal> 上面的示例表示,如果projectForm的action属性等于modify,则处理<bean:message key="project.project_modify"/ >语句。 forward Forward control to the page specified by the ActionForward entry. greaterEqual Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value. iterate Repeat the nested body content of this tag over a specified collection. lessEqual Evaluate the nested body content of this tag if the requested variable is less than or equal to the specified value. lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value. match Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable. messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request. messagesPresent Generate the nested body content of this tag if the specified message is present in this request. notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string. notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值