0

im new in springboot and really need your expertise. 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

<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. enter image description here

2 Answers2

0

Your custom class com.portal.dmtt.model.taskSchJob isn't String. Please consider passing the valid JSON/XML in request body. This is applied to model object as well.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
  • is there any method without use json or html? i looked an example, where they just use (@Requestparam). – Amir Asyraf Apr 25 '18 at 09:20
  • You need `json` if you wish to send the complete `taskSchJob` object in `request body`. `Query parameters` will work if you wish to send small data. – Shanu Gupta Apr 25 '18 at 09:22
  • Found this answer which will be useful to you. https://stackoverflow.com/questions/25385559/rest-api-best-practices-args-in-query-string-vs-in-request-body – Shanu Gupta Apr 25 '18 at 09:24
  • thank you sir. But my objective is, i just wan the specific data Ex: if user click the button number 1, it will send title data "TEST" to the controller. – Amir Asyraf Apr 25 '18 at 09:25
0

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

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