在向下钻取中显示值 SelectOneMenus(父子)
当我填充两个 selectOneMenu
的深入选择时,我遇到了恼人的问题。第一个菜单是Sector,它控制另一个Categorty。我根据部门加载类别的选择,所有这些都工作正常。
但是,当我编辑一个附加了类别(有一个)扇区的业务对象时。首次加载 edit.xhtml 页面时,类别不会显示。我知道 SessionScoped
支持 bean 中的值是正确的。如果我选择另一个扇区类别,然后返回到初始扇区,则类别会被正确设置,并且首先会被保留。
POJO 类(Sector-Category)具有 hashCode()
和 equals(Object object)
函数。
我有 CRUD 为 POJO 对象生成 JSF 转换器,但我不认为这会导致问题。我认为第二个 selectOneMenu
由于某种原因没有显示其应有的值。如果我将父级(扇区)翻转到其他位置并返回到初始状态,则会显示来自 manged bean 的正确值。
如何让 Category selectOneMenu
组件显示托管 bean 中的值?
问候 Chris
面临错误消息
FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=null[severity=(ERROR 2), summary=(No activity selected), detail=(No activity selected)]
Edit.xhtml
...
<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selectedSector}" title="#{bundle.CreateSectorLabel_sectorName}" required="false" requiredMessage="#{bundle.CreateSectorRequiredMessage_sectorName}"
valueChangeListener="#{activityController.changeSectorMenu}"
disabled="#{activityController.activityStatusOngoing or activityController.activityStatusComplete}">
<f:ajax event="change" execute="@this" render="categoryMenu"/>
<f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Category:" />
<h:selectOneMenu id="categoryMenu" value="#{activityController.selectedCategory}" title="#{bundle.CreateSectorLabel_sectorName}"
required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}"
disabled="#{activityController.activityStatusOngoing}" rendered="true">
<f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>
...
类别部分的控制器 bean
@ManagedBean(name = "categoryController")
@SessionScoped
public class CategoryController implements Serializable{
....
@FacesConverter(forClass = Category.class)
public static class CategoryControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
CategoryController controller = (CategoryController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "categoryController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Category) {
Category o = (Category) object;
return getStringKey(o.getIdCategory());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CategoryController.class.getName());
}
}
}
POJO 对象的
...
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idCategory")
private Integer idCategory;
...
I have irritating problem when I populate a drill-down selection of two selectOneMenu
. The first menu is Sector which controls the other one Categorty. I load the selection for the Category depending on Sector and all of this is working fine.
But when I edit a business object that has a Category (has one) Sector attached to it. The Category doesn't get display when first loading the edit.xhtml page. I know the value is correct in the SessionScoped
backing bean. If I select another Sector-Category and then go back to the initial Sector, the Category get properly set at it was persisted in the first place.
The POJO classes (Sector-Category) has the hashCode()
and equals(Object object)
functions.
I have CRUD generated JSF converters for the POJO object, but I don't think this is causing the problem. I think the second selectOneMenu
don't display its value as it should for some reason. If I flip the parent (Sector) to something else and the back to the initial state, the correct value from the manged bean get displayed.
Ho can I make the Category selectOneMenu
component display the value in the managed bean?
Greetings Chris
Faces Error Message
FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=null[severity=(ERROR 2), summary=(No activity selected), detail=(No activity selected)]
Edit.xhtml
...
<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selectedSector}" title="#{bundle.CreateSectorLabel_sectorName}" required="false" requiredMessage="#{bundle.CreateSectorRequiredMessage_sectorName}"
valueChangeListener="#{activityController.changeSectorMenu}"
disabled="#{activityController.activityStatusOngoing or activityController.activityStatusComplete}">
<f:ajax event="change" execute="@this" render="categoryMenu"/>
<f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Category:" />
<h:selectOneMenu id="categoryMenu" value="#{activityController.selectedCategory}" title="#{bundle.CreateSectorLabel_sectorName}"
required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}"
disabled="#{activityController.activityStatusOngoing}" rendered="true">
<f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>
...
Controller bean for Category
@ManagedBean(name = "categoryController")
@SessionScoped
public class CategoryController implements Serializable{
....
@FacesConverter(forClass = Category.class)
public static class CategoryControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
CategoryController controller = (CategoryController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "categoryController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Category) {
Category o = (Category) object;
return getStringKey(o.getIdCategory());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CategoryController.class.getName());
}
}
}
Part of POJO object
...
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idCategory")
private Integer idCategory;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选项(不像我最近进行的切换那么困难)是包含 Seam Faces 和 Persistence 模块。 Seam Faces 模块启用 ViewScope 而无需 @ManagedBean 注释。
但 SeamFaces 迫使我们(这是合理的)使用 CDI 注释(@Named 而不是 @ManagedBean 等)。
请参考这篇文章 。 (您可以使用@EJB,也可以注入任何用@Named注释的bean)
Option (not so difficult as I recently made a switch) is to include Seam Faces and Persistence modules. Seam Faces module enables ViewScope without @ManagedBean annotation.
But SeamFaces forces us (which is justified) to use CDI annotations (@Named instead of @ManagedBean etc).
Please refer this post. (You can use @EJB and also Inject any bean annotated with @Named)
这并不像我最初想象的那样是转换器。这是两个
selectOneMenu
的渲染。这是我对 xhml 文件所做的更改。感谢您的时间和努力,问候克里斯。编辑.xhml
It was not the Converters as I first thought. It was the rendering of the two
selectOneMenu
. This is the changes I did to the xhml file. Thank you for your time and effort, greetings Chris.Edit.xhml