0
<h:outputText value="Category: " />
<p:selectOneMenu value="#{categoryController.selectedCategory}">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{categoryController.categories}" var="category" itemLabel="#{category.name}" itemValue="#{category.categoryID}"/>
</p:selectOneMenu>

I have a form which lets users enter questions for a quiz. Categories can also be entered, and 1 category has many questions.

At first, the form allowed category ID's to be entered. Now, to make it a little more sophisticated, I instead populate a selectOneMenu with categories to be chosen from. The menu populates fine.

The problem: selecting a category from this list should mean selecting it's ID underneath so that a question entity refers to the correct category ID as always has been done. I was under the impression that the attribute

itemValue="#{category.categoryID}

should allow this. But this does not seem to be working, as question entities aren't appearing as added in my database when a category is chosen. I also tried

questionController.newQuestion.categoryID

as newQuestion has been working for text fields in the form. But it seems that the desired result still isn't achieved this way.

Is itemValue the attribute I should be using? And if not, then which?

UPDATE:

So still pretty stuck but here is some code showing how categories are implemented.

The category controller class:

public class CategoryController extends BasePageController {

@Autowired
private ICategoryRepository categoryRepository;

private List<Category> categories;

private Category newCategory = new Category();

private Category selectedCategory = new Category();

private Category[] selectedCategories;

and the Category class:

public class Category {

private int categoryID;
private String name;
Chucky
  • 1,701
  • 7
  • 28
  • 62
  • 1
    Have you tried ``? – user1983983 Jul 30 '13 at 10:05
  • Yes. But doing that changes which items appear in the list, not which values get passed in. Therefore categories should be in. – Chucky Jul 30 '13 at 10:20
  • `itemValue` is what you are looking for. It has to be the same datatype as the `value`-attribute of your ``. And you are retrieving the Entity from database via the id saved in `categoryController.selectedCategory`? Some more code would be helpful. – Lester Jul 30 '13 at 10:23
  • Which items appear in the list is controlled by the `value`-attribute of `f:selectItems`, so this can not be. Are you sure you changed the attribute at the right tag? – user1983983 Jul 30 '13 at 10:24
  • No, @Lester, all categories a user has created should appear in the list, hence the #{categoryController.categories} in the selectItems value attribute. And user1983983, you are correct. Hence why I shouldn't be populating the list with just a categoryID. – Chucky Jul 30 '13 at 10:25
  • Still fairly stuck. I'm not sure how I grab the ID of an existing category entity by it's name. – Chucky Jul 30 '13 at 11:50
  • I asked for value-attribute of `selectOneMenu` and not `selectItems`. Still more code would help. – Lester Jul 30 '13 at 17:32
  • In the updated code you do not select a category but change the id of the category that is already selected. Why not simply have `cat` itself as `itemValue` and select a category instead of it's id? Of course the value of `selectOneMenu` must be changed then, too. Please provide more code. – Lester Jul 30 '13 at 18:28
  • @user1983983, your first suggestion seems to have worked. My guess is I thought you were referring to selectItems and changed the value of that instead. Today though, feeling more fresh and alert, I made your recommended changes correctly and they worked. If you can flesh that into an answer I will accept it as correct. – Chucky Jul 31 '13 at 12:27
  • Put your mouse on top of `[selectonemenu]` tag until a popbox shows up and then click therein *info* link. – BalusC Jul 31 '13 at 14:06

1 Answers1

-1

As the entitiy which you would like to create and for which you are selecting a category is a question you have to set the values on questionController.newQuestion. I.e. you would have to set the categoryId on questionController.newQuestion.categoryID.

Which you can achieve by setting the value of p:selectOneMenu to that. So try the following for the p:selectOneMenu-open-tag:

<p:selectOneMenu value="#{questionController.newQuestion.categoryID}">
user1983983
  • 4,793
  • 2
  • 15
  • 24
  • 1
    Very bad advice. You shouldn't manipulate the entity ID yourself (the persistence layer is the only one responsible for this), but instead the whole entity itself. In first place, the `itemValue` was been wrong, it should have been `#{category}`. – BalusC Jul 31 '13 at 14:04
  • But how else can I access a category ID when that category is used in the question? – Chucky Jul 31 '13 at 14:11
  • I do not know whats done in the business logic, maybe this is the way OP needs it. I know that directly selecting a category and setting it to the entity via a converter would be the right way, but I was just trying to solve his problem, regarding the concrete question and assuming that the backend is implemented in the needed way. – user1983983 Jul 31 '13 at 14:12
  • Chucky, it's already inside `#{category}` itself. The alternative would be to replace `private Category selectedCategory` by `private Long selectedCategoryId`. This way the current answer would become valid. – BalusC Jul 31 '13 at 14:28