Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  190] [ 7]  / answers: 1 / hits: 23914  / 11 Years ago, thu, january 9, 2014, 12:00:00

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


More From » java

 Answers
36

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

[#73280] Wednesday, January 8, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amari

Total Points: 736
Total Questions: 111
Total Answers: 90

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;