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

时间:2018-04-25 09:03:58

标签: java spring-boot thymeleaf

我是springboot的新手,真的需要你的专业知识。请帮帮我。

我需要在单击按钮时将数据传递给控制器​​。现在即时面对下面的错误,我的代码实际上我做错了什么?

'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"

控制器

@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

<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>

模型

@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
    //------

下面的图片是,当用户点击该按钮时,它会发送数据标题&#39;到控制器。 enter image description here

2 个答案:

答案 0 :(得分:0)

您的自定义课程com.portal.dmtt.model.taskSchJob不是String。请考虑在请求正文中传递有效的JSON/XML。这也适用于model对象。

答案 1 :(得分:0)

最后,我做到了。请找到以下答案:

@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";
}

使用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>