【问题标题】:How to pass a name to form action in thymeleaf?如何在百里香中传递名称以形成动作?
【发布时间】:2014-08-01 04:38:08
【问题描述】:

我的控制器正在跟踪

@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";
}

我的 HTML 页面是,

<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>

其中,roleDTO 是一个包含 roleId 和 RoleName 值的对象。所以现在我想在单击“编辑权限”按钮时传递这个 roleName 以形成操作。我们可以使用 {id}(id=${角色名})。同样我想在百里香中传递角色名。如何传递?请大家帮忙

【问题讨论】:

    标签: javascript html forms spring-mvc thymeleaf


    【解决方案1】:

    对于你的控制器方法

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

    表单动作应该是这样的:

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

    参考Thymeleaf Preprocessing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 2017-02-27
      • 2019-11-24
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      相关资源
      最近更新 更多