Skip to content
Advertisement

Save drop-down list value to database in Struts 2 with Hibernate

I want to save the selected value of drop-down list into the database.

First index.jsp is loaded. From index.jsp, we can go to register.jsp when we click register URL of index.jsp.

struts.xml:

JavaScript

index.jsp:

JavaScript

register.jsp:

JavaScript

Action class is:

JavaScript

the drop down list is actually only one of the form fields. there are other fields also in the form.

All values other than month field can be entered into database. for month field, the value being entered is null.

I think the value of drop-down is not being taken.

Advertisement

Answer

I would suggest to change the months member variable to a Map like this:

JavaScript

Then, implement the Preparable interface in you action, and initialize the map with something like this:

JavaScript

Of course, you need to add a getter and a setter for months

Also, add an private Integer month member variable in your action, that will hold the selected month by the user (again, with getters and setters)

Then, use the following s:select tag in your JSP:

JavaScript

So now in your execute method, the month member variable should hold the selected month. 0 should be no selection, 1 should be January, etc.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement