最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Radio button selection and its value in spring mvc - Stack Overflow

programmeradmin4浏览0评论

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

<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

<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.

Thanks in advance

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

<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

<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.

Thanks in advance

Share Improve this question asked Jan 9, 2014 at 19:05 Manish SinghManish Singh 3101 gold badge6 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Use a bination of Spring's form tags and a form backing bean.

First create a simple bean to back the form.

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.

@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:

<%@ taglib prefix="form" uri="http://www.springframework/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.

private String env = "QA 71"; // QA 71 will be selected by default
发布评论

评论列表(0)

  1. 暂无评论