我有一个由两个JSP组成的JSF应用程序: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对象的列表。
答案 0 :(得分:2)
这不是解决方案 - 这是一个可以帮助您诊断问题的示例。当然,你可能已经这样做了 - 我看到你已经实现了一个阶段监听器。
豆:
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)