无法在jsf中实现Phase Listener
Can't implement Phase Listener in jsf
我正在尝试在 jsf 中的一个 bean 中使用阶段监听器,但它不起作用。
Class:
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;
}
}
面孔配置文件:
<?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
不明白为什么会显示这个?
任何帮助!!!
首先将 <phase-listener>com.mycompany.creditcard1.MyPhaseListener</phase-listener>
替换为 <phase-listener>com.mycompany.creditcard1.UserDetailsLogin1</phase-listener>
...您的 class 名称有误!
此外,我不知道将 Bean 和 PhaseListener 混合到同一个中是否是一个好习惯 class...
我正在尝试在 jsf 中的一个 bean 中使用阶段监听器,但它不起作用。
Class:
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;
}
}
面孔配置文件:
<?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
不明白为什么会显示这个? 任何帮助!!!
首先将 <phase-listener>com.mycompany.creditcard1.MyPhaseListener</phase-listener>
替换为 <phase-listener>com.mycompany.creditcard1.UserDetailsLogin1</phase-listener>
...您的 class 名称有误!
此外,我不知道将 Bean 和 PhaseListener 混合到同一个中是否是一个好习惯 class...