Spring boot 如何使用thyemeleaf发布输入字符串值?

Spring boot 如何使用thyemeleaf发布输入字符串值?,spring-boot,thymeleaf,Spring Boot,Thymeleaf,我的项目基于springboot、Thymeleaf、mysql、html和Jquery 我试图将输入字符串数据发布到@RestController页面,但它被接收为空,这意味着null。 这是我的代码详细信息 HTML <form id="addchargesformid" method="post" th:action="@{/addcharge}"> <!-- input box add --> <div class="form-group">

我的项目基于springboot、Thymeleaf、mysql、html和Jquery

我试图将输入字符串数据发布到@RestController页面,但它被接收为空,这意味着null。 这是我的代码详细信息

HTML

<form id="addchargesformid" method="post" th:action="@{/addcharge}">  
 <!-- input box add -->
 <div class="form-group">
    <label for="chargeName">Add New Charge*</label> 
    <input th:id="chargeName" th:name="chargeName" th:value="${chargeName}" type="text" class="form-control" placeholder="Ex : Maintanence charge">
</div>
<br/>
<button th:type="submit" id="addchargebtnid" class="btn btn-info">Add New Charge</button>
</form>

请帮助我解决此问题…

因为您不执行从该表单到模型类的任何绑定活动,并且您只想传递字符串,请尝试使用@RequestParam而不是@ModelAttribute:

public ModelAndView doAddCharge(@RequestParam("chargeName") String chargeName)

由于您不执行从该表单到模型类的任何绑定活动,并且只想传递字符串,请尝试使用@RequestParam而不是@ModelAttribute:

public ModelAndView doAddCharge(@RequestParam("chargeName") String chargeName)

我做了一个代码更改…在HTML中添加

th:object=“${chargeName}”

在表单中,添加输入标记

name=“chargeName”

此处为完整代码

 <form id="addchargesformid" method="post" th:action="@{/addcharge}" th:object="${chargeName}">  
     <!-- input box add -->
     <div class="form-group">
        <label for="chargeNameid">Add New Charge</label> 
        <input id="chargeNameid" name="chargeName" type="text" class="form-control" placeholder="Ex : Maintanence charge">
    </div>
    <br/>
    <button th:type="submit" id="addchargebtnid" class="btn btn-info">Add New Charge</button>
    </form>

我做了一个代码更改…在HTML中添加

th:object=“${chargeName}”

在表单中,添加输入标记

name=“chargeName”

此处为完整代码

 <form id="addchargesformid" method="post" th:action="@{/addcharge}" th:object="${chargeName}">  
     <!-- input box add -->
     <div class="form-group">
        <label for="chargeNameid">Add New Charge</label> 
        <input id="chargeNameid" name="chargeName" type="text" class="form-control" placeholder="Ex : Maintanence charge">
    </div>
    <br/>
    <button th:type="submit" id="addchargebtnid" class="btn btn-info">Add New Charge</button>
    </form>