Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a name to form action in thymeleaf?

My controller is following

@RequestMapping(value = "/editpermission/{roleName}/{roleId}", method = RequestMethod.GET)   
public String AddRolePermissionPage(@PathVariable Integer roleId,
                                    @PathVariable String roleName, ModelMap modelMap) {
    List<Systems> systemList=systemServices.findAll();
    modelMap.addAttribute("roleId",roleId);
    modelMap.addAttribute("systemList",systemList);
    modelMap.addAttribute("roleName",roleName);
    return "security/role/role_permissions";
}

And my HTML page is,

<form th:action="@{/roles/update/{id}(id=${roleId})}" method="post"
      onSubmit="return formValidation();" commandName="roleDTO">
    <!-- {id1}(id1=${roleName}) -->
    <div class="form-group">
        <div class="col-xs-12 col-sm-9">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right"
                   for="rolename">Name <font color="red">*</font></label>
            <div class="clearfix">
                <input type="text" id="roleName" name="roleName"
                       class="col-xs-12 col-sm-5" th:value="${roleDTO.roleName}"/>
            </div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>

    <div class="form-group">
        <div class="col-xs-12 col-sm-9">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right"
                   for="active">Active</label>
            <div class="clearfix">
                <span class="input-icon input-icon-right">
                    <label style="margin-top: 10px;"></label>
                    <input type="checkbox" class="ace ace-switch ace-switch-2"
                           name="createActive" th:checked="${roleDTO.createActive}"/> 
                    <span class="lbl"></span>
                    <i class="icon-leaf green"></i>
                </span>
            </div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>


    <div class="form-group">

        <div class="col-xs-12 col-sm-9">
            <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="active">
                <!-- Create User : -->
            </label>
            <div class="clearfix">
                <input type="hidden" id="createUser" name="createUser"
                       class="col-xs-12 col-sm-5" th:value="${roleDTO.createUser}"/>
            </div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>

    <div class="form-group">

        <div class="col-xs-12 col-sm-9">
            <div class="clearfix">
                <input type="hidden" id="roleId" name="roleId"
                       class="col-xs-12 col-sm-5" th:value="${roleDTO.roleId}"/>
            </div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>

    <div class="form-group">
        <div class="col-xs-12 col-sm-6">
            <div class="wizard-actions">
                <button class="btn btn-sm btn-success">
                    <i class="icon-ok bigger-110"></i> Submit
                </button>
                &nbsp;&nbsp;&nbsp;
                <button class="btn btn-sm btn-grey" type="reset">
                    <i class="icon-undo bigger-110"></i> Reset
                </button>
            </div>
        </div>
    </div>
</form>

<form th:action="@{/roles/editpermission/{id}(id=${roleName})/{id}(id=${roleId})}"
      method="get" id="validation-form" style="float:left;">

    <div class="form-group">
        <div class="col-xs-12 col-sm-6">
            <div class="wizard-actions">
                <button class="btn btn-sm btn-success">
                    <i class="icon-ok bigger-110"></i> Edit Permission
                </button>
            </div>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>
</form>

In which,the roleDTO is an object that contains roleId and RoleName values.SO now i want to pass this roleName to form action when i click the Edit Permission button.We can pass roleId using {id}(id=${roleName}).Similarly i want to pass roleName in thymeleaf.How to pass? Plz anybody help

like image 350
Md Aslam Avatar asked Aug 01 '14 04:08

Md Aslam


People also ask

How do you pass parameters in Thymeleaf?

Request parameters can be easily accessed in Thymeleaf views. Request parameters are passed from the client to server like: https://example.com/query?q=Thymeleaf+Is+Great! In the above example if parameter q is not present, empty string will be displayed in the above paragraph otherwise the value of q will be shown.

What is th object?

We use th:action to provide the form action URL and th:object to specify an object to which the submitted form data will be bound. Individual fields are mapped using the th:field=”*{name}” attribute, where the name is the matching property of the object.


1 Answers

For your controller method

@RequestMapping(value = "/editpermission/{roleName}/{roleId}", method = RequestMethod.GET)   
public String AddRolePermissionPage(@PathVariable Integer roleId, @PathVariable String roleName,ModelMap modelMap){
    //method body
} 

the form action should like this :

 <form th:action="@{/roles/editpermission/__${roleDTO.roleName}__/__${roleDTO.roleId}__}" method="get" id="validation-form" style="float:left;"> 

Refer Thymeleaf Preprocessing

like image 178
faizi Avatar answered Sep 27 '22 21:09

faizi