Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created August 3, 2017 17:35
Show Gist options
  • Select an option

  • Save darbyluv2code/8e8eaa9e0452bdc9e911216a4e3b8b9b to your computer and use it in GitHub Desktop.

Select an option

Save darbyluv2code/8e8eaa9e0452bdc9e911216a4e3b8b9b to your computer and use it in GitHub Desktop.
Spring MVC - Drop Down Lists
/**
*
*/
package com.luv2code.springdemo.mvc;
import java.util.SortedMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author Rajib
*
*/
@Controller
@RequestMapping("/student")
public class StudentController {
@Value("#{countryOptions}")
private SortedMap<String, String> countryOptions;
/**
* @param theModel
* @return String
*/
@RequestMapping("/showForm")
public String showForm(Model theModel){
// create a student object
Student theStudent = new Student();
// add student object to the model
theModel.addAttribute("student", theStudent);
System.out.println(countryOptions);
// add the country options to the model
theModel.addAttribute("theCountryOptions", countryOptions);
return "student-form";
}
/**
* @param theStudent
* @return String
*/
@RequestMapping("/processForm")
public String processForm(@ModelAttribute("student") Student theStudent){
// log the input
System.out.println("theStudent : " + theStudent.getFirstName() + " " + theStudent.getLastName() + " " +theStudent.getCountry());
/*return "student-confirmation";*/
return "redirect:/student/showForm";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment