1.1.1.1    Action和业务逻辑

思考题?

Action属于MVC中的Controller还是Model?为什么?

 

1.1.1.2    使用Struts内置的Action

Struts1.1框架的org.apache.struts.actions包中包含了5个内置的Action,用来执行一些通用的操作,你可以把它们用在你的项目中,以节省你的开发时间。接下来我们分别介绍这5个内置的Action

1.1.1.2.1      org.apache.struts.actions.ForwardAction类

很多情况下,你仅仅需要引导客户从一个JSP页面跳转到另外一个JSP页面,按照我们通常的做法,可以做一个链接让用户直接访问要跳转到的页面。但是MVC模式不推荐你这么做,因为,Controller的职责就是接收所有的客户请求,然后将客户请求提交给一个合适的模块进行处理,并将合适的UI推给用户,如果直接方式JSP页面,则跳过了Controller的控制,则无法享受Controller所提供的优点。为了解决这个问题,并且不用你去为了执行一个简单的重定向操作而创建一个Action类 ,Struts框架提供了ForwardAction类,这个Action只是简单地执行一个重定向操作,重定向的目的地通过parameter属性配置。要使用ForwardAction类,只需要在Struts配置文件中将Actiontype属性配置为org.apache.struts.actions.ForwardAction:

<action

  input="/index.jsp"

  name="loginForm"

  path="/viewsignin"

  parameter="/security/signin.jsp"

  scope="request"

  type="org.apache.struts.actions.ForwardAction"

  validate="false"/>

</action>

当你访问/viewsignin的时候,就会自动重定向到/security/signin.jsp

1.1.1.2.2      org.apache.struts.actions.IncludeAction类

暂略

1.1.1.2.3      org.apache.struts.actions.DispatchAction类

暂略

1.1.1.2.4      org.apache.struts.actions.LookupDispatchAction类

暂略

1.1.1.2.5      org.apache.struts.actions.SwitchAction类

暂略

1.1.2           Model

Struts没有定义具体的Model层的实现,Model层通常是和业务逻辑紧密相关的,还通常有持续化的要求,Struts目前没有考虑到这一层,但是,不管在开源世界还是商业领域,都有一些都别优秀的工具可以为Model层次的开发提供便利,例如优秀的O/R Mapping开源框架Hibernate。

1.1.3           View

通常,Web应用的UI由以下文件组成:

l         HTML

l         JSP

JSP中通常包含以下组件:

l         自定义标签

l         DTO(Data Transfer Object数据传输对象)

Struts中,还包含了以下两种常用的组件:

l         Struts ActionForms

l         资源绑定(java resource bundles),例如将标签的显示内容,错误提示的内容通过配置文件来配置,这样可以为实现国际化提供基础。

由此可见,Struts对于传统的Web UI所作的扩充就是Struts ActionForms和资源绑定,接下来对其进行进一步描述。

1.1.3.1    使用 Struts ActionForm

Struts框架中,ActionForm负责在用户和业务逻辑层之间来回地传递用户输入的数据。框架会自动收集用户输入并以form bean的方式将这些数据传递给Action,然后,form bean可以被传递到业务层。不过,为了减少表示层和业务层的耦合,不建议将ActionForm 直接传递给业务层,而建议代之为DTO。即在Action中利用form bean的数据创建合适的DTO,然后传递给业务层。下面的步骤描述了Struts框架在每一次请求中,是如何处理ActionForm的:

1、     检查是否已经配置ActionForm映射到Action

2、     如果某一个ActionForm被映射到Action利用配置文件中action元素的name属性查找相匹配的ActionForm配置信息;

3、     检查是否已经存在该ActionForm的实例(instance);

4、     如果存在该ActionForm的实例,并且符合当前请求的需要,则重用这个实例;

5、     否则,创建该ActionForm的实例,并且将其保存在合适的生存区域中(生存区域 (scope)的设置请查看action元素,scope表示该实例的生存期限,一般来说,request,session,application等几种)

6、     调用ActionForm实例的reset()方法;

7、     遍历请求参数,根据不同的参数名,调用ActionForm实例和参数名相对应的setter方法,设置参数值到ActionForm实例中;

8、  最后,如果validate属性设置为true,则调用ActionForm实例的validate()方法,该方法可以返回任何错误,主要为校验错误。

对于每一个需要传递form数据的HTML页面,必须使用一个ActionForm,同一个ActionForm可以被多个不同的页面使用,只要HTMLFORM域和ActionForm的属性相匹配即可。

下面是一个ActionForm的示例:

package com.oreilly.struts.banking.form;
 
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
/**
 * This ActionForm is used by the online banking appliation to validate
 * that the user has entered an accessNumber and a pinNumber. If one or
 * both of the fields are empty when validate( ) is called by the
 * ActionServlet, error messages are created.
 */
public class LoginForm extends ActionForm {
  // The user's private ID number
  private String pinNumber;
  // The user's access number
  private String accessNumber;
 
  public LoginForm(  ) {
    super(  );
    resetFields(  );
  }
  /**
   * Called by the framework to validate the user has entered values in the
   * accessNumber and pinNumber fields.
   */
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest req ){
    ActionErrors errors = new ActionErrors(  );
 
    // Get access to the message resources for this application.
    // There's no easy way to access the resources from an ActionForm.
    MessageResources resources =
      (MessageResources)req.getAttribute( Action.MESSAGES_KEY );
 
    // Check and see if the access number is missing.
    if(accessNumber == null || accessNumber.length(  ) == 0) {
      String accessNumberLabel = resources.getMessage( "label.accessnumber" );
      ActionError newError =
        new ActionError("global.error.login.requiredfield", accessNumberLabel );
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
    }
 
    // Check and see if the pin number is missing.
    if(pinNumber == null || pinNumber.length(  ) == 0) {
      String pinNumberLabel = resources.getMessage( "label.pinnumber" );
      ActionError newError =
        new ActionError("global.error.login.requiredfield", pinNumberLabel );
      errors.add(ActionErrors.GLOBAL_ERROR, newError);
    }
    // Return the ActionErrors, if any.
    return errors;
  }
 
  /**
   * Called by the framework to reset the fields back to their default values.
   */
  public void reset(ActionMapping mapping, HttpServletRequest request) {
    // Clear out the accessNumber and pinNumber fields.
    resetFields(  );
  }
  /**
   * Reset the fields back to their defaults.
   */
  protected void resetFields(  ) {
    this.accessNumber = "";
    this.pinNumber = "";
  }
 
  public void setAccessNumber(String nbr) {
    this.accessNumber = nbr;
  }
 
  public String getAccessNumber(  ) {
    return this.accessNumber;
  }
 
  public String getPinNumber(  ) {
    return this.pinNumber;
  }
  public void setPinNumber(String nbr) {
    this.pinNumber = nbr;
  }
}


posted on 2004-06-23 17:22  Johnny  阅读(521)  评论(0编辑  收藏  举报