我有这个页面,我有两个表格。第二种形式由 ajax 加载(渲染)。问题是,当按下下一个表单的按钮时,页面只是重新加载自己,而不是实际调用“操作”中指定的方法。
XHTML
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./../WEB-INF/employeeTemplate.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns="http://www.w3.org/1999/xhtml">
<ui:define name="content">
finding user ...
<h:form id="mainForm">
<h:inputText id="userId" value="#{eFindUser.userId}" />
<h:commandButton value="Render!" action="#{eFindUser.findUser}" >
<f:ajax event="action" execute="userId" render="@all" />
</h:commandButton>
</h:form>
<h:panelGroup id="result">
<h:panelGroup layout="block" rendered="#{eFindUser.notFound}" style="color:red">
User not found
</h:panelGroup>
<h:form>
<h:panelGroup layout="block" rendered="#{eFindUser.responseRendered}" >
<h:inputHidden value="#{eFindUser.user}" />
<table>
<tr>
<td>Name</td> <td>#{eFindUser.user.name}</td>
</tr>
<tr>
<td>Balance:</td> <td>#{eFindUser.user.account.balance}</td>
</tr>
<tr>
<td><h:commandButton value="update balance" action="#{eFindUser.process()}" /></td>
</tr>
</table>
</h:panelGroup>
</h:form>
</h:panelGroup>
</ui:define>
</ui:composition>
支持豆
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package backingbeans;
import entities.User;
import java.io.Serializable;
import java.util.Random;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import stateless.EmployeeFacade;
/**
*
* @author Martin
*/
@ManagedBean(name="eFindUser")
@ViewScoped
public class EFindUserManagedBean implements Serializable{
private static final long serialVersionUID = -7106162864352727534L;
private boolean responseRendered = false;
private boolean notFound = false;
@NotNull
private Long userId;
private User user;
@Inject
private EUpdateBalanceManagedBean updateBalance;
@Inject
private EmployeeFacade employeeFacade;
/**
* Creates a new instance of EFindUserManagedBean
*/
public EFindUserManagedBean() {
}
public void findUser() {
if(new Random().nextDouble() < 0.3) {
notFound = true;
responseRendered = false;
} else {
notFound = false;
responseRendered = true;
user = employeeFacade.getUserById(3L);
}
}
public boolean isResponseRendered() {
return responseRendered;
}
public void setResponseRendered(boolean responseRendered) {
this.responseRendered = responseRendered;
}
public boolean isNotFound() {
return notFound;
}
public void setNotFound(boolean notFound) {
this.notFound = notFound;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
System.out.println("blah");
this.userId = userId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
System.out.println("setting user");
this.user = user;
}
public String process() {
updateBalance.setUser(user);
System.out.println("process");
return "/employee/updateBalance.xhtml?faces-redirect=true";
}
}
当我单击第一个带有“渲染!”标签的按钮时,它就像魅力一样。第二个表单已加载并准备好工作。当我单击下一个带有“更新余额”标签的按钮时,页面只是重新加载它不应该的,当然。它应该重定向到“/employee/updateBalance”。或者它至少应该调用 process 方法。
非常感谢
编辑:@BalusC 是对的,答案在上一个问题中