简体   繁体   中英

Radio button selection and its value in spring mvc

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

Use a combination 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.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.

private String env = "QA 71"; // QA 71 will be selected by default

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM