2
我找到了一些關於此的示例和帖子,但找不到完整的解決方案或示例。
我想使用枚舉來保存單選按鈕的值。
請幫忙。將枚舉綁定到jsp中的單選按鈕
我有一個枚舉象下面這樣:
public enum MyEnum implements Serializable {
SUNDAY, MONDAY, TUESDAY
}
我在我的JSP形式如下:
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>
我有一個型號分類如下:
MyModel.java
public class MyModel {
private String temp;
private MyEnum myEnum;
//getters and setters here
}
我有一個控制器,如下:
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";
}
如何可以結合在MyJsp.jsp單選按鈕來枚舉值,這樣我根據所選的單選按鈕獲取枚舉值?
簡單的單選按鈕或彈簧:無線電標籤可以爲我工作。
請幫忙!
參考我上面的代碼,可以請您建議如何根據您的答案更改jsp後,在我的控制器中查找選定的無線電值。 我的意思是在這種情況下如何檢索所選單選按鈕的值。 – user3865751
用於檢索所選單選按鈕值的代碼片段位於上面的答案中(請參閱'@ RequestParam'和'FormModel')。試試這個作爲一個小樣本,你將能夠使其工作。 – manish
我的意思是在onSubmit方法中,我如何從FormModel中檢索FormModel類似的選定無線電值: public class FormModel { Day day; String temp; } 其中Day是枚舉 – user3865751