Created
November 17, 2017 22:17
-
-
Save darbyluv2code/dd5aacbd5dd75ac87548cd070819a301 to your computer and use it in GitHub Desktop.
Spring MVC - Fix for no bean found
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.luv2code.springdemo.mvc; | |
| import java.util.LinkedHashMap; | |
| public class Student { | |
| private String firstName; | |
| private String lastName; | |
| private String favoriteLanguage; | |
| private LinkedHashMap<String, String> favoriteLanguageOptions; | |
| private String country; | |
| public String getCountry() { | |
| return country; | |
| } | |
| public void setCountry(String country) { | |
| this.country = country; | |
| } | |
| // create no-arg constructor | |
| public Student() { | |
| // populate favorite language options | |
| favoriteLanguageOptions = new LinkedHashMap<String, String>(); | |
| // parameter order: value, display label | |
| // | |
| favoriteLanguageOptions.put("Java", "Java"); | |
| favoriteLanguageOptions.put("C#", "C#"); | |
| favoriteLanguageOptions.put("PHP", "PHP"); | |
| favoriteLanguageOptions.put("Ruby", "Ruby"); | |
| } | |
| public String getFavoriteLanguage() { | |
| return favoriteLanguage; | |
| } | |
| public void setFavoriteLanguage(String favoriteLanguage) { | |
| this.favoriteLanguage = favoriteLanguage; | |
| } | |
| public LinkedHashMap<String, String> getFavoriteLanguageOptions() { | |
| return favoriteLanguageOptions; | |
| } | |
| // define getter/setter methods | |
| public String getFirstName() { | |
| return firstName; | |
| } | |
| public void setFirstName(String firstName) { | |
| this.firstName = firstName; | |
| } | |
| public String getLastName() { | |
| return lastName; | |
| } | |
| public void setLastName(String lastName) { | |
| this.lastName = lastName; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.luv2code.springdemo.mvc; | |
| import java.util.Map; | |
| 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; | |
| @Controller | |
| @RequestMapping("/student") | |
| public class StudentController { | |
| @Value("#{countryOptions}") | |
| private Map<String, String> countryOptions; | |
| /* | |
| @Value("#{favoriteLanguageOptions}") | |
| private Map<String, String> favoriteLanguageOptions; | |
| */ | |
| @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); | |
| // add the country options to the model | |
| theModel.addAttribute("theCountryOptions", countryOptions); | |
| System.out.println("\n\ntheStudent.getFavoriteLanguageOptions(): " + theStudent.getFavoriteLanguageOptions() + "\n\n"); | |
| // theModel.addAttribute("favoriteLanguageOptions", favoriteLanguageOptions); | |
| return "student-form"; | |
| } | |
| @RequestMapping("/processForm") | |
| public String processForm(@ModelAttribute("student") Student theStudent) { | |
| // log the input data | |
| System.out.println("theStudent: " + theStudent.getFirstName() | |
| + " " + theStudent.getLastName()); | |
| return "student-confirmation"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment