如何在没有值的情况下在Thymeleaf + Spring流中传递对象

时间:2015-07-02 06:31:39

标签: html5 spring-boot thymeleaf

在“搜索”页面中,用户可以单击“新建”按钮。如果用户在“搜索”页面中输入p_no,然后用户单击“新建”,则会在“创建新页面”中执行此编号。但实际上它不应该传递价值。下面的代码有什么问题。

PersonEntry.HTML(创建页面):

<form id="entry" action="#" th:object="${person}" method="post">
    <div id="boxes">
        <div class="col-md-1">
            <input type="text" class="form-control" id="pno" th:value="${person.p_no}" disabled="disabled" />
        </div>
    </div>
</form>

Controller.java:

@Controller 
@RequestMapping("/searchPersons")
public class SearchPersonController {           
@RequestMapping(value = "/createNewbtn", method = RequestMethod.POST)
public ModelAndView creatNewPerson(Person person) { 

    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName("PersonEntry");
    return modelAndView;

}
}   

Search.HTML:

<script>
     $(document).ready( function() { 
         createActionURL = function(obj) {
             $("#seach").attr("action", obj);
         }
         createActionURL(); 
        }); 
</script>
<form id="search" action="#" th:object="${person}" method="post">
<div class="col-md-1">
    <input type="text" size="2" class="form-control" id="p_no" th:field="*{p_no}"></input>
</div>
<div class="row" align="right" style="width: 100%">
    <input type="submit" value="Create New" class="btn btn-primary" id="createNewbtn" th:onclick="'javascript:createActionURL(\'' + @{/searchPersons/createNewbtn} + '\')'" 
/>  
</div>  
</form>

任何人都可以帮助解决这个问题。谢谢

1 个答案:

答案 0 :(得分:0)

以下代码为我工作

<script>
     $(document).ready( function() { 
         $('#createNewbtn').click(function{
               $("#seach").clearForm(); // clearForm function will be called to clear necessary input values.
         })
         createActionURL = function(obj) {
             $("#seach").attr("action", obj);
         }
         createActionURL(); 
        }); 
</script>