bind enum to radio button in jsp

2.8k Views Asked by At

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 !

1

There are 1 best solutions below

5
manish On

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.