我有一个ap:selectOneMenu的问题,无论我做什么我都无法让JSF调用JPA实体上的setter.JSF验证失败,显示以下消息:
form:location:验证错误:值无效
我有这个工作在几个相同类型的其他类(即,连接表类),但不能为我的生活让这一个工作.
如果有人可以针对此类问题提出一些故障排除/调试技巧,我们将不胜感激.
使用日志语句我已经验证了以下内容:
Conveter将返回正确的,非null数值.setLocation(Location location)永远不会调用setter .这是我能做的最简单的例子,它根本不起作用:
<h:body>
<h:form id="form">
<p:messages id="messages" autoUpdate="true" />
<p:selectOneMenu id="location" value="#{locationStockList.selected.location}" converter="locationConverter">
<p:ajax event="change" update=":form:lblLocation"/>
<f:selectItems value="#{locationStockList.locationSelection}"/>
</p:selectOneMenu>
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud)
转换器:
@FacesConverter(forClass=Location.class, value="locationConverter")
public class LocationConverter implements Converter, Serializable {
private static final Logger logger = Logger.getLogger(LocationConverter.class.getName());
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value.isEmpty())
return null;
try {
Long id = Long.parseLong(value);
Location location = ((LocationManagedBean) context.getApplication().getELResolver().getValue(context.getELContext(), null, "location")).find(id);
logger.log(Level.SEVERE, …Run Code Online (Sandbox Code Playgroud) 我正在创建一个Web应用程序,您必须从DB中读取对象/实体列表并在JSF中填充它<h:selectOneMenu>.我无法对此进行编码.有人可以告诉我该怎么做吗?
我知道如何List<User>从数据库中获取数据.我需要知道的是,如何填充此列表<h:selectOneMenu>.
<h:selectOneMenu value="#{bean.name}">
...?
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud) 我怎么可以注入的依赖一样@EJB,@PersistenceContext,@Inject,@AutoWired,等的@FacesConverter?在我的具体情况下,我需要通过@EJB以下方式注入EJB :
@FacesConverter
public class MyConverter implements Converter {
@EJB
protected MyService myService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// myService.doSomething
}
}
Run Code Online (Sandbox Code Playgroud)
然而,它没有被注射并且仍然存在null,导致NPE.看来,@PersistenceContext和@Inject也不起作用.
如何在转换器中注入服务依赖项以便我可以访问数据库?
我正在填充<p:selectOneMenu/>数据库,如下所示.
<p:selectOneMenu id="cmbCountry"
value="#{bean.country}"
required="true"
converter="#{countryConverter}">
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
<f:selectItems var="country"
value="#{bean.countries}"
itemLabel="#{country.countryName}"
itemValue="#{country}"/>
<p:ajax update="anotherMenu" listener=/>
</p:selectOneMenu>
<p:message for="cmbCountry"/>
Run Code Online (Sandbox Code Playgroud)
加载此页面时,默认选择的选项是,
<f:selectItem itemLabel="Select" itemValue="#{null}"/>
Run Code Online (Sandbox Code Playgroud)
转换器:
@ManagedBean
@ApplicationScoped
public final class CountryConverter implements Converter {
@EJB
private final Service service = null;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try {
//Returns the item label of <f:selectItem>
System.out.println("value = " + value);
if (!StringUtils.isNotBlank(value)) {
return null;
} // Makes no difference, if removed.
long parsedValue …Run Code Online (Sandbox Code Playgroud) 我想在一个中使用枚举值<h:selectManyCheckbox>.正确填充复选框,但是,在选择某些值并提交它们时,它们的运行时类型是String,而不是枚举.我的代码:
<h:selectManyCheckbox value="#{userController.roles}" layout="pageDirection">
<f:selectItems value="#{userController.rolesSelectMany}" />
</h:selectManyCheckbox>
Run Code Online (Sandbox Code Playgroud)
UserController类(SecurityRole是枚举类型):
public SelectItem[] getRolesSelectMany() {
SelectItem[] items = new SelectItem[SecurityRole.values().length];
int i = 0;
for (SecurityRole role : SecurityRole.values()) {
items[i++] = new SelectItem(role, role.toString());
}
return items;
}
public List<SecurityRole> getRoles() {
getCurrent().getRoles();
}
public void setRoles(List<SecurityRole> roles) {
getCurrent().setRoles(roles);
}
Run Code Online (Sandbox Code Playgroud)
当JSF调用setRoles方法时,它包含String类型的列表,而不是枚举类型.有任何想法吗?谢谢!
我是JSF的新手,我一直在尝试存储使用ah:selectOneMenu来获取产品类别的表单中的数据.h:selectOneMenu正在从DB填充,但是,当尝试在DB中存储产品时,我收到一个错误:转换错误设置值'52'表示'null Converter'.我已经在线查看了StackOverflow和教程中的类似问题,但我仍然收到错误.
这是xhtml:
<h:selectOneMenu id="category_fk" value="#{productController.product.category_fk}"
converter="#{categoryConverter}" title="Category_fk" >
<!-- DONE: update below reference to list of available items-->
<f:selectItems value="#{productController.categoryList}" var="prodCat"
itemValue="#{prodCat}" itemLabel="#{prodCat.name}"/>
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
这是产品控制器:
@Named
@RequestScoped
public class ProductController {
@EJB
private ProductEJB productEjb;
@EJB
private CategoryEJB categoryEjb;
private Product product = new Product();
private List<Product> productList = new ArrayList<Product>();
private Category category;
private List<Category> categoryList = new ArrayList<Category>();
public String doCreateProduct()
{
product = productEjb.createProduct(product);
productList = productEjb.findAllProducts();
return "listProduct";
}
@PostConstruct
public void init()
{
categoryList …Run Code Online (Sandbox Code Playgroud) 我正在从IceFaces变为PrimeFaces(我真的想改为RichFaces,但导致新版本中出现错误,我不会)并且我正在努力实现正确的primefaces autoComplete.根据他的手册,我只需要实现一个返回对象列表的方法,在这种情况下需要一个转换器.
我正在返回的列表是javax.faces.model.SelectItem的列表,我真的不明白为什么我需要为此创建一个转换器,但让我们继续.我创建了一个简单的转换器来测试,但是primefaces无法识别我的转换器并在浏览器中返回此错误:
/resources/components/popups/popupBuscaPessoa.xhtml @ 35,41 itemLabel ="#{pessoa.label}":类'java.lang.String'没有属性'label'.
这是我的转换课程(只是为了测试):
public class ConversorSelectItem implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value!=null && value.isEmpty())
return null;
SelectItem selectItem=new SelectItem();
selectItem.setLabel(value);
return selectItem;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
return ((SelectItem)object).getLabel();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用p:autocomplete的地方:
<p:autoComplete value="#{modeloPopupBuscaPessoa.itemSelecionado}"
completeMethod="#{controladorSugestaoPessoa.atualizarSugestoes}"
var="pessoa" itemLabel="#{pessoa.label}" itemValue="#{pessoa.value}"
converter="#{conversorSelectItem}"/>
Run Code Online (Sandbox Code Playgroud)
我做错什么了吗?是否有SelectItem的默认转换器?有没有更简单的方法来实现这个转换器?
我正在使用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) 我正在尝试实施两个 <h:selectOneMenu/>
第一个包含Type 1对象.当用户从该列表中选择一个对象时,<h:selectOneMenu/>将显示第二个对象.
这是我的代码
XHTML
<h:form>
<h:selectOneMenu value="#{handler.selectedType1}" valueChangeListener="#{handler.valueChanged}">
<f:selectItems value="#{handler.types1}" var="type1" itemValue="#{type1.name}" itemLabel="#{type1.name}" />
<a4j:ajax event="valueChange" render="type2List" execute="@this"/>
</h:selectOneMenu>
<h:selectOneMenu id="type2List" value="#{handler.selectedType2}" rendered="#{not empty handler.selectedType2}">
<f:selectItems value="#{handler.types2}" var="type2" itemLabel="#{type2.name}" />
</h:selectOneMenu>
</h:form>
Run Code Online (Sandbox Code Playgroud)
托管bean
@ManagedBean
@ViewScoped
public class Handler {
private Type1 selectedType1;
private List<Type1> types1;
private Type2 selectedType2;
private List<Type2> types2;
//getters, setters
public void valueChanged(ValueChangeEvent event) {
variants.clear();
if (event.getNewValue() != null) {
System.out.println("new value: " + event.getNewValue());
// Fill the second list
}
} …Run Code Online (Sandbox Code Playgroud) 所以我使用seam来尝试创建一个下拉菜单以将交付添加到数据库.我正在使用下拉菜单选择数据库中的哪个员工正在进行交付.菜单加载数据库中的所有员工就好了但是当我选择一个并单击添加时,我得到'null Converter'的错误"Conversion Error setting value'(hash for employee)'.
这是下拉菜单的代码:
<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}" required="false">
<f:selectItem itemValue="#{null}" itemLabel="Selct One"/>
<s:selectItems value="#{deliveryPort.empList}" var="emp" label="# {emp.employeeId} #{ emp.nameFirst}"/>
</my:dropdown>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.谢谢