Java spring mvc中的单选按钮选择及其值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21028954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Radio button selection and its value in spring mvc
提问by Manish Singh
I am creating a web application which is based on Spring mvc framework.This application contains four radio buttons.My requirement is to pass the selection of radio buttons to controller that part is done.Now,once the selection is passed to controller then I don't know how to retain the selection of the radio on JSP.Because When I am returning the name of jsp as index.jsp then page is getting reloaded with intial values of no selection in radio button
我正在创建一个基于 Spring mvc 框架的 web 应用程序。这个应用程序包含四个单选按钮。我的要求是将单选按钮的选择传递给控制器,该部分已完成。现在,一旦选择传递给控制器,我就不不知道如何在 JSP 上保留收音机的选择。因为当我将 jsp 的名称作为 index.jsp 返回时,页面正在重新加载单选按钮中没有选择的初始值
<form id="envselection" action="${pageContext.request.contextPath}/env" method="post">
<input type="radio" name="env" id="radioSelection" value="QA 71" onclick="submitForm()">
<input type="radio" name="env" id="radioSelection" value="QA 72" onclick="submitForm()">
<input type="radio" name="env" id="radioSelection" value="QA 73" onclick="submitForm()">
<input type="radio" name="env" id="radioSelection" value="QA 74" onclick="submitForm()">
</form>
Javascript
Javascript
<script>
function submitForm() {
document.getElementById("envselection").submit();
}
</script>
Controller part
控制器部分
@RequestMapping(value = "/env", method = RequestMethod.POST)
public String env(HttpServletRequest request){
logger.info("parameter is "+request.getParameter("env"));
return "index";
}
Is there any other way of setting the selected value of radio button for java use and retaining the selection of radio button on JSP also.
有没有其他方法可以为java使用设置单选按钮的选定值并在JSP上保留单选按钮的选择。
Thanks in advance
提前致谢
采纳答案by Will Keeling
Use a combination of Spring's form
tags and a form backing bean.
使用 Spring 的form
标签和表单支持 bean 的组合。
First create a simple bean to back the form.
首先创建一个简单的 bean 来支持表单。
public class EnvBean {
private String env;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
}
Next modify your controller method to accept the bean.
接下来修改您的控制器方法以接受 bean。
@RequestMapping(value = "/env", method = RequestMethod.POST)
public String env(@ModelAttribute("envBean") EnvBean envBean){
logger.info("parameter is " + envBean.getEnv());
return "index";
}
To use the form
tags, add the following taglib directive to the top of your jsp:
要使用form
标签,请将以下 taglib 指令添加到您的 jsp 顶部:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
Then you can modify your form as follows:
然后你可以修改你的表单如下:
<form:form id="envselection" modelAttribute("envBean") action="/env">
<form:radiobutton path="env" value="QA 71" onclick="submitForm()"/>QA 71
<form:radiobutton path="env" value="QA 72" onclick="submitForm()"/>QA 72
<form:radiobutton path="env" value="QA 73" onclick="submitForm()"/>QA 73
<form:radiobutton path="env" value="QA 74" onclick="submitForm()"/>QA 74
</form:form>
After posting the form and returning to the same page, the value of the selected radio button will be retained.
提交表单并返回同一页面后,将保留所选单选按钮的值。
If you want a radio button to be selected by default when you first land on the page, then just default the value in the form backing bean.
如果您希望在第一次登陆页面时默认选择一个单选按钮,那么只需在表单支持 bean 中默认值。
private String env = "QA 71"; // QA 71 will be selected by default