2

I found a few examples and posts regarding this but couldn't find a complete solution or example.
I want to use enum to hold the value of radio buttons.
Please help.

I have an enum like below:

public enum MyEnum implements Serializable {
SUNDAY, MONDAY, TUESDAY
}

I have a form in my jsp as below :

MyJSP.jsp

<form:form action="" method="post">
    <input type="radio" id="id1" name="name1" value="value1" >
    <input type="radio" id="id2" name="name2" value="value2" >
    <input type="radio" id="id3" name="name3" value="value3" >               
</form:form>

I have a model class as below:
MyModel.java

public class MyModel {
    private String temp;
    private MyEnum myEnum;
    //getters and setters here
}

I have a controller as below :
MyController.java

@Controller
@SessionAttributes(MyModel)
public class MyController {

  @ModelAttribute(MyModel)
  public MyModel initMyModel(final HttpServletRequest httpRequest, final HttpSession session) {
    MyModel myModel = new MyModel(.....);
    //code goes here

    return myModel;
  }

  @RequestMapping(value = , method = RequestMethod.GET)
  public String initialViewMyModel(final HttpServletRequest httpRequest, final HttpSession session) {
    return MyJsp;
  }

  @SuppressWarnings("boxing")
  @RequestMapping(value = , method = RequestMethod.POST)
  public String onSubmit(final @ModelAttribute(MyModel) MyModel myModel, final BindingResult bindingResult, final HttpServletRequest httpRequest, final ModelMap modelMap) {
    HttpSession session = httpRequest.getSession();
    //Here is the issue
    MyEnum[] myEnum= myModel.getmyEnum();   //I want here to get the index or anything so that I could identify the selected radio button and use the switch case further but getting null here
    switch (//----) {
        case SUNDAY :
            break;

        case MONDAY :
            break;

        case TUESDAY :
            break;
    }

    return "someotherJSP";
  }

How can I bind the radio buttons in MyJsp.jsp to the enum values so that I get the enum value based on the selected radio button ?
Either of the simple radio button or spring:radio tag can work for me.
Please help !

user3865751
  • 21
  • 1
  • 3

1 Answers1

0

Radio buttons representing a single action must have the same name. Start with the following:

<form:form action="" method="post">
  <input type="radio" name="day" value="SUNDAY" >
  <input type="radio" name="day" value="MONDAY" >
  <input type="radio" name="day" value="TUESDAY" >               
</form:form>

Then, simply collect the radio button value in the controller:

@RequestMapping
public String onSubmit(@RequestParam MyEnum day)
{
  ...
}

If the user had selected one of the radio button values, you will be able to read that value in the mapped controller method. You can also collect the selected value in a field inside a model object, such as:

@RequestMapping
public String onSubmit(FormModel model)
{
  ...
}

where FormModel is a Java class containing a field called day.

manish
  • 19,695
  • 5
  • 67
  • 91
  • With reference to my code above can you please suggest how can I retrive the selected radio value inside my controller after changing the jsp as per your answer. I mean how to retrive the value of selected radio button in this case. – user3865751 Jul 23 '14 at 05:32
  • The code snippets for retrieving the value of the selected radio button are in my answer above (see `@RequestParam` and `FormModel`). Try this out as a small sample and you will be able to make it work. – manish Jul 23 '14 at 07:25
  • I mean inside the method onSubmit, how do I retrieve selected radio value from FormModel where FormModel is something like : public class FormModel { Day day; String temp; } where Day is the enum – user3865751 Jul 23 '14 at 08:37
  • `model.day`, or `model.getDay()`, depending on how you have coded `FormModel`. – manish Jul 23 '14 at 08:53
  • You can check out my sample application running on [Google App Engine](http://spring-mvc-enum.appspot.com) and its source code on [Github](https://github.com/manish-in-java/spring-mvc-enum). – manish Jul 23 '14 at 11:58