【问题标题】:Retrieve value from html and print to controller从 html 中检索值并打印到控制器
【发布时间】:2018-10-30 16:57:59
【问题描述】:

从上面的标题,有没有办法从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>

【问题讨论】:

    标签: java spring-mvc spring-boot thymeleaf


    【解决方案1】:

    解决方案 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。

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2016-07-20
      • 1970-01-01
      相关资源
      最近更新 更多