1

I'm creating my first Spring MVC project and I'm having difficulty understanding how to redirect the user to different web pages based on the option they choose within an HTML option form. Here is the following code:

CarsController.java

@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect(@RequestParam("carmodel") String carmodel) {
    System.out.println("here's the value of car: " + carmodel);
    if (carmodel == "bmw") {
        return "redirect:bmwPage";
    } else {
        return "redirect:testPage";
    }
}

@RequestMapping(value = "/bmwPage", method = RequestMethod.GET)
public String bmwPage() {

   return "bmw_page";
}

@RequestMapping(value = "/testPage", method = RequestMethod.GET)
public String testPage() {

   return "test_page";
}

index.jsp (Form section)

<form action="/HelloSpringMVC/redirect" method="GET">
<p>Please select which car you are interested in buying:</p>
<select name="carmodel">
    <option value="SELECT">--- Select ---</option> 
    <option value="bmw">BMW</option>
    <option value="ford">Ford</option>
    <option value="mercedesbenz">Mercedes Benz</option>
</select>
<input type="submit" value="Submit"/>
</form>

Please let me know what else I can provide to make this question better? Thanks for the feedback!

Brandon
  • 933
  • 1
  • 10
  • 19

0 Answers0