Multiple Select Dao Mysql Spring MVC

1.8k Views Asked by At

I want to build in a select with the possibility of marking more elements. So multiple="true". I use mysql and the Dao Technology of spring. I get the values for the select from database successfully. But now I have a problem when inserting the selected values to my database.

The important tables for that are:

desc

The table demo.instrumente is filled with data like guitar, piano, etc. and an id. These values (i.e. guitar, piano) are displayed in the multiple select.

A user is able to select maybe 2 or 3 instruments. So I need to add the following instruments to the students. I do this with the table schueler_instrumente. Every student and instrument has an id. So i need to create data like this:

student_id 1 and instrument_id 2
student_id 1 and instrument_id 5

Here is my code for the instrument model class:

public class Instrumente {

  private Integer instrumentid;
  private String instrumentname;

  //...getters and setters
}

This code is part of my controller:

@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model) {
  model.put("instrumentListe", schuelerManager.getinstrumente());
  return "usercenter";
}

And here's the relevant part of my schuelerManager

public Map<String, String> getinstrumente() {
  Map<String,String> result = new LinkedHashMap<String,String>();
  for (Instrumente instrument : instrumentDao.getInstrumente()) {
    result.put(instrument.getInstrumentid().toString(),
               instrument.getInstrumentname());
  }
  return result;
}

And here's how I get the data from my database:

public  List<Instrumente> getInstrumente() {

  return getJdbcTemplate().query("SELECT * FROM instrumente",
                                 new RowMapper<Instrumente>() {
                                   public Instrumente mapRow(ResultSet rs,
                                                             int rowNum)
                                     throws SQLException {
                                     Instrumente instrument = new Instrumente();
                                     instrument.setInstrumentid
                                       (rs.getInt("Instrument_ID"));
                                     instrument.setInstrumentname
                                       (rs.getString("Instrumentenbezeichnung"));
                                     return instrument;
                                   }
                                 });
}

I do now know what I need to do in order to get the selected values from the select list. What do I have to write to the path="?" in the jsp.

I think that I can get a list of values back but how can I insert this list to my table schueler_instrument. Did I need to make a while or for repeat and make an insert everytime? I can't find any nice example on the Internet. I hope someone can show me how to do this maybe with some code snippets.

1

There are 1 best solutions below

2
On

In your controller's showUserForm() you are adding correctly your date to the ModelMap (or you could use its Java-5 counterpart Model). Now you'll need to use Spring's form tags in your view to represent the options in a dropdown/list way and receive onSubmit back in your controller the results that you will further persist in your db.

Have a look for a full example here.

Something that is not showcased in this example and I suggest you take a look is the @ModelAttribute annotation which is a nice way to communicate objects and values between your controller and your jsp view. For an example have a look in this tutorial.