欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSF / RichFace4 验证 validator,使用 groups属性

程序员文章站 2022-07-16 20:48:19
...

 

JSF验证的基础用法,官方文档有一些代码说明,可以自己搜一下,或者看下面的链接

http://www.mastertheboss.com/web-interfaces/293-jsf-validation-tutorial.html?showall=1

 

本篇略过基础的annotation标签和JSF自定义验证的内容,主要简单说一下用jsf验证时遇到的问题,网上关于JSF比较细节的资料或者例子比较少,查了半天才发现解决方法。

 

应用场景:

自定义了一个Manage Bean,即action类,对数据进行增删改查

添加数据的JSF页面:有个空表单需要在提交时验证数据

编辑数据的JSF页面:有个编辑数据的表单,提交时需要验证

 

问题:两个表单要验证的字段不一样,有些数据不需要验证,有些数据需要多个字段交叉验证

解决: 主要用Groups属性,将要验证的字段分类

 

抽取的部分代码示例:

 

ManageBean

 

import java.io.Serializable;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.example.ejb.service.UserService;

import org.example.web.validator.UserAddValidator; //自己建一个空的interface,用于定义group
import org.example.web.validator.UserEditValidator; //自己建一个空的interface 

@Named("userManageBean")
@RequestScoped
public class UserManageBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private int 	id;
	@Size(min=2, max=12, message="长度必须大于2,小于12")
	private String 	userName;
	@Size(min=6, max=12, message="长度必须大于6,小于12")
	private String 	password="";
	@Size(min=6, max=12, message="长度必须大于6,小于12")
	private String 	repassword="";
	
	@Pattern(regexp="^(\\d{18,18}|\\d{15,15}|(\\d{17,17}[x|X]))$", message="身份证格式错误")
	private String 	personId;


	//用于后台验证的service	
	@EJB
	private UserService userService;


	public UserManageBean() {
	}
	
	@AssertTrue(message = "密码不一致!", groups=UserAddValidator.class)
	public boolean isPasswordsEquals() {
		return password.equals(repassword);
	}

	//添加用户时,身份证必须未存在,忽略具体的check代码,下同
	@AssertTrue(message = "该用户身份证已存在!", groups=UserAddValidator.class)
	public boolean isPersonIdExsists() { 
		return userService.checkPersonId(personId);
	}
	
	//添加用户时,账号必须不存在
	@AssertTrue(message = "该用户账号已存在!", groups=UserAddValidator.class)
	public boolean isUserNotExsists() {
		return userService.checkUserName(userName);
	}

	//编辑用户时,不能把账号改成别人已有的身份证
	@AssertTrue(message = "该用户身份证已存在!", groups=UserEditValidator.class)
	public boolean isPersonIdConflig() {
		return userService.checkPersonId(id, personId);
	}
	
	public String update() {
                XXXXXXXXX
		return SUCCESS;
	}
	
	public String add() {
                XXXXXXXXXX
		return SUCCESS;
	}
。。。。。。一堆getter setter。。。。
}

 

add页面的表单代码

 

<h:form>
	<rich:graphValidator id="userValidator" value="#{userManageBean}"
		groups="org.example.web.validator.UserAddValidator">
		<rich:panel header="用户添加" width="100%">

			<rich:message for="userValidator" />

			<h:panelGrid columns="3">
				<h:panelGrid columns="2">
					<h:outputText value="*" style="color:red" />
					<h:outputText value="帐户:" />
				</h:panelGrid>
				<h:inputText id="userName" value="#{userManageBean.userName }">
					<rich:validator />
				</h:inputText>
				<rich:message for="userName" />
			</h:panelGrid>

			<h:panelGrid columns="3">
				<h:panelGrid columns="2">
					<h:outputText value="*" style="color:red" />
					<h:outputText value="密码:" />
				</h:panelGrid>
				<h:inputSecret id="password" value="#{userManageBean.password }">
					<rich:validator />
				</h:inputSecret>
				<rich:message for="password" />
			</h:panelGrid>
			</h:panelGrid>

			<h:panelGrid columns="3">
				<h:panelGrid columns="2">
					<h:outputText value="*" style="color:red" />
					<h:outputText value="身份证:" />
				</h:panelGrid>
				<h:inputText id="personId" value="#{userManageBean.personId }">
					<rich:validator />
				</h:inputText>
				<rich:message for="personId" />
			</h:panelGrid>

			<h:panelGrid columns="4">
				<h:panelGrid columns="2">
					<h:outputText value="*" style="color:red" />
					<h:outputText value="重复密码:" />
				</h:panelGrid>
				<h:inputSecret id="repassword" value="#{userManageBean.repassword }">
					<rich:validator />
				</h:inputSecret>
				<rich:message for="repassword" />
			</h:panelGrid>

			<h:commandButton value="提   交" action="#{userManageBean.add}"
				style="font-size:14px; font-weight:bold;" />
		</rich:panel>
	</rich:graphValidator>
</h:form>

 edit页面代码

 

<h:form>
	<rich:graphValidator id="userValidator" value="#{userManageBean}"
		groups="org.example.web.validator.UserEditValidator">

		<h:inputHidden value="#{userManageBean.id }" />

		<rich:panel header="用户编辑" width="100%">

			<rich:message for="userValidator" />

			<h:panelGrid columns="3">
				<h:panelGrid columns="2">
					<h:outputText value="*" style="color:red" />
					<h:outputText value="帐户:" />
				</h:panelGrid>
				<h:inputText id="userName" value="#{userManageBean.userName }">
					<rich:validator />
				</h:inputText>
				<rich:message for="userName" />
			</h:panelGrid>
			<h:panelGrid columns="3">
				<h:panelGrid columns="2">
					<h:outputText value="*" style="color:red" />
					<h:outputText value="身份证:" />
				</h:panelGrid>
				<h:inputText id="personId" value="#{userManageBean.personId }">
					<rich:validator />
				</h:inputText>
				<rich:message for="personId" />
			</h:panelGrid>

			</h:panelGrid>
			<h:commandButton value="保  存 " action="#{userManageBean.update}"
				style="font-size:14px; font-weight:bold;" />
		</rich:panel>
	</rich:graphValidator>
</h:form>

 UserAddValidator  UserEditValidator就是一个空的interface

 

public interface UserAddValidator {
}
public interface UserEditValidator {
}

 

上面的代码执行时,在add页面,不会执行被groups标记为UserEditValidator的验证;同样edit页面提交时也不会执行groups被标记为UserAddValidator的验证

 

转载标明源 http://asyty.iteye.com/blog/1420326