<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;