我有一个非常奇怪的问题。我无法在第二个按钮中使用功能
步骤:
1.写下姓名和确认名
2.点击"找到"按钮。结果:数据表中的项目
3.单击" TEMPPP"只刷新页面
我必须使用函数sendUserInformation()
Administrator.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
template="/WEB-INF/templates/template.xhtml">
<ui:define name="menuItems">
</ui:define>
<ui:define name="logedContent">
<h:form id="form1t">
<center><h:outputLabel value="#{msg.find_employee}"/></center>
<table class="tableGeneral">
<tr>
<td><h:outputLabel value="#{msg.forename}*"/></td>
<td>
<h:inputText id="name1" value="#{administrationController.forename}">
<f:validator validatorId="DefaultStringValidator"/>
</h:inputText>
</td>
<td>
<h:message styleClass="error" for="name1"/>
</td>
</tr>
<tr>
<td><h:outputLabel value="#{msg.surename}*"/></td>
<td>
<h:inputText id="name2" value="#{administrationController.surename}">
<f:validator validatorId="DefaultStringValidator"/>
</h:inputText>
</td>
<td>
<h:message styleClass="error" for="name2"/>
</td>
</tr>
<tr>
<td colspan="2">
<h:commandButton styleClass="buttonGeneral" value="#{msg.find}" >
<!-- <f:ajax event="click" execute="name1 name2" render=":form1t:form2:tab" listener="#{administrationController.find_employee()}"/> -->
<f:ajax event="click" execute="name1 name2" render="@all" listener="#{administrationController.find_employee()}"/>
</h:commandButton>
</td>
</tr>
</table>
</h:form>
<hr/>
<br/>
<h:form id="form2">
<center><h:outputLabel value="#{msg.found}"/></center>
<h:dataTable id="tab" value="#{administrationController.foundUsers}" styleClass="tableGeneral" var="o">
<h:column>
<f:facet name="header"><h:outputLabel value="#{msg.forename}"/></f:facet>
<h:outputText value="#{o.forename}"/>
</h:column>
<h:column>
<f:facet name="header"><h:outputLabel value="#{msg.surename}"/></f:facet>
<h:outputText value="#{o.surename}"/>
</h:column>
<h:column>
<f:facet name="header"><h:outputLabel value="#{msg.job}"/></f:facet>
<h:outputText value="#{o.job}"/>
</h:column>
<h:column>
<f:facet name="header"><h:outputLabel value="#{msg.user}"/></f:facet>
<h:outputText value="#{o.login}"/>
</h:column>
<h:column>
<f:facet name="header"><h:outputLabel value="#{msg.activated}"/></f:facet>
<h:outputText value="#{msg.yes}" rendered="#{o.activated}"/>
<h:outputText value="#{msg.no}" rendered="#{!o.activated}"/>
</h:column>
<h:column>
<!-- <h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation(o.id)}"/> -->
<!--
<h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation()}">
<f:param name="id" value="#{o.id}"/>
</h:commandButton>
-->
<h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation(o.id)}" styleClass="buttonGeneral" >
</h:commandButton>
<!--
<h:button value="TEMPPP #{o.id}" outcome="userInformation.xhtml">
<f:param name="id" value="#{o.id}"/>
</h:button>
-->
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
AdministrationController
package controller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import others.MenuBars;
import database.Queries;
import users.User;
public class AdministrationController implements Serializable {
private static final long serialVersionUID = -2151888463865423931L;
private Queries queries = (Queries) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("queries");
private MenuBars menubars = (MenuBars)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("menuBars");
private List<User> users = new ArrayList<User>();
private List<User> foundUsers = new ArrayList<User>();
private String forename = "";
private String surename = "";
public AdministrationController(){
System.out.println("AdministrationController()");
users = queries.getAllUsers();
}
/*
public void tempUser(ValueChangeEvent event){
System.out.println(event.getNewValue().getClass());
User u = (User) event.getNewValue();
//TODO
System.out.println("idz do informacji o uzytkowniku"+u.name());
}*/
public String getForename() {
return forename;
}
public void setForename(String forename) {
this.forename = forename;
}
public String getSurename() {
return surename;
}
public void setSurename(String surename) {
this.surename = surename;
}
public void find_employee(){
System.out.println("find_employee()");
foundUsers.clear();
for(User u: users){
if(u.getForename().equalsIgnoreCase(getForename()) && u.getSurename().equalsIgnoreCase(getSurename())){
foundUsers.add(u);
}
}
}
public List<User> getFoundUsers() {
return foundUsers;
}
public void setFoundUsers(List<User> foundUsers) {
this.foundUsers = foundUsers;
}
public String sendUserInformation(String id){
System.out.println("sendUserInformation; id = "+id);
menubars.addToMap("id", id);
return "userInformation.xhtml";
}
}
解决!
f:需要ajax(可能还有viewcope)
<h:commandButton action="#{administrationController.sendUserInformation(o.id)}" styleClass="buttonGeneral" value="TEMP #{o.id}">
<f:ajax/>
</h:commandButton>
答案 0 :(得分:0)
假设您的AdministrationController
bean是@RequestScoped
(因为您还没有指定它的范围),那么<h:dataTable>
中的数据将会重新加载请求在同一视图中。这意味着,当您选择
<h:commandButton value="TEMPPP #{o.id}" action="#{administrationController.sendUserInformation(o.id)}"/>
这将触发新的AdministrationController
,List
中的数据将会丢失,这会导致您的行为失效,因为o.id
不再可用。
要解决此问题,请至少使用@ViewScoped
等更广泛的范围。
@ManagedBean
@ViewScoped
public class AdministrationController implements Serializable {
//rest of your code
}