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

标签 java xpages

在我的 xpages 应用程序中,我想通过托管 bean 中的保存方法保存文档。但是我不知道如何在此保存方法中包含验证规则并将消息发送回 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();
        }
    }

最佳答案

尝试这个示例,它应该可以解决您的问题:

员工(您的数据对象):

package com.test.data;

public class Employee {

    private String email;

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

    public String getEmail() {
        return email;
    }

}

FormBean(用于在笔记后端保存数据的 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 生命周期!

关于java - 将验证添加到我的保存方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217207/

相关文章:

Java GWT在应用自定义celltable/datagrid css样式时如何编写文件路径

java - JiraRestClient 搜索未返回 JQL 查询的结果

Xpages 绑定(bind)到重复编辑控件

twitter-bootstrap-3 - 将 SSJS 操作添加到引导药丸中

xpages - 服务器端验证 - 计算 viewScope 所需的参数

java - 缓冲读取器不读取某些行

java - 尝试在android应用程序中为计算器做计算代码

java - 处理或否则抛出

xpages - 覆盖通过 xPages URL 打开的附件的内容处置类型

javascript - 我可以触发 xe :namepicker via csjs?