向我的保存方法添加验证 [英] adding validation to my save method

查看:24
本文介绍了向我的保存方法添加验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my xpages application I want to save documents via a save method in a managed bean. However I do not know how to include validation rules in this save method and send a message back to the XPage. I tried several examples that I have found googling but none of them worked. Perhaps someone can shine a light on how I could add a simple server-side validation without setting up a separate validation bean or so?

以下是我到目前为止所想到的:

Here is what I have come up so far:

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();
        }
    }

推荐答案

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

Try this sample, it should solve your problem:

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 生命周期!

Keep in mind: Save will only be entered if no validation errors. JSF Lifecycle!

这篇关于向我的保存方法添加验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆