我想使用相同的支持 bean 和 requestscope 将数据从数据表传递到另一个页面,这是否可以在不使用数据模型的情况下实现?我正在使用 Servlet 3.0
这是我的数据表页面
<p:dataTable var="entity" value="#{collegeController.allCollege}">
<p:column headerText="Code">
#{entity.collegeCode}
</p:column>
<p:column headerText="Description">
#{entity.collegeDesc}
</p:column>
<p:column headerText="">
<p:commandLink value="Edit" action="#{collegeController.prepareEdit(entity)}"/>
</p:column>
</p:dataTable>
这是我的支持 bean
@ManagedBean
@RequestScoped
public class CollegeController implements Serializable {
private String redirect = ".jsf?faces-redirect=true";
private CollegeCatalog entity;
public CollegeController() {
}
public String prepareEdit(CollegeCatalog selectedEntity) {
Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
entity = (CollegeCatalog) s.load(CollegeCatalog.class, selectedEntity.getCollegeKey());
return "update" + redirect;
}
public List getAllCollege() {
Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = s.beginTransaction();
String query = ""
+ "FROM CollegeCatalog entity "
+ "WHERE entity.deleted = FALSE";
Query q = s.createQuery(query);
List l = q.list();
tx.commit();
return l;
}
/**
* @return the entity
*/
public CollegeCatalog getEntity() {
if (entity == null) {
entity = new CollegeCatalog();
}
return entity;
}
/**
* @param entity the entity to set
*/
public void setEntity(CollegeCatalog entity) {
this.entity = entity;
}
}
这是我的更新页面(这是我想显示所选数据的地方)
<h:form>
<p:outputLabel value="Code:" for="txtCode"/>
<p:inputText id="txtCode" value="#{collegeController.entity.collegeCode}"/>
<br/>
<p:outputLabel value="Description:" for="txtDesc"/>
<p:inputText id="txtDesc" value="#{collegeController.entity.collegeDesc}"/>
<br/><br/>
<p:commandButton value="Update" action="#{collegeController.update()}"/>
<p:commandButton value="Back" action="index.jsf?faces-redirect=true"/>
</h:form>
它总是返回null
。
最佳答案
您发送给collegeController.action
的参数方法将会丢失,因为您有 @RequestScoped
托管 bean 和整个托管 bean 将根据每个请求创建。此外,根据您的实际设计,该列表将在每个 collegeController.allCollege
上重新创建。方法调用。
解决您的问题:
将托管 Bean 范围更改为更广泛的范围。在这种情况下,
@ViewScoped
会没事的。有关托管 bean 范围的更多信息:Communication in JSF 2 - Managed Bean Scopes将所有业务逻辑移至 getter 之外。由于您正在使用 JSF 2 并希望在创建 bean 时加载列表,因此您可以使用
@PostConstruct
方法并在那里加载列表。在 JSF 中使用托管 bean 时,请记住不要将任何业务逻辑放入 getter/setter 中。更多信息:Why JSF calls getters multiple times
采纳这些建议并将其应用到您的代码中,托管 Bean 将如下所示:
@ManagedBean
@ViewScoped
public class CollegeController implements Serializable {
private String redirect = ".jsf?faces-redirect=true";
private CollegeCatalog entity;
private List<CollegeCatalog> collegeCatalogList;
public CollegeController() {
}
@PostConstruct
public void init() {
Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = s.beginTransaction();
String query = ""
+ "FROM CollegeCatalog entity "
+ "WHERE entity.deleted = FALSE";
Query q = s.createQuery(query);
collegeCatalogList = (List<CollegeCatalog>)q.list();
tx.commit();
entity = new CollegeCatalog();
}
public String prepareEdit(CollegeCatalog selectedEntity) {
Session s = NewHibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
entity = (CollegeCatalog)
s.load(CollegeCatalog.class, selectedEntity.getCollegeKey());
return "update" + redirect;
}
public List<CollegeCatalog> getAllCollege() {
return collegeCatalogList;
}
/**
* @return the entity
*/
public CollegeCatalog getEntity() {
return entity;
}
/**
* @param entity the entity to set
*/
public void setEntity(CollegeCatalog entity) {
this.entity = entity;
}
}
不过,请注意,这种方法只能帮助您发送 entity
<p:commandLink action="#{collegeController.prepareEdit(entity)}"/>
中的参数到托管 bean。如果您不重定向页面,而只是转发它,那就更好了。
如果您仍然喜欢使用重定向来执行此操作,则此方法将无法帮助您将数据发送到另一个页面。如果你使用 <h:button>
来代替会更好(或者更好的是 <h:link>
),它将重定向到您的其他页面,您可以在那里处理参数。这里有更好的解释:Communication in JSF 2 - Processing GET Request Parameters .
关于java - jsf将数据传递到其他页面不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16756087/