【发布时间】:2014-11-08 06:26:39
【问题描述】:
我有以下addService.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head><title>Add Service</title></h:head>
<h:body>
<h:form>
<p:outputLabel for="name" value="Service Name" />
<p:inputText id="name" value="#{serviceMB.name}"></p:inputText>
<p:outputLabel for="categoryCompId" value="Service Category" />
<p:selectOneMenu id="categoryCompId" value="#{serviceMB.selectedCategory}" >
<f:selectItem itemLabel="Select Category" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{serviceMB.categories}" var="category" itemLabel="#{category.name}" itemValue="#{category}"/>
</p:selectOneMenu>
<p:commandButton id="saveService" value="Save" action="#{serviceMB.saveServiceAndExit}"/>
</h:form>
</h:body>
还有下面的托管bean
@Component
@Controller
@Scope(value = "request")
@ManagedBean(name="serviceMB")
@RequestScoped
public class ServicesManagedBean implements Serializable {
private Category selectedCategory;
private String name;
public Category getSelectedCategory() {
return selectedCategory;
}
public void setSelectedCategory(Category selectedCategory) {
this.selectedCategory = selectedCategory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Category> getCategories() {
return categoryService.getCategories();
}
}
setSelectedCategory() 方法不会被调用并且selectedCategory 字段始终为空,尽管我在<p:selectOneMenu 中使用它。我的代码有什么问题吗?
我添加了一个转换器,但是当我使用 Object selectedObject = ((HtmlSelectOneMenu) arg1).getValue(); 时,它也会返回 null。
【问题讨论】:
-
托管 bean 类上有多余的注释。他们为什么在那里?
-
我需要将 Spring 服务 bean 注入托管 bean。所以,我也需要这个托管 bean 在 Spring 容器上可见。
标签: jsf primefaces