DataModel and DataModelSelection question
w17chm4n Oct 11, 2007 11:18 AMFirst of all, the code..
@Stateless @Name("QuestionCategoryController") public class QuestionCategoryControllerBean implements QuestionCategoryController { @Logger private Log log; @In(create = true) private QuestionCategoryManager questionCategoryManager; @In(required = false) private QuestionCategory questionCategory; @DataModel(value = "categories") private List<QuestionCategory> categoryList; @DataModelSelection(value = "categories") private List<Question> questionList; public void addCategory() { questionCategory.setCreated(new Date()); questionCategoryManager.addCategory(this.questionCategory); } public void removeCategory(QuestionCategory questionCategory) { //todo log.info("Trying to remove #{questionCategory.categoryName}"); if(questionCategory == null) { log.error("questionCategory is NULL !"); } else { questionCategoryManager.removeCategory(questionCategory); } } @Factory("categories") public void getAllQuestionsCategories() { setCategoryList(questionCategoryManager.getAllQuestionCategories()); } public List<QuestionCategory> getCategoryList() { return categoryList; } public void setCategoryList(List<QuestionCategory> categoryList) { this.categoryList = categoryList; } public List<Question> getQuestionList() { return questionList; } public void setQuestionList(List<Question> questionList) { this.questionList = questionList; } }
and
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:s="http://jboss.com/products/seam/taglib"> <body> <h:form> <h:inputText value="#{questionCategory.categoryName}" required="true"/> <h:commandButton type="submit" value="Add new category" action="#{QuestionCategoryController.addCategory}"></h:commandButton><br/> <h:outputText value="Categories rowCount is null" rendered="#{categories.rowCount == null}"/><br/> <h:outputText value="Categories rowCount = 0" rendered="#{categories.rowCount == 0}"/><br/> <h:outputText value="Available categories: " rendered="#{categories.rowCount > 0}"/><br/> <h:dataTable var="cat" value="#{categories}"> <h:column> <h:outputText value="[ #{cat.categoryName} ]"/> </h:column> <h:column> <h:outputText value=" - [ #{cat.created} ]"><f:convertDateTime pattern="dd-MMM-yyyy H:mm"/></h:outputText> </h:column> <h:column> <s:link value="remove" action="#{QuestionCategoryController.removeCategory(cat)}"/> </h:column> </h:dataTable> </h:form> <ui:debug hotkey="d"/> </body> </html>
Whats the problem is that QuestionCategoryController.removeCategory(cat) doesn`t work. I know it`s something about the annotations but I can`t find any answer.
As this controller should be used for adding new QuestionCategory, returning the existing categories and remove them i`ve also tried sth like that:
@Stateless @Name("QuestionCategoryController") public class QuestionCategoryControllerBean implements QuestionCategoryController { @Logger private Log log; @In(create = true) private QuestionCategoryManager questionCategoryManager; @DataModelSelection(value = "categories") @In(required = false) @Out(required = false) private QuestionCategory questionCategory; @DataModel(value = "categories") private List<QuestionCategory> categoryList; public void addCategory() { questionCategory.setCreated(new Date()); questionCategoryManager.addCategory(this.questionCategory); } public void removeCategory(QuestionCategory questionCategory) { //todo log.info("Trying to remove #{questionCategory.categoryName}"); if(questionCategory == null) { log.error("questionCategory is NULL !"); } else { questionCategoryManager.removeCategory(questionCategory); } } @Factory("categories") public void getAllQuestionsCategories() { setCategoryList(questionCategoryManager.getAllQuestionCategories()); } public List<QuestionCategory> getCategoryList() { return categoryList; } public void setCategoryList(List<QuestionCategory> categoryList) { this.categoryList = categoryList; } }
Oh and the exception, I simply get an NullPointerException (or in logs "questionCategory is NULL !") so as I think, the QuestionCategory object isn`t injected into the removeQuestion method.
Any suggestions how to deal with this problem ?