html>
以上基于Struts框架的JSP文件有以下特点:
●没有任何Java程序代码
●使用了许多Struts的客户化标签,例如 form>和 present>标签 ●没有直接提供文本内容,取而代之的是 message>标签,输出到网页上的文本内容都是由 message>标签来生成的。 例如: messagekey="hello.jsp.prompt.person"/> Struts客户化标签是联系视图组件和Struts框架中其它组件的纽带。 这些标签可以访问或显示来自于控制器和模型组件的数据。 在本书第12章至16章讲专门介绍Struts标签的用法,本节先简单介绍几种重要的Struts标签。 hello.jsp开头几行用于声明和加载Struts标签库: <%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%> <%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%> <%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%> 以上代码表明该JSP文件使用了StrutsBean、Html和Logic标签库,这是加载客户化标签库的标准JSP语法。 hello.jsp中使用了来自StrutsHTML标签库中的标签,包括 errors>, form>和 text>: ● errors>: 用于显示Struts框架中其他组件产生的错误消息。 ● form>: 用于创建HTML表单,它能够把HTML表单的字段和ActionFormBean的属性关联起来。 ● text>: 该标签是 form>的子标签,用于创建HTML表单的文本框。 它和ActionFormBean的属性相关联。 hello.jsp中使用了来自StrutsBean标签库的两个标签 message>和 write>: ● message>: 用于输出本地化的文本内容,它的key属性指定消息key,和消息key匹配的文本内容来自于专门的ResourceBundle,关于ResourceBundle的概念参见本书第9章(Struts应用的国际化)。 ● write>: 用于输出JavaBean的属性值。 本例中,它用于输出personbean对象的userName属性值: writename="personbean"property="userName"/> hello.jsp使用了来自StrutsLogic标签库的 present>标签。 present>标签用来判断JavaBean在特定的范围内是否存在,只有当JavaBean存在,才会执行标签主体中的内容: presentname="personbean"scope="request"> Hello writename="personbean"property="userName"/>! present> 本例中, present>标签用来判断在request范围内是否存在personbean对象,如果存在,就输出personbean的userName属性值。 和 present>标签相对的是 notPresent>标签,它表示只有当JavaBean在特定的范围内不存在,才会执行标签主体中的内容。 2.3.2创建消息资源文件 hello.jsp使用 message>标签来输出文本内容。 这些文本来自于ResourceBundle,每个ResourceBundle都对应一个或多个本地化的消息资源文件,本例中的资源文件为application.properties,例程2-2是该消息资源文件的内容。 例程2-2application.properties文件 #ApplicationResourcesforthe"Hello"sampleapplication hello.jsp.title=Hello-AfirstStrutsprogram hello.jsp.page.heading=HelloWorld! AfirstStrutsapplication hello.jsp.prompt.person=PleaseenteraUserNametosayhelloto: hello.jsp.page.hello=Hello #ValidationanderrormessagesforHelloForm.javaandHelloAction.java hello.dont.talk.to.monster=Wedon'twanttosayhellotoMonster! ! ! hello.no.username.error=PleaseenteraUserNametosayhelloto! 以上文件以“消息key/消息文本”的格式存放数据,文件中“#”后面为注释行。 对于以下JSP代码: messagekey="hello.jsp.title"/> message>标签的key属性为“hello.jsp.tilte”,在ResourceBundle中与之匹配的内容为: hello.jsp.title=Hello-AfirstStrutsprogram 因此,以上 message>标签将把“Hello-AfirstStrutsprogram”输出到网页上。 2.3.3创建ActionFormBean 当用户提交了HTML表单,Struts框架自动把表单数据组装到ActionFormBean中。 ActionFormBean中的属性和HTML表单中的字段一一对应。 ActionFormBean还提供数据验证方法,以及把属性重新设置为默认值的方法。 Struts框架中定义的ActionForm类是抽象的,必须在应用中创建它的子类,来存放具体的HTML表单数据。 例程2-3为HelloForm.java的源程序,它用于处理hello.jsp中的表单数据。 例程2-3HelloForm.java packagehello; importjavax.servlet.http.HttpServletRequest; importorg.apache.struts.action.ActionMessage; importorg.apache.struts.action.ActionErrors; importorg.apache.struts.action.ActionForm; importorg.apache.struts.action.ActionMapping; publicfinalclassHelloFormextendsActionForm{ privateStringuserName=null; publicStringgetUserName(){ return(this.userName); } publicvoidsetUserName(StringuserName){ this.userName=userName; } /** *Resetallpropertiestotheirdefaultvalues. */ publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){ this.userName=null; } /** *Validatethepropertiespostedinthisrequest.Ifvalidationerrorsare *found,returnan *Ifnovalidationerrorsoccur,return * */ publicActionErrorsvalidate(ActionMappingmapping, HttpServletRequestrequest){ ActionErrorserrors=newActionErrors(); if((userName==null)||(userName.length()<1)) errors.add("username",newActionMessage("hello.no.username.error")); returnerrors; } } 从以上代码可以看出,ActionFormBean实质上是一种JavaBean,不过它除了具有JavaBean的常规方法,还有两个特殊方法: ●validate(): 用于表单验证。 ●reset(): 把属性重新设置为默认值。 2.3.4数据验证 几乎所有和用户交互的应用都需要数据验证,而从头设计并开发完善的数据验证机制往往很费时。 幸运的是,Struts框架提供了现成的、易于使用的数据验证功能。 Struts框架的数据验证可分为两种类型: 表单验证和业务逻辑验证,在本例中,它们分别运用于以下场合: ●表单验证: 如果用户没有在表单中输入姓名,就提交表单,将生成表单验证错误 ●业务逻辑验证: 如果用户在表单中输入的姓名为“Monster”,按照本应用的业务规则,不允许向“Monster”打招呼,因此将生成业务逻辑错误。 第一种类型的验证,即表单验证由ActionFormBean来负责处理。 在本例中,HelloForm.java的validate()方法负责完成这一任务: publicActionErrorsvalidate(ActionMappingmapping, HttpServletRequestrequest){ ActionErrorserrors=newActionErrors(); if((userName==null)||(userName.length()<1)) errors.add("username",newActionMessage("hello.no.username.error")); returnerrors; } } 当用户提交了HTML表单,Struts框架自动把表单数据组装到ActionFormBean中。 接下来Struts框架会自动调用ActionFormBean的validate()方法进行表单验证。 如果validate()方法返回的ActionErrors对象为null,或者不包含任何ActionMessage对象,就表示没有错误,数据验证通过。 如果ActionErrors中包含ActionMessage对象,就表示发生了验证错误,Struts框架会把ActionErrors对象保存到request范围内,然后把请求转发到恰当的视图组件,视图组件通过 errors>标签把request范围内的ActionErrors对象中包含的错误消息显示出来,提示用户修改错误。 在Struts早期的版本中,使用ActionError类来表示错误消息,ActionError类是ActionMessage的子类。 Struts1.2将废弃ActionError,统一采用ActionMessage类来表示正常或错误消息。 第二种类型的验证,即业务逻辑验证,由Action来负责处理,参见本章2.4.3节。 2.4创建控制器组件 控制器组件包括ActionServlet类和Action类。 ActionServlet类是Struts框架自带的,它是整个Struts框架的控制枢纽,通常不需要扩展。 Struts框架提供了可供扩展的Action类,它用来处理特定的HTTP请求,例程2-4为HelloAction类的源程序。 例程2-4HelloAction.java packagehello; importjavax.servlet.RequestDispatcher; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpSession; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts.action.Action; importorg.apache.struts.action.ActionMessage; importorg.apache.struts.action.ActionMessages; importorg.apache.struts.action.ActionForm; importorg.apache.struts.action.ActionForward; importorg.apache.struts.action.ActionMapping; importorg.apache.struts.util.MessageResources; publicfinalclassHelloActionextendsAction{ /** *ProcessthespecifiedHTTPrequest,andcreatethecorrespondingHTTP *response(orforwardtoanotherwebcomponentthatwillcreateit). *Returnan *controlshouldbeforwarded,or *alreadybeencompleted. */ publicActionForwardexecute(ActionMappingmapping, ActionFormform, HttpServletRequestrequest, HttpServletResponseresponse) throwsException{ //These"messages"comefromtheApplicationResources.propertiesfile MessageResourcesmessages=getResources( copyright@ 2008-2023 冰点文库 网站版权所有 经营许可证编号:鄂ICP备19020893号-2ActionErrors
objectcontainingtheerrors.null
oranemptyActionErrors
object.ActionForward
instancedescribingwhereandhownull
iftheresponsehas