从html中检索值并打印到控制器

时间:2018-05-21 04:28:49

标签: java spring-mvc spring-boot thymeleaf

从上面的标题中,有没有办法从html页面获取值? 目前,我使用thymleaf模板,并希望从th:value =“ {projectName}”,th:text =“ {projectTask}”和th:value =“* {projectStatus}”获取值

<div class="container" style="padding-top: 70px">
    <h2>Status Project</h2>
    <form action="#" th:action="@{/updateProject/__${projectId}__}"  method="post"
          id="addProject">
        <div>
            <label for="usr">Project Name:</label>
            <input type="text" class="form-control" id="usr" th:value="*{projectName}">
        </div>
        <div class="form-group" style="padding-top: 10px">
            <label for="comment">ITO Task:</label>
            <textarea class="form-control" rows="5" id="comment" th:text="*{projectTask}"></textarea>
        </div>

        <div class="col-xs-4" style="padding-left: 0px; ">
            <label for="usr">Status:</label>
            <input type="text" class="form-control" id="status" th:value="*{projectStatus}">
        </div>


        <div class="col-sm-offset-2 col-sm-10" style="padding-top: 20px;right: 205px">
            <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

解决方案1: 单独接受所有参数,如下面的代码。

@RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@RequestParam("projectName") String projectName,@RequestParam("projectTask") String projectTask,,@RequestParam("projectStatus") String projectStatus, BindingResult bindingResult,
            Model uiModel, HttpServletRequest httpServletRequest,RedirectAttributes redirectAttributes) {
*//code goes here*
}

解决方案2: 像下面的代码一样接受pojo对象。

@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@Valid Project project, BindingResult bindingResult,Model uiModel, HttpServletRequest httpServletRequest,RedirectAttributes redirectAttributes) {
*//code goes here*
}

注意:创建一个pojo类,其中包含这些字段以引用解决方案2.