一旦提交了自动完成表单并且AJAX调用handleSelect方法 - 我正确地接收了事件(例如:org.primefaces.event.SelectEvent [source=org.primefaces.component.autocomplete.AutoComplete@3d7b6188]),但是在下一步中event.getObject()为null。我想也许Casting存在问题。
1)表格
<p:autoComplete
id="searchArticle"
value="#{articleBean.selectedArticle}"
completeMethod="#{articleBean.complete}"
var="article"
itemValue="#{article}"
itemLabel="#{article.articleName}"
converter="articleConverter" >
<p:ajax event="itemSelect" listener="#{articleBean.handleSelect}" update=":searchForm:message"/>
</p:autoComplete>
2)handleSelect方法
public void handleSelect(SelectEvent event){
System.out.println("handleSelect - event=" + event);
// returns example : org.primefaces.event.SelectEvent[source=org.primefaces.component.autocomplete.AutoComplete@7dffb095]
Article a = ((Article) event.getObject());
System.out.println("handleSelect - object=" + a); // returns null
}
3)ArticleBean
package bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.primefaces.event.SelectEvent;
import business.ArticleService;
import model.Article;
@Model
public class ArticleBean implements Serializable {
private static final long serialVersionUID = -4080308091985560927L;
private int articlePrice;
private String articleNumber;
private String articleName;
private String articleDescription;
// for Search field
private Article selectedArticle;
private List<Article> articles = new ArrayList<Article>();
// EJB
@Inject
ArticleService articleService;
// Default Constructor
public ArticleBean() {
super();
}
@PostConstruct
public void init(){
articles = articleService.getArticles();
}
// search - suggestions
public List<Article> complete(String query) {
List<Article> suggestions = new ArrayList<Article>();
for(Article article : articles) {
if(article.getArticleName().startsWith(query) || article.getArticleDescription().startsWith(query)) {
suggestions.add(article);
}
}
return suggestions;
}
// after Search selected
public void handleSelect(SelectEvent event){
System.out.println("handleSelect - event=" + event);
// returns example : org.primefaces.event.SelectEvent[source=org.primefaces.component.autocomplete.AutoComplete@7dffb095]
Article a = ((Article) event.getObject());
System.out.println("handleSelect - object=" + a); // returns null
}
public String save() {
System.out.println("Saving..." + this.articleName + " " + this.articleDescription);
Article article = new Article();
article.setArticleNumber(articleNumber);
article.setArticlePrice(articlePrice);
article.setArticleDescription(articleDescription);
article.setArticleName(articleName);
articleService.save(article);
addMessage("New Product created");
return("index");
}
public void addMessage(String summary){
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
FacesContext.getCurrentInstance().addMessage(null,message);
}
// Getters and Setters
// 1.ArticleNumber
// format "xx-1234567890"
@Pattern(regexp="[A-Z]{2}-[0-9]{1,10}$", message="Number : Format AA-1111111")
public String getArticleNumber() {
return articleNumber;
}
public void setArticleNumber(String articleNumber) {
this.articleNumber = articleNumber;
}
// 2.ArticleName
@NotNull(message="Name : Must be inserted") // nenulove
@Pattern(regexp="[a-zA-Z0-9-_\\s+]{3,30}$", message="Name : Invalid characters inserted") // povolene znaky : male, Velke, cisla a -,_, $ = konec Stringu
@Size(min=3, max=30, message="Name : Invalid lenght (should be 3-30)") // delka Stringu min 3 a max 30
public String getArticleName() {
return articleName;
}
public void setArticleName(String articleName) {
this.articleName = articleName;
}
// 3.ArticlePrice
@Min(1) // hodnota min 1
public int getArticlePrice() {
return articlePrice;
}
public void setArticlePrice(int articlePrice) {
this.articlePrice = articlePrice;
}
// 4. ArticleDescription
public String getArticleDescription() {
return articleDescription;
}
public void setArticleDescription(String articleDescription) {
this.articleDescription = articleDescription;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public Article getSelectedArticle() {
return selectedArticle;
}
public void setSelectedArticle(Article selectedArticle) {
this.selectedArticle = selectedArticle;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
}
4)文章
package model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@NamedQueries(
{
@NamedQuery(name=Article.GET_ARTICLE_BY_ID, query="Select a From Article a where a.id = :id"),
@NamedQuery(name=Article.GET_ALL_ARTICLE, query="Select a From Article a")
}
)
@Entity
@Table(name="article")
public class Article {
public static final String GET_ALL_ARTICLE = "Article.get_all_articles";
public static final String GET_ARTICLE_BY_ID = "Article.get_article_by_id";
private int id;
private double articlePrice;
private String articleNumber;
private String articleName;
private String articleDescription;
public Article() {
super();
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getArticleNumber() {
return articleNumber;
}
public void setArticleNumber(String articleNumber) {
this.articleNumber = articleNumber;
}
public String getArticleName() {
return articleName;
}
public void setArticleName(String articleName) {
this.articleName = articleName;
}
public String getArticleDescription() {
return articleDescription;
}
public void setArticleDescription(String articleDescription) {
this.articleDescription = articleDescription;
}
public double getArticlePrice() {
return articlePrice;
}
public void setArticlePrice(double articlePrice) {
this.articlePrice = articlePrice;
}
}
5)文章转换器
package convertor;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import model.Article;
@FacesConverter(value="articleConverter")
public class ArticleConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
if(submittedValue.trim() == null || submittedValue.trim().isEmpty())
return null;
else
{
Article article = new Article();
article.setArticleName(submittedValue);
return article;
}
}
// we dont use it
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
return null;
}
}