Sending radio button selected value to controller using thymeleaf

21 views Asked by At

I am new to springboot-thymeleaf. I need to send radio button value which default selected on form load to the controller. I will used the value to query database and result list will be populated in multi select dropdown. But radio button value is coming null in controller. Problem statement is on form load default selected radio button value will send into controller and based on that from database dat will come list will be returned and populated in multiselect dropdown. Can anyone please suggest what I am missing as this is forst time I am working with thymeleaf.

HTML code:

<form id="form1" action="#" th:action="@{/dbAssigned}" method="post" th:object="${formAssigned}">
    
    <th:block th:insert="~{fragments/mainMenu}"></th:block>
    <div class="page">
        
        <div class="main" style="width:940px;min-height:530px">
            
            <div style="clear:both;width:98%;margin-left:10px;">
                <h4>Database Assets and Users</h4>
                <div class="wizpanel1" style="min-height:16px;">
                    <span id="MainContent_lblMessage" class="message"></span>
                </div>
                
                <div class="dbxleft_">
                    
                    <!-- Org & User section -->
                    <div id="divAvailOrg" class="dbxll_">
                        <div id="divAvailTitle" class="dbxtitle_ ui-buttonset">
                            <table id="MainContent_rblAvailable" class="rbl">
                                <tbody>
                                <tr>
                                    <td>
                                        <input id="mainContent_rblAvailable_0" type="radio"
                                               th:field="*{mainContent}" value="1" checked="checked"
                                               class="ui-helper-hidden-accessible">
                                        <label for="MainContent_rblAvailable_0"
                                               class="ui-state-active ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left ui-state-hover"
                                               role="button" aria-disabled="false">
                                            <span class="ui-button-text">Organizations</span></label>
                                    </td>
                                    <td>
                                        <input id="mainContent_rblAvailable_1" type="radio"
                                               th:field="*{mainContent}" value="2"
                                               class="ui-helper-hidden-accessible"><label
                                            for="MainContent_rblAvailable_1"
                                            class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-right"
                                            role="button" aria-disabled="false"><span
                                            class="ui-button-text">Users</span></label>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <div id="divCntTitle" class="dbxtitleC_">
                            <h6 id="MainContent_h6UNO">75 Items</h6>
                        </div>
                        <div id="div3" class="dbxlbx">
                            <select size="23" name="ctl00MainContentlbxAvail" onchange="javascript:setTimeout('', 0)"
                                    id="MainContent_lbxAvail" style="font-size:13px;width:100%;">
                                <option value="18" title="Abbott"></option>
                                <option value="270" title="AbbVie"></option>
                            
                            </select>
                        </div>
                        <span style="width:50%;float:left;padding-top:5px;">
                    <a id="MainContent_lkbClearFilters" href="javascript:('','')">Clear Selection</a>
                </span>
                    </div>
                </div>
                
                <div class="dbxright_">
                    <!-- DB List -->
                    <div id="div4" class="dbxtitler_">
                        <h6 id="MainContent_h6DBLabel">Treatment Pathways Databases, 114 Items</h6>
                    </div>
                    <div class="wizpanel2txp_" style="margin-bottom:20px;">
                <span class="wizanswertxp_">
                     <select id="MainContent_lbxDBList" size="23" onchange="javascript:setTimeout()" name="MainContent"
                             style="font-size:13px;width:100%;" multiple>
                        <option value="1000760" title="123"></option>
                        <option value="1000776" title="456"></option>
                        <option value="1000803" title="789"></option>
        </select>
                </span>
                    </div>
                </div>
            </div>
        
        </div>
        <div class="clear">
        </div>
    </div>
</form>

Controller Code:

@RequestMapping(value = "/dbAssigned")
public Object lkbDbAssigned_Click(@ModelAttribute("formAssigned") FormAssigned formAssigned, Model model) {
    System.out.println("Radio Value:"+formAssigned.getMainContent());
    FormAssigned assigned = new FormAssigned();
    model.addAttribute("formAssigned",assigned);
    return "dbAssigned";
}

Form Model:

public class FormAssigned {

private String mainContent;

public String getMainContent() {
    return mainContent;
}

public void setMainContent(String mainContent) {
    this.mainContent = mainContent;
}

} dbAssigned.html page: enter image description here

Can anyone please suggest me what i need to do to make it done. Thanks, Prabhash Mishra

0

There are 0 answers