无法在jsf中实现Phase Listener

时间:2015-01-13 00:17:12

标签: jsf-2

我正在尝试在jsf中的一个bean中使用阶段监听器,但它不起作用。

类别:

package com.mycompany.creditcard1;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named(value = "userDetailsLogin1")
@ViewScoped
public class UserDetailsLogin1 implements Serializable, PhaseListener {

    private UserDetails userDetails;
    Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

    @PostConstruct
    public void init() {
        System.out.println("inti");
        userDetails = (UserDetails) flash.get("userDetails");
        if (userDetails == null) {
            userDetails = new UserDetails();
        }
    }

    public UserDetailsLogin1() {
    }

    public UserDetails getUserDetails() {
        return userDetails;
    }

    public String action() {

        flash.put("userDetails", userDetails);
        return "UserDetailsLogin2?faces-redirect=true";

    }

    @Override
    public void afterPhase(PhaseEvent pe) {
        System.out.println("after phase");
    }

    @Override
    public void beforePhase(PhaseEvent pe) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;

    }

}

faces-config文件:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

    <lifecycle>
        <phase-listener>com.mycompany.creditcard1.MyPhaseListener</phase-listener>
    </lifecycle>

错误:

无法创建'com.mycompany.creditcard1.MyPhaseListener'的新实例:javax.faces.FacesException:com.mycompany.creditcard1.MyPhaseListener

不明白为什么会这样? 任何帮助!!!

1 个答案:

答案 0 :(得分:1)

首先将<phase-listener>com.mycompany.creditcard1.MyPhaseListener</phase-listener>替换为<phase-listener>com.mycompany.creditcard1.UserDetailsLogin1</phase-listener> ......你的班级名称错误了!

另外,我不知道将Bean和PhaseListener混合到同一个类中是否是一个好习惯......