简体   繁体   English

SpringBoot:将按钮值解析为控制器

[英]SpringBoot: parse button value to controller

im new in springboot and really need your expertise. 我是springboot的新手,真的需要您的专业知识。 Please help me. 请帮我。

i need to pass the data to controller when the button is click. 单击按钮时,我需要将数据传递给控制器​​。 Now im facing with the error below, what actually i do wrong in my code ? 现在我面对下面的错误,我实际上在我的代码中做错了什么?

'java.lang.String' to required type 'com.portal.dmtt.model.taskSchJob'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'TEST'; nested exception is java.lang.NumberFormatException: For input string: "TEST"

Controller 控制者

@RequestMapping(value="/runJob", method=RequestMethod.GET)
public String runJob(Model model, taskSchJob schJob) {
     System.out.println("Start get request");
     model.addAttribute("theTaskSchJobList", new taskSchJob());
     schJob.getScript();
     System.out.println("End get request");
     return "redirect:/cronJob";
}

@RequestMapping(value="/runJob", method=RequestMethod.POST)
public String customerSubmit(@ModelAttribute taskSchJob schJob, Model model,@RequestParam String taskJobScript) {
     System.out.println("Start post request");
     System.out.println("End post request" + taskJobScript.toString());
     return "redirect:/cronJob";
}

HTML 的HTML

<tbody>
    <tr th:each="taskJob : ${theTaskSchJobList}" style="text-align: center">
        <form th:action="@{/runJob}" th:object="${theTaskSchJobList}" th:method="POST">
            <td th:text="${taskJob.id}">Id</td>
            <td th:text="${taskJob.title}">Username</td>
            <td th:text="${taskJob.script}">Script</td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td>
                <button type="submit" class="btn btn-success" th:value="${taskJob.script}" th:name="taskSchJob">
                    <span class="glyphicon glyphicon-play"></span>
                </button>
        </form>
        </td>
    </tr>
</tbody>

Model 模型

@Entity
@Table (name = "task_Sch_Job")
public class taskSchJob {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "username")
    private String username;

    @Column(name = "script")
    private String script;

    @Column(name = "date_create")
    private String date_create;

    @Column(name = "cron_job")
    private String cron_job;

    @Column(name = "status")
    private String status;

    //------
    //setter and getter
    //------

the below image is, when the user click the button, it will send the data 'title' to the controller. 下图是,当用户单击按钮时,它将把数据“标题”发送到控制器。 在此处输入图片说明

Your custom class com.portal.dmtt.model.taskSchJob isn't String . 您的自定义类com.portal.dmtt.model.taskSchJob不是String Please consider passing the valid JSON/XML in request body. 请考虑在请求正文中传递有效的JSON/XML This is applied to model object as well. 这也适用于model对象。

finally, i did it.Please find the below answer: 最后,我做到了。请找到以下答案:

@RequestMapping(value = "/taskRun/{id}", method = RequestMethod.GET, params = "actionEdit")
public String taskEdit(Model model, @PathVariable(required = true, name = "id") Long id) {

    System.out.println("Start GET request: the id is: " + id);


    return "redirect:/cronJob";
}

HTML text by using GET Method 使用GET方法的HTML文本

<form th:action="@{/taskRun/__${taskJob.id}__}">
    <button type="submit" class="btn btn-success" th:name="actionRun" th:value="run">
    <span class="glyphicon glyphicon-play"></span>
</button></form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM