问题描述
我有Article DTO(Article.java;代码摘录)
I have an Article DTO (Article.java; code excerpts)
public class Article {
private int id;
public Article() {
this.id = 0;
}
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
我有一个用于编辑文章的视图模板(edit_article.xhtml;代码摘录):
I got a view template for editing the article (edit_article.xhtml; code excerpts):
<h:form id="article_form">
<p:messages id="messages"/>
<h:panelGrid columns="2">
<h:outputText value="" />
<h:inputHidden id="articleId" value="#{hqArticleView.article.id}" converter="javax.faces.Integer" />
</h:panelGrid>
</h:form>
并且我有一个视图支持bean(HqArticleView.java;代码摘录):
and I have a view backing bean (HqArticleView.java; code excerpts):
@Named("hqArticleView")
@RequestScoped
public class HqArticleView implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Logger log;
@Inject
private IArticleService articleService;
private Article article;
private List<Article> articles;
private Map<String, String> requestParams;
@PostConstruct
private void init() {
log = LoggerFactory.getLogger(getClass());
requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
article = new Article();
article.setPublish(true);
if (requestParams.containsKey("id")) {
article.setId(Integer.parseInt(requestParams.get("id")));
article = articleService.getArticle(article);
}
}
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
public List<Article> getArticles() {
articles = articleService.getAll();
Collections.reverse(articles);
return articles;
}
public void save() {
log.debug("save pressed");
}
public void edit(Article article) {
log.debug("edit pressed | id=" + article.getId());
}
public void delete(Article article) {
log.debug("delete pressed | id=" + article.getId());
}
}
问题是:我尝试访问视图时遇到错误:类Article没有可写的属性'id'.文章中的所有其他字段均已正确处理,只有id是一个问题.奇怪的是,当我在Bean中包装Article对象的id获取器和setter时:
The problem is: I try to access the view I'm getting an error: The class Article does not have a writable property 'id'.all other fields from the Article are processed correctly, only the id is a problem.Strange is, when I wrap the id getter and setter of the Article object inside the bean in:
public void setId(int id){
this.article.setId(id);
}
public int getId(){
return this.article.getId();
}
然后完美运行.我想念什么?
then it works perfectly. what am I missing?
推荐答案
在检查要读取的属性类型时,EL会查看吸气剂的返回类型:
When examing the property type for reading, EL looks at the return type of the getter:
public Integer getId() {
return id;
}
因此是Integer
类型.当EL需要写入更改后的值时,EL期望设置器采用相同的类型:
It's thus of type Integer
. When EL needs to write the changed value, EL is expecting a setter taking the same type:
public void setId(Integer id) {
this.id = id;
}
但是,您的Article
类中不存在这样的设置器.相反,您有一个使用int
的二传手.这就是EL抛出PropertyNotWritableException
的原因.这意味着它找不到完全相同类型的匹配设置器.通过在setter中使用Integer
而不是int
进行相应的修复(或者,在这种情况下更好的方法是在getter中使用int
而不是Integer
)进行修复.
However, no such setter exists in your Article
class. Instead, you've a setter taking an int
. That's exactly why EL threw a PropertyNotWritableException
. It means that it couldn't find a matching setter taking exactly the same type. Fix it accordingly by using Integer
instead of int
in the setter (or, in this particular case better, by using int
instead of Integer
in the getter).
当您在控制器中分解模型时,它之所以起作用(顺便说一句不好的做法)是因为getter和setter彼此匹配.
That it worked when you exploded the model in the controller (bad practice by the way) is because the getter and setter matched each other.
这篇关于javax.el.PropretyNotWritableException:类Article没有可写属性'id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!