简体   繁体   中英

How to store data of “Drop-down” list from JSP to our MySql Database?

In my application I have a dropdown list in the form, I want to take one value of that dropdown list from User and store it in the database. I am new to spring development and I do not know what is the datatype of the dropdown list in MySql and plus how should I configure the controller and entity class?

I have used ENUM in MySql database.

`domain` enum('Web-Development', 'Software Development','Application Development') DEFAULT NULL,

You can follow this approach followed for Dropdown:

1] Create a lookup table where you define all your consatnts with their associated type and id something like as said in this link :

Look up table .

2] Your Drop-down will display these values from lookup table as soon as a value is selected in drop down their associated id will be used in backend to store the selected value can be used to store in database rather than actual values as this id should be unique and be used for DB CRUD operations.

this might help, I have solved the issue for tempory base or for small scale development. All I have done is, I have changed datatype in MySql from ENUM to VARCHAR(45) . ( ENUM also works fine). And then I have manually added the dropdown values in the controller class.

@ModelAttribute("domainList")
    public List<String> getCountryList() {
        List<String> domainList = new ArrayList<String>();
        domainList.add("Web-Development");
        domainList.add("Software Development");
        domainList.add("Application Development");
        return domainList;
    }

In my JSP file, I have called this under <form:select>, <form:option>, and <form:options>

<td>
    <form:select path="domain">
    <form:option value="NONE" label="Select" />
    <form:options items="${domainList}" />
    </form:select>
</td>

For, Data binding in Entity class I have just used return type as String and generate getters , setters and toString .

That's all I have done, it is working. Happy Coding...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM