[英]jsf passing of data to other page not working
我想使用带有请求范围的相同后备bean将数据从数据表传递到另一个页面,这可以在不使用数据模型的情况下进行吗? 我正在使用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>
这是我的后援豆
@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范围的更多信息: JSF 2中的通信-托管Bean范围
将所有业务逻辑移到吸气剂之外。 由于您正在使用JSF 2,并且希望在创建bean时加载列表,因此可以使用@PostConstruct
方法并将列表加载到那里。 在JSF中使用托管bean时,请记住不要在getters / setter中放置任何业务逻辑 。 更多信息: 为什么JSF多次调用getters
采纳这些建议并将其应用于您的代码,托管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;
}
}
不过,请注意,这种方法仅有助于您将<p:commandLink action="#{collegeController.prepareEdit(entity)}"/>
的entity
参数发送到托管bean。 如果只转发页面而不是重定向页面,那会更好。
如果您仍然希望使用重定向来执行此操作,则此方法将无法帮助您将数据发送到另一个页面。 如果改用<h:button>
(甚至更好的是<h:link>
),它将重定向到另一个页面,您可以在那里处理参数,那会更好。 在这里可以对此进行更好的解释: JSF 2中的通信-处理GET请求参数 。
尝试使用f:setPropertyActionListener
和@ViewScoped
像这样
<p:column headerText="">
<p:commandLink value="Edit" action="#{collegeController.action" >
<f:setPropertyActionListener value="#{collegeController.entity}" target="#{collegeController.targetEntity}">
</p:commandLink>
</p:column>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.