我有一个JSF应用程序,它由两个jsp组成:login.jsp&main.jsp.
我有以下faces-config.xml:
<lifecycle>
<phase-listener>package.programs.scorecard.beans.EventBean</phase-listener>
</lifecycle>
<managed-bean>
<managed-bean-name>FormBean</managed-bean-name>
<managed-bean-class>package.programs.scorecard.beans.FormBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>DataBean</managed-bean-name>
<managed-bean-class>package.programs.scorecard.beans.DataBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>EventBean</managed-bean-name>
<managed-bean-class>package.programs.scorecard.beans.EventBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
以及以下web.xml:
<welcome-file-list>
<welcome-file>faces/login.jsp</welcome-file>
<welcome-file>faces/index.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
我在login.jsp上有几个组件,它们在单击commandLink时更新DataBean中的属性。成功完成后,用户将导航到/main.jsp.
我注意到,当我单击commandLink时,该方法在bean更新之前执行。这导致在方法执行时,我的所有bean属性都为null,因为我的表单中的值没有应用于bean。
根据我对JSF生命周期的理解,"更新模型值"应该总是在"调用应用程序"之前执行,除非指定了"immediate=true",而这不适用于我的commandLink。
今天早上,我已经试着调试了几个小时,我欢迎任何建议或见解。
更新:我已经彻底调查了这个问题,并得出结论,我使用的下拉组件中有一个错误,应该在下一个版本中修复。我的解决方案是使用常规的h:selectOneMenu来代替第三方组件,它的工作原理就像一个魅力:
<h:selectOneMenu id="ddlManager" value="#{DataBean.managerId}">
<f:selectItems value="#{DataBean.managerList}" />
</h:selectOneMenu>
其中managerList是SelectItem对象的列表。
这不是一个解决方案,而是一个可能帮助您诊断问题的示例。当然,你可能已经在做了——我看到你已经实现了一个阶段监听器。
Bean:
public class LoginBean implements Serializable {
private String user;
private String password;
public String getPassword() {
return password;
}
public String getUser() {
return user;
}
public void setPassword(String password) {
System.out.println("Update Password: " + password);
this.password = password;
}
public void setUser(String user) {
System.out.println("Update User: " + user);
this.user = user;
}
public String login() {
if ("user".equals(user) && "password".equals(password)) {
// ok
System.out.println("Login OK");
return "loggedIn";
} else {
System.out.println("Login Failed");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(
"Invalid user name or password");
context.addMessage(null, message);
return null;
}
}
}
视图:
<f:view>
<h:form>
login:
<h:inputText value="#{loginBean.user}" />
<h:inputSecret value="#{loginBean.password}" />
<h:commandButton value="login" action="#{loginBean.login}" />
<h:messages />
</h:form>
</f:view>
faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>login.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>login</display-name>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>loggedIn</from-outcome>
<to-view-id>/label.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<lifecycle>
<phase-listener>login.PhaseListenerImpl</phase-listener>
</lifecycle>
</faces-config>
相位侦听器:
public class PhaseListenerImpl implements PhaseListener {
public void afterPhase(PhaseEvent event) {
System.out.println("AFTER: " + event.getPhaseId());
}
public void beforePhase(PhaseEvent event) {
System.out.println("BEFORE: " + event.getPhaseId());
}
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}
匹配用户名和密码的预期日志输出为:
BEFORE: RESTORE_VIEW(1)
AFTER: RESTORE_VIEW(1)
BEFORE: APPLY_REQUEST_VALUES(2)
AFTER: APPLY_REQUEST_VALUES(2)
BEFORE: PROCESS_VALIDATIONS(3)
AFTER: PROCESS_VALIDATIONS(3)
BEFORE: UPDATE_MODEL_VALUES(4)
Update User: user
Update Password: password
AFTER: UPDATE_MODEL_VALUES(4)
BEFORE: INVOKE_APPLICATION(5)
Login OK
AFTER: INVOKE_APPLICATION(5)
BEFORE: RENDER_RESPONSE(6)
AFTER: RENDER_RESPONSE(6)