这个项目是用 Primefaces 3.5、JSF 2.0、Oracle 数据库制作的。
首先,用户需要登录。登录后重定向到“议程”。此页面包含一个 selectOneMenu,需要根据已登录的用户填充数据。
登录.xhtml
<p:inputText style="text-transform: uppercase" value="#{loginBean.username}"
id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password: " />
<h:inputSecret value="#{loginBean.password}"
id="password" required="true" label="password" />
我的 LoginBean.java
sets and get my userId which i need to identify my user
also handles my loginrequest.
议程.xhtml
<p:selectOneMenu value="#{orResourcesBean.orResource}" converter="resourcesConverter" var="orResource"
style="width:250px" filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="Select Resource" itemValue="" />
<f:selectItems value="#{orResourcesBean.orResources}" var="orResource" itemLabel="#{orResource.RLname} #{orResource.RFname}"
itemValue="#{orResource}" />
<p:column>
#{orResource.RLname}
</p:column>
<p:column>
#{orResource.RFname}
</p:column>
</p:selectOneMenu>
或Resourcesbean.java
@ManagedBean(name="orResourcesBean")
@SessionScoped
public class OrResourcesBean implements Serializable {
private OrResources orResource;
private List<OrResources> orResources;
public void setOrResource(OrResources orResource)
{
this.orResource = orResource;
}
public OrResources getOrResource() {
return orResource;
}
public List<OrResources> getOrResources() {
orResources = ResourcesConverter.resources;
return orResources;
}
}
ResourcesConverter.java更新的工作功能
public static List<OrResources> resources;
static OrResourcesDao orResourceDao = new OrResourcesDaoImpl();
public Integer uKey;
{
LoginBean loginBean = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginBean");
if (loginBean != null) {
uKey = loginBean.getUKey();
if (uKey == null ) {
resources = null;
} else {
resources = orResourceDao.getOrResourcesById(uKey);
}
} else {
resources = null;
}
}
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (submittedValue.trim().equals("")) {
return null;
} else {
try {
String number = submittedValue;
for (OrResources orR : resources) {
if (orR.getRCode().equals(number)) {
return orR;
}
}
} catch(NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid resource"));
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((OrResources) value).getRCode());
}
}
所以你可以在我的 Agenda.xhtml 中看到,selectOneMenu 充满了所有资源,现在我必须只用已登录的用户可用的资源来填充它。我的问题是我无法获得来自任何地方的参数,因此我可以使我的 init 方法如下:
它不会让我将非静态变量放入静态(初始化)方法中。
但我该如何解决这个问题?