0

我有这个由 BalusC 提供的抽象表单类,它设置验证消息

 public abstract class Form {

// Properties 
private Map messages = new LinkedHashMap();
private List formFeedback = new ArrayList();
private List errorMessages = new ArrayList();
private boolean hasError;

然后我有一堆扩展表单的类

public class ParentForm extends Form { 
public class SubForm1 extends Form {
public class SubForm2 extends Form {
public class SubForm3 extends Form {

在 parentForm 我调用每个子表单,但我不知道如何将验证消息从子表单传递给父表单。

public class ParentForm extends Form { 

  public Object update(HttpServletRequest request) {

    pseudo code
     //if subform1 is being edited
     SubForm1 subForm1 = new SubForm1 (authenticatedUser);
     subFormBean1 sfb1 = new subFormBean1 ();
     sfb1 = subForm1.update(request);

     //if subform2 is being edited
     SubForm2 subForm2 = new SubForm2 (authenticatedUser);
     subFormBean2 sfb2 = new subFormBean2 ();
     sfb2 = subForm2.update(request);

Form服务使用 Struts 风格的 Action 在 servlet 中执行

类更新动作

public class UpdateAction implements ControllerAction {

private Form form;
private Object obj;
private HashMap modelMap;

    public UpdateAction(HashMap modelMap, Form form, Object obj) {
    this.form = form;
    this.obj = obj;
    this.modelMap = modelMap;
}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    obj = form.update(request);
            request.setAttribute("obj", obj);
            request.setAttribute("form", form);

在我的jsps中我会放

<span class="formError"><c:out value="${form.messages.whatever}" />但这仅从父表单中获取消息,而不是从任何子表单中获取消息

我想知道像 `form[subForm].messages.whatever 这样的东西是否可行?就像是

 if (!sfb1.notSuccess) {
 parentForm.setSubFormMessages(sfb1.getMessages());
 }

我的项目有点长,但基本上我希望 HTML 页面上的部分(组件??)是 HTML 表单,仅在某些条件下才有效(它是我们用来在办公室手动路由的清单的工作流程应用程序) 想知道是否最好将其吸收并移植到 JSF 或修改 Form 类。

编辑 我正在尝试实现抽象子表单,但现在子表单上的类似内容仍会更新 Dao,即使说我的条目验证(processEntry)失败

  processEntry(request, ad);
  processEdit_date(request, ad);
  if (isSucces()) {
    dao.update(ad, authenticatedUser);
  }
4

1 回答 1

1

您基本上应该ParentForm在子表单的构建过程中通过。

SubForm1 subForm1 = new SubForm1(this, authenticatedUser);

public SubForm1(Form parent, User authenticatedUser) {
    this.parent = parent;
    this.authenticatedUser = authenticatedUser;
}

然后在SubForm1委托中向它发送消息

public addMessage(field, message) {
    parent.addMessage(field, message);
}

要保持SubForm1,SubForm2等 DRY,您可以按照包装模式创建另一个抽象类:

public abstract class SubForm extends Form {

    private Form parent;

    public SubForm(Form parent) {
        this.parent = parent;
    }

    public addMessage(field, message) {
        parent.addMessage(field, message);
    }

    // ...
}

然后设计SubForm1,SubForm2等如下:

public class SubForm1 extends SubForm {

    public SubForm1(Form parent, User authenticatedUser) {
        super(parent);
        this.authenticatedUser = authenticatedUser;
    }

    // ...
}
于 2012-10-05T11:08:41.220 回答