我正在创建一个Web应用程序,您必须从DB中读取对象/实体列表并在JSF中填充它<h:selectOneMenu>.我无法对此进行编码.有人可以告诉我该怎么做吗?
我知道如何List<User>从数据库中获取数据.我需要知道的是,如何填充此列表<h:selectOneMenu>.
<h:selectOneMenu value="#{bean.name}">
...?
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud) 我有理解如何有效地使用JSF 2中的选择和POJO /实体.例如,我试图Warehouse通过下面的下拉列表选择一个实体:
<h:selectOneMenu value="#{bean.selectedWarehouse}">
<f:selectItem itemLabel="Choose one .." itemValue="#{null}" />
<f:selectItems value="#{bean.availableWarehouses}" />
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
以下托管bean:
@Named
@ViewScoped
public class Bean {
private Warehouse selectedWarehouse;
private List<SelectItem> availableWarehouses;
// ...
@PostConstruct
public void init() {
// ...
availableWarehouses = new ArrayList<>();
for (Warehouse warehouse : warehouseService.listAll()) {
availableWarehouses.add(new SelectItem(warehouse, warehouse.getName()));
}
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
请注意,我使用整个Warehouse实体作为值SelectItem.
当我提交表单时,会失败并显示以下消息:
'null Converter'的转换错误设置值'com.example.Warehouse@cafebabe'.
我希望Warehouse当我将它包装在一个中时,JSF可以将正确的对象设置为我的托管bean SelectItem.将我的实体包装在里面SelectItem意味着跳过Converter为我的实体创建一个.
Converter每当我想在我的实体中使用实体时,我真的必须使用<h:selectOneMenu>吗?JSF应该可以从可用项列表中提取所选项.如果我真的必须使用转换器,那么实际的方法是什么?到目前为止,我想到了这个:
Converter为实体创建实现. …我怎么可以注入的依赖一样@EJB,@PersistenceContext,@Inject,@AutoWired,等的@FacesValidator?在我的具体情况下,我需要通过以下方式注入Spring托管bean @AutoWired:
@FacesValidator("emailExistValidator")
public class EmailExistValidator implements Validator {
@Autowired
private UserDao userDao;
// ...
}
Run Code Online (Sandbox Code Playgroud)
然而,它没有被注射,它仍然存在null,导致java.lang.NullPointerException.看来@EJB,@PersistenceContext并且@Inject也不起作用.
如何在验证器中注入服务依赖项以便我可以访问数据库?
从一些搜索来看,这似乎已经存在了一段时间的问题.我写了一个看起来如下的FacesConverter.对象Category是JPA实体,CategoryControl是获取它的DAO.
@FacesConverter(value = "categoryConverter")
public class CategoryConverter implements Converter {
@Inject private CategoryControl cc;
public CategoryConverter() { }
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (cc != null) return cc.getByName(value);
System.out.println("CategoryConverter().getAsObject(): no injection!");
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Category)) return null;
return ((Category) value).getName();
}
}
Run Code Online (Sandbox Code Playgroud)
你可能已经猜到了,我从来没有注射过.我从这个页面得到了这个解决方法,看起来像这样:
Workaround for this problem: create this method in your localeController:
public Converter …Run Code Online (Sandbox Code Playgroud) 我已经开始学习JSF,但遗憾的是,大多数教程只提供登录或注册部分.
你能指点我一些更深入的例子吗?我感兴趣的一件事是一个展示产品清单的页面.我在页面回家,我按下页面产品,以便我可以看到添加的最新产品.每次访问该页面时,都会根据数据库中的最新条目创建产品列表.我怎么处理这个?
解决此问题的一种方法是创建一个会话范围的托管bean,在其中我将通过其他托管bean更新不同的实体.我在一些教程中发现了这种方法,但它看起来很困难而且很笨拙.
哪个是解决这类问题的最佳方法?在两页主从细节用户界面中,会话范围的正确用法是什么?
我写了一个转换器.我正在使用CDI和注入并行.在这种情况下,不会注入类.如何使注射成为可能?
@FacesConverter(forClass = MyClass.class)
public class MyConverter implements Converter{
@EJB
private ClassForEJB classForEJB;
@Inject
private ClassForInject classForInject;
// Converter Methods
}
Run Code Online (Sandbox Code Playgroud) 我有一个无状态的EJB加入我的数据库.我需要在JSF 2转换器中使用此bean来从String值参数中检索实体对象.我正在使用JEE6和Glassfish V3.
@EJB 注释不起作用并获得NPE,因为它位于faces上下文中,并且它无法访问EJB上下文.
我的问题是:是否仍然可以使用@Resource其他注释或JNDI查找来注入此bean ,还是需要解决方法?
解
像这样执行JNDI查找:
try {
ic = new InitialContext();
myejb= (MyEJB) ic
.lookup("java:global/xxxx/MyEJB");
} catch (NamingException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我需要将Spring bean注入JSF(Primefaces)转换器.我尝试使用EL解析器注入豆子.但是,豆子null在转换器内部.
我的JSF转换器:
public class DepartmentConverter implements Converter {
private DepartmentService departmentService;
//getter setter for this property
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
//codes
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
//Codes
}
}
Run Code Online (Sandbox Code Playgroud)
faces-config.xml:
<converter>
<converter-id>DepartmentConverter</converter-id>
<converter-class>com.studinfo.jsf.converter.DepartmentConverter</converter-class>
<property>
<property-name>departmentService</property-name>
<property-class>com.studinfo.services.DepartmentService</property-class>
<default-value>#{DepartmentService}</default-value>
</property>
</converter>
Run Code Online (Sandbox Code Playgroud)
EL解析器:
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
Run Code Online (Sandbox Code Playgroud)
当我调试我的代码时,departmentService属性是null.我可以以相同的方式访问托管JSF bean中的Spring bean.
我正在使用Spring和Hibernate开发JSF项目,其中包括许多Converter遵循相同模式的s:
getAsObject 接收对象id的字符串表示形式,将其转换为数字,并获取给定种类的实体和给定的id
getAsString receive和entity并返回转换为的对象的id String
代码基本上是以下内容(省略检查):
@ManagedBean(name="myConverter")
@SessionScoped
public class MyConverter implements Converter {
private MyService myService;
/* ... */
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
int id = Integer.parseInt(value);
return myService.getById(id);
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
return ((MyEntity)value).getId().toString();
}
}
Run Code Online (Sandbox Code Playgroud)
鉴于大量的ConverterS中的完全一样(除了类型MyService和MyEntity当然的),我想知道,如果它使用一个通用的转换器是值得的.通用本身的实现并不困难,但我不确定声明Beans的正确方法.
可能的解决方案如下:
1 - 编写通用实现,让我们调用它MyGenericConverter,不需要任何Bean注释
2 - 将特定转换器ad写为子类,MyGenericConverter<T>并根据需要对其进行注释:
@ManagedBean(name="myFooConverter")
@SessionScoped
public class MyFooConverter implements MyGenericConverter<Foo> { …Run Code Online (Sandbox Code Playgroud) 我有EJB无状态bean.
如何通过programmatic而不是@EJB注释将它注入JSF托管bean?
我无法在jsf转换器中使用@Autowired注入服务:
转换器:
@Component("advertiserConverter")
@FacesConverter("advertiserConverter")
public class AdvertiserConverter implements Converter , Serializable {
@Autowired
private IAdvertiserService advertiserService;
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
Long id = Long.parseLong(value);
return advertiserService.findAdvertiser(id);
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return value instanceof Advertiser ? ((Advertiser) value).getId().toString() : "";
}
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml中:
<context:annotation-config />
<context:component-scan base-package="com.test.example"/>
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
服务:
@Service
@Transactional
public class AdvertiserServiceImpl implements IAdvertiserService { …Run Code Online (Sandbox Code Playgroud) 这converter是从我的JSF调用的.我已在里面注册了faces-config.xml
public class ProjectConverter implements Converter{
@EJB
DocumentSBean sBean;
@ManagedProperty(value="#{logging}")
private Logging log;
public ProjectConverter(){
}
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
if(value.trim().equals("")){
return null;
}
return sBean.getProjectById(value);
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
if(value == null){
return null;
}
return String.valueOf(((Project) value).getId());
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了java.lang.NullPointerException,当我进入时getAsObject(),主要原因是因为我的会话Bean sBean为空.我不知道如何解决这个问题,我需要访问我的Session bean,以便我可以从我的数据库中查询