将验证添加到我的保存方法

时间:2017-04-04 20:35:15

标签: java xpages

在我的xpages应用程序中,我想通过托管bean中的save方法保存文档。但是,我不知道如何在此save方法中包含验证规则并将消息发送回XPage。我尝试了几个我发现谷歌搜索的例子,但没有一个工作。也许有人可以说明如何在不设置单独的验证bean的情况下添加简单的服务器端验证?

这是我到目前为止所得到的:

public void save(Employee employee) throws ValidatorException {

        try {

            Document doc;           openDBPlusView();

            if (employee.getUnid() != null) {
                doc = view.getDocumentByKey(employee.getUnid(), true);
            } else {
                doc = null;
            }
            if (doc == null) {
                doc = db.createDocument();
            }
            if (employee.getEmail().equals("")) {
                FacesMessage message = new FacesMessage();
                message.setDetail("Email missing");
                throw new ValidatorException(message);
            } else {
                doc.replaceItemValue("email", employee.getEmail());
            }
            doc.replaceItemValue("Form", "employee");
            doc.save(true, false);          
            recycleNotesObjects();
        } catch (NotesException e) {

            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:2)

试试这个样本,它应该可以解决你的问题:

员工(您的数据对象):

package com.test.data;

public class Employee {

    private String email;

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

}

FormBean(用于在Notes后端保存数据的bean):

package com.test.bean;

import java.io.Serializable;

import com.test.data.Employee;

public class FormBean implements Serializable {

    private static final long serialVersionUID = -1042911106119057617L;

    public void save(Employee employee) {

        System.out.println("Keep in  mind: Save will only be entered if no validation errors! JSF Lifecycle!");
        System.out.println(employee.getEmail());

    }

}

FormValidators(用于验证的bean,在您的示例中为电子邮件地址):

package com.test.validation;

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public class FormValidators implements Serializable {

   private static final long serialVersionUID = 7157377379640149444L;

   public void validateEmail(FacesContext facesContext, UIComponent component, Object value) {
      // Check if email is valid or not
      if (value.toString().equals("") || value.toString().indexOf('@') == -1) {
         // Create a message -> email is invalid
         FacesMessage message = new FacesMessage("Email is invalid!");
         // Throw an exception so that it prevents document from being saved
         throw new ValidatorException(message);
      }
   }

}

面-配置:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <managed-bean>
    <managed-bean-name>formBean</managed-bean-name>
    <managed-bean-class>com.test.bean.FormBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
  </managed-bean>
  <managed-bean>
    <managed-bean-name>formValidators</managed-bean-name>
    <managed-bean-class>com.test.validation.FormValidators</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
  </managed-bean>
  <!--AUTOGEN-START-BUILDER: Automatically generated by IBM Domino Designer. Do not modify.-->
  <!--AUTOGEN-END-BUILDER: End of automatically generated section-->
</faces-config>

XPage(用于测试您的要求/将所有要求放在一起):

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">


    <xp:br></xp:br>
    <xp:br></xp:br>

    <xp:messages id="messages1"></xp:messages>

    <xp:br></xp:br>
    <xp:br></xp:br>

    <xp:inputText id="txtEmail" validator="#{formValidators.validateEmail}" value="#{requestScope.email}"
        required="true">
    </xp:inputText>

    <xp:br></xp:br>
    <xp:br></xp:br>

    <xp:button id="btnSave" value="Save">
        <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:var employee:com.test.data.Employee = new com.test.data.Employee();
employee.setEmail(requestScope.email);
formBean.save(employee);}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>

</xp:view>

请注意:只有在没有验证错误的情况下才会输入保存。 JSF生命周期!

答案 1 :(得分:0)

我过去所做的是让方法返回一个布尔值回到我的保存按钮,如果保存没有完成则返回false。然后在保存按钮SSJS代码中,我调用bean来获取要在displayerror(或errors)控件中显示的消息。我不确定validatorException是否在自定义验证器之外工作。

另一种选择是验证输入文本,除非各种控件通过验证,否则不进行保存。