Search code examples
springjspspring-mvcjstlspring-form

Checked radion button based on model variable + Spring forms + JSP


I have model called cuntry.java with variable lang .I want to check the radio button option in JSP based on the lang. It could be 'EN' or 'FR'. when i wrote below code, throwing error in JSP page.

Code:

<form:radiobutton path="lang" id="language" value="FR" name="radios" <c:if ${cuntry.lang == 'FR' ? 'checked="checked"' : '' } /> ></form:radiobutton>

Error : Unterminated &lt;form:radiobutton tag


Solution

  • There are multiple errors in the form tag. When using Spring tags, you may not specify id and name attributes. They will be generated automatically from path variable. Tag <c:if ... /> inside another tag <form:radiobutton ... /> is not allowed. Remove end tag as well. This is the right way

    <form:radiobutton path="lang" value="FR" checked="${cuntry.lang == 'FR' ? 'checked' : '' }" />
    

    If the value of cuntry.lang is FR, the Spring tag will result the following HTML (after compiling)

    <input id="lang" name="lang" checked="checked" type="radio" value="FR" />
    

    otherwise

    <input id="lang" name="lang" type="radio" value="FR" />
    

    If you absolutely need attributes id = language and name = radios, you can not use Spring tag .

    You will find a good example here.