原文都是英文,是自己翻译的,如有不对的地方欢迎指正。
Q:What is ActionServlet?
什么是控制器(中央控制器)?
A:The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the Jakarta Struts Framework this class plays the role of controller. All the requests to server goes through the controller.Controller is responsible for handling all the requests.
类class org.apache.struts.action.ActionServlet被叫做控制器。在Jakarta Struts Framework中,这个类扮演者控制者的角色。所有给服务器的请求都通过这个控制者。它负责处理所有的请求。
Q:How you will make available any Message Resources Definitions file to the Struts Framework Environment?
你如何使得任何对Struts Framework环境的信息资源定义文件可用?
A:Message Resources Definitions file are simple proerties files and these files contains the messages that can be used in the struts project Message Resources Definitions files can be added to the struts-config.xml file through<message-resources>tag . Example:
<message-resources parameter="MessageResources" />
信息资源定义文件是简单的属性文件,并且这些文件包含的信息可以被用在struts项目中。这些文件可以通过struts-config.xml中的<message-resources>标签添加。
Q:What is Action Class?
什么是Action(操作)类?
A:The Action is part of the controller. The purpose of Action Class is to translate the HttpServletRequest to the bussiness logic. To use the Action, we need to Subclass and overwritethe execute() method. The ActionServlet (commad) passes the parameterized class to Action Form using the execute() method. There should be no database interactions in the action. The action should receive the request, call business objects (which then handle database, or interface with J2EE.etc) and then determine where to go next. Even better, the business objects could be handed to the action at runtime(IoC style) thus removing any dependencies on the model. The return type of the execute method is AtionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object.
Action(操作)是控制器的一部分。Action类的目的是把HttpServletRequest(请求对象)转换成业务逻辑。要用Action,我们需要继承和重写execute()方法。ActionServlet通过execute()方法把参数化类传递给Action Form(表单)。在这个Action中应该没有任何数据库的互动。这个action应该接受请求,调用业务对象(在此控制数据,或者J2EE界面,等等),并决定接下来做什么。甚至,业务对象可以在运行时被action控制从而消除对model的依赖(IoC)。(对spring不熟,此处翻译可能有误)。execute方法的返回类型是ActionForward,作为每个ActionForward对象的返回值,Struts Framework用它来告诉请求指向的文件.
Q:Write code of any Action Class?
写出任意Action类的代码。
A:Here is the code of Action Class that returns the ActionForward object.
TestAction.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class TestAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
return mapping.findForward("testAction");
}
}
Q:What is ActionForm?
什么是ActionForm?
A:An ActionForm is a JavaBean that extends org.apache,struts.action.ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.
一个ActionForm就是一个JavaBean,它继承org.apache.struts.action.ActionForm。ActionForm在网络应用中保持session状态,并且ActionForm对象自动把客户端的表单数据填送给服务器端。
Q:What is Struts Validator Framework?
什么是Struts验证框架?
A:Struts Framework provides the functionality to validate the form data.It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser. Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class.
Struts验证框架提供验证表单数据的功能。它可以验证用户浏览器和服务器上的信息。Struts框架使用javascript来验证浏览器端数据。服务器端的表单验证由Form Bean的子类DynaValidatorForm类完成。
验证框架由David Winterfeldt开发,作为第三方加入到Struts.现在验证框架是Jakarta Commons项目的一部分,并且在不在sturs中都可以使用。验证框架与struts紧密结合,可以不做任何其他设置直接使用。
Q:Give the Details of XML files used in Validator Framework?
给出在验证框架中使用XML文件的细节。
A:The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml .The validator-rule.xml defines the standard validation routines, these are reusable and used in validation.xml to define the form specific validations. The validation.xml defines the validations aplied to a form bean.
验证框架由两个xml文件组成validator-rules.xml和validation.xml。validator-rules.xml定义验证的基本规则,他们是可在validation.xml定义表单具体验证时重复使用的。validation.xml为表单项定义验证项。
Q:How you willl display validation fail errors on jsp page?
在jsp页面中如何显示验证错误信息。
A:Follwing tag displays all the errors:
<html:errors/>
Q:How you will enable front-end validation based on the xml in validation.xml?
如何使得前端验证基于validation.xml的xml定义。
A:The <html:javascript>tag to allow front-end validation based on the xml in validation.xml. For example the code <html:javascript formName="logonForm" dynamicJavascript="true" staticJavascript="true" />generates the client side java script for the form "logonForm" as defined in the validation.xml file. The <html:javascript>when added in the jsp file generates the client site validation script.
<html:javascript>标签使得前端验证基于validation.xml的xml定义。举例来说,代码<html:javascript
formName="logonForm" dynamicJavascript="true" staticJavascript="true" />使得表单logonForm通过使用客户端javascript使用validation.xml文件中定义的验证。当<html:javascript>加入到jsp页面中,就构建了客户端验证script。