当前位置: 代码迷 >> Web前端 >> 重写FacesStateManager,为解决验证显示有关问题
  详细解决方案

重写FacesStateManager,为解决验证显示有关问题

热度:218   发布时间:2013-06-26 14:29:32.0
重写FacesStateManager,为解决验证显示问题

Richfaces验证后,验证不通过会有错误显示,当页面触发其他ajax请求时,如果想把验证错误信息清理掉,可以重写FacesStateManager来解决,关键代码:

package org.iata.isss.web.common.jsf.factory;

import java.io.IOException;
import java.util.Iterator;

import javax.faces.application.StateManager;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

/**
 * 
 * @author Xiao Jianbo
 *
 */
public class FacesStateManager extends StateManager {

	private final StateManager stateManager;

	public FacesStateManager(StateManager sm) {
		this.stateManager = sm;
	}

	@Override
	protected Object getComponentStateToSave(FacesContext ctx) {
		throw new UnsupportedOperationException();
	}

	@Override
	protected Object getTreeStructureToSave(FacesContext ctx) {
		throw new UnsupportedOperationException();
	}

	@Override
	protected void restoreComponentState(FacesContext ctx, UIViewRoot viewRoot,
			String str) {
		throw new UnsupportedOperationException();
	}

	@Override
	protected UIViewRoot restoreTreeStructure(FacesContext ctx, String str1,
			String str2) {
		throw new UnsupportedOperationException();
	}

	@Override
	public SerializedView saveSerializedView(FacesContext facesContext) {
		
		processEditableValueHolder(facesContext.getViewRoot());
		
		return stateManager.saveSerializedView(facesContext);
	}

	private void processEditableValueHolder(UIComponent component) {

		if (component instanceof EditableValueHolder) {
			EditableValueHolder editableValueHolder = (EditableValueHolder) component;
			editableValueHolder.setValid(true);
			boolean localset = editableValueHolder.isLocalValueSet();
			editableValueHolder.setValue(null);// super.setValue(value);setLocalValueSet(true);
			editableValueHolder.setLocalValueSet(localset);
		} else {
			Iterator<UIComponent> kids = component.getFacetsAndChildren();
			while (kids.hasNext()) {
				UIComponent kid = kids.next();
				processEditableValueHolder(kid);
			}
		}

	}

	@Override
	public void writeState(FacesContext ctx, SerializedView sv)
			throws IOException {
		stateManager.writeState(ctx, sv);
	}

	@Override
	public UIViewRoot restoreView(FacesContext ctx, String str1, String str2) {
		return stateManager.restoreView(ctx, str1, str2);
	}

	@Override
	public Object saveView(FacesContext facesContext) {
		return stateManager.saveView(facesContext);
	}

	@Override
	public void writeState(FacesContext ctx, Object sv) throws IOException {
		stateManager.writeState(ctx, sv);
	}

	@Override
	public boolean isSavingStateInClient(FacesContext ctx) {
		return stateManager.isSavingStateInClient(ctx);
	}

}

?

  相关解决方案