使用Facelets在Java Server Faces中创建“编辑我的项目”页面 [英] Creating an "Edit my Item"-page in Java Server Faces with Facelets
问题描述
假设你有以下Facelet(使用Facelets 1.1.12):
Let's say that you have the following Facelet ( Using Facelets 1.1.12 ):
edit_item.xhtml which i access with edit_item.jsf
现在我有另一页将我发送给带有GET-paremeter ID的edit_item.jsf uri看起来像这样: http://mysite.com/edit_item.jsf?ID=200
Now i have another page sending me to edit_item.jsf with the GET-paremeter ID the uri looks like this: http://mysite.com/edit_item.jsf?ID=200
怎么做您访问Bean并获取信息,并使用JSF和Facelets在请求页面上显示它?有没有办法在页面加载时运行bean?
How do you access a Bean and fetch the Information, and display this on the requesting page with JSF and Facelets? Is there a way to run a bean when a page loads?
推荐答案
你可以使用 faces- config.xml
从 param
地图中注入ID的配置。
You can use the faces-config.xml
configuration to inject the ID from the param
map.
为此simple bean:
For this simple bean:
public class BeanWithId implements Serializable {
private String id;
private String info;
private void populateInfo() {
info = "Some info from data source for id=" + id;
}
public String getId() { return id; }
public void setId(String id) {
this.id = id;
populateInfo();
}
public String getInfo() { return info; }
public void setInfo(String info) { this.info = info; }
public String save() {
System.out.println("Saving changes to persistence store");
return null; // no navigation
}
}
您可以使用此注入ID定义:
You could inject the ID using this definition:
<managed-bean>
<managed-bean-name>beanWithId</managed-bean-name>
<managed-bean-class>datasource.BeanWithId</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>id</property-name>
<property-class>java.lang.String</property-class>
<value>#{param.ID}</value>
</managed-property>
</managed-bean>
Facelets表格:
Facelets form:
<h:form>
<p>ID: <h:outputText value="#{beanWithId.id}" /></p>
<p>Info: <h:inputText value="#{beanWithId.info}" /></p>
<p><h:commandLink action="#{beanWithId.save}" value="Save">
<f:param name="ID" value="#{param.ID}" />
</h:commandLink></p>
</h:form>
这不是唯一的方法(您可以使用<直接查看ID) code> FacesContext 例如)。
This isn't the only way to do it (you could look the ID up directly using the FacesContext
for example).
这篇关于使用Facelets在Java Server Faces中创建“编辑我的项目”页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!