日期:2014-05-16  浏览次数:20451 次

JSF的ViewExpiredException异常处理 转

视图没有存储的异常

javax.faces.application.ViewExpiredException: viewId:/pages/gardeninfo/envRiskEditForm.faces - View /pages/gardeninfo/envRiskEditForm.faces could not be restored.

对于用户来说是不懂的,那只要在重写生命周期便可处理

第一种:

?1. 重写一个类,继承Lifecycle类,重写父类的execute方法,捕捉该异常,然后进行处理(如设置跳转到登录页面)。
2. 重写一个类,继承LifecycleFactoryImpl类,添加SessionLifecycleImpl的实例到工厂中,假设重写的子类为project.jsf.test.SessionLifecycleFactoryImpl。
3.在faces-config.xml中注册JSFLifecycleFactory类。
4.在web.xml中指定JSFLifecycleFactory工厂中创建的JSFLifecycle实例的键(key)。

public class JSFLifecycleFactory extends LifecycleFactoryImpl {
?private static Logger LOGGER = FacesLogger.LIFECYCLE.getLogger();

?public static final String JSF_LIFECYCLE = "JSF";

?public JSFLifecycleFactory() {
??super();
?
??lifecycleMap.put(JSF_LIFECYCLE, new JSFLifecycle());
??if (LOGGER.isLoggable(Level.FINE)) {
???LOGGER.fine("Created EJS JSF Lifecycle");
??}
?}
}

public class JSFLifecycle extends Lifecycle {

?private static Logger LOGGER = FacesLogger.LIFECYCLE.getLogger();

?private Phase response = new RenderResponsePhase();

?private Phase[] phases = {
???null, // ANY_PHASE placeholder, not a real Phase
???new RestoreViewPhase(), new ApplyRequestValuesPhase(),
???new ProcessValidationsPhase(), new UpdateModelValuesPhase(),
???new InvokeApplicationPhase(), response };

?private List<PhaseListener> listeners = new CopyOnWriteArrayList<PhaseListener>();

?public void execute(FacesContext context) throws FacesException {
??try {
???if (context == null) {
????throw new NullPointerException(MessageUtils
??????.getExceptionMessageString(
????????MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
????????"context"));
???}

???if (LOGGER.isLoggable(Level.FINE)) {
????LOGGER.fine("execute(" + context + ")");
???}

???for (int i = 1, len = phases.length - 1; i < len; i++) {
????if (context.getRenderResponse()
??????|| context.getResponseComplete()) {
?????break;
????}
????phases[i].doPhase(context, this, listeners.listIterator());
???}
??}
??catch (ViewExpiredException e) {
???JSFMessageUtils.removeMessage();
???JSFMessageUtils.showAndLogException("信息","操作错误", e
???????.getMessage(), null, JSFMessageUtils.WARN,
?????JSFMessageUtils.OK);
??}
??catch (Exception e) {
???JSFMessageUtils.removeMessage();
???JSFMessageUtils.showAndLogException("信息","操作错误", e
?????.getMessage(), null, JSFMessageUtils.WARN,
???JSFMessageUtils.OK);
??}
?}

?public void render(FacesContext context) throws FacesException {
??try {
???if (context == null) {
????throw new NullPointerException(MessageUtils
??????.getExceptionMessageString(
????????MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
????????"context"));
???}

???if (LOGGER.isLoggable(Level.FINE)) {
????LOGGER.fine("render(" + context + ")");
???}
???if (!context.getResponseComplete()) {
????response.doPhase(context, this, listeners.listIterator());
???}
??}
??catch (FacesException e) {
???JSFMessageUtils.removeMessage();
???JSFMessageUtils.showAndLogException("信息","操作错误", e
?????.getMessage(), null, JSFMessageUtils.WARN,
???JSFMessageUtils.OK);
??}
??catch (Exception e) {
???JSFMessageUtils.showAndLogException("信息","操作错误", e
?????.getMessage(), null, JSFMessageUtils.WARN,
???JSFMessageUtils.OK);
??}
?}

?public void addPhaseListener(PhaseListener listener) {
??if (listener == null) {
???throw new NullPointerException(MessageUtils
?????.getExceptionMessageString(
???????MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
???????"listener"));
??}

??if (listeners == null) {
???listeners = new CopyOnWriteArrayList<PhaseListener>();
??}

??if (listeners.contains(listener)) {
???if (LOGGER.isLoggable(Level.