无法使用bindingResult设置表单元素

时间:2016-04-20 03:27:21

标签: javascript java jsp spring-mvc

我试图在提交中将表单中的值从另一个表单中获取。因为我需要使用帖子将信息从一个页面传递到另一个页面。 但是我对该对象得到null。

这是我的代码:

表单1

<form:form id="user" modelAttribute="user" method="post" action="">
    <form:input type="text" placeholder="" id="name" path="name" maxLength="10" style="width:150px" />
    <form:input type="text" placeholder="" id="address" path="name" maxLength="32" style="width:150px" />
    <form:input type="text" placeholder="" id="phone" path="phone" maxLength="32" style="width:150px" />        
</form:form>

表单2

<form:form  id="selectRuleforuserForm" action="/myApp/selectRule" enctype="multipart/form-data" modelAttribute="rule" method="post">        
    <form:input type="hidden" name="name" id="major_Version" path="name"/>
    <form:input type="hidden" name="address" id="internal_TradeId" path="adress"/>
    <form:input type="hidden" name="phone" id="reporting_Party" path="phone"/>      
    <input type="submit" id="selectRules" class="ui-widget" value="Select Rules"/>
</form:form >

的Javascript

$("#selectRuleforuserForm").submit(function(){

                    var user="${user.name}";
                    var add="${user.adress}";
                    var phone="${user.phone}";


                    $("name").val(user);
                    $("address").val(add);
                    $("phone").val(phone);

 });

控制器:

 @RequestMapping(value="/selectRule",method=RequestMethod.POST)
     public String updateRulesForUserRequest(Rule rule,BindingResult result,Model model,HttpServletRequest request,HttpServletResponse response){
        log.info("Select rules for user: "+rule.toString());


     }

日志:

2016-04-20 18:43:06,646 INFO  administrator - Select rules for user: Rule [name=, address=, phone=], org.springframework.validation.BindingResult.Rule=org.springframework.validation.BeanPropertyBindingResult: 0 errors - com.myApp.RuleController:112

2 个答案:

答案 0 :(得分:0)

ModelAttribute注释应该在方法参数而不是形式中使用:请参阅What is Model Attribute

答案 1 :(得分:0)

在<:p>中缺少“#”

   $("#name").val(user);
                    $("#address").val(add);
                    $("#phone").val(phone);