当前位置: 首页 > 知识库问答 >
问题:

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

柏高丽
2023-03-14

我是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
    //------

共有2个答案

殳飞扬
2023-03-14

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

@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>
楚羽
2023-03-14

您自定义的类com.portal.dmtt.model.taskSchJobs不是String。请考虑在请求正文中传递有效的JSON/XML。这也应用于模型对象

 类似资料:
  • 我正试图从这个js发布我的数据 有什么建议如何修复它吗? 堆栈跟踪:

  • 我有我的UIController类和一个按钮方法类。按钮方法类包含按钮操作的所有代码。我需要能够使用按钮方法类中控制器类中的按钮。例如,我在控制器类中定义了这些 例如,单击按钮时,按钮锁定有一个动作事件 理想情况下,我希望按钮方法可以做到这一点: 出于各种原因,我不能将代码放在动作事件中,而将按钮对象放在参数中会让我的操作变得太混乱。那么如何将FXML对象放入button类中呢?

  • 我有使用Springboot和Thymeleaf模板编写的简单Web应用程序。报告控制器从表单接收数据并构建TestPlanReportReportACK对象,该对象作为模型属性添加如下: 我可以在“图表”thymeleaf模板中使用该数据并显示我需要的数据,但我需要在单击按钮时将完全相同的对象发送回控制器,但我得到TestPlanReportACK对象作为设置空值的参数。 以下是我的按钮在图表模

  • 问题内容: 以下是我的html模板: 下面是我的代码: 为什么我会收到“ 未定义 ”而不是“ 某些消息 ” 下面是一个小提琴 http://jsfiddle.net/j2K7N/27/ 问题答案: 您的代码几乎是正确的,但是这里有几个问题: 在这里,您从控制器传递函数,该函数带有一个未定义的参数,该参数会导致警报消息带有“未定义”文本。我建议将HTML代码修改为: 请注意,我将传递为变量而不是函数

  • 点击无结果。 以下是我的xpage的代码重复控制: 如何在重复控制中运行此CSJS按钮代码?

  • 使用鼠标库,您可以使用Arduino Leonardo,Micro或Due控制计算机的屏幕光标。 此特定示例使用五个按钮移动屏幕上的光标。 其中四个按钮是方向性的(向上,向下,向左,向右),一个用于鼠标左键单击。 Arduino的光标移动始终是相对的。 每次读取输入时,光标的位置都会相对于其当前位置进行更新。 每当按下一个方向按钮时,Arduino将移动鼠标,将HIGH输入映射到适当方向的5的范围