Spring: get input button id

时间:2018-02-05 12:50:27

标签: spring thymeleaf

I have this html template in thymeleaf.

<table id="listAllAccountantTable" th:cellspacing="0" class="table table-striped table-bordered" style="width:100%;">
  <thead>
  <tr>
     <th>No. </th>
     <th>Registered Date</th>
     <th>Status</th>
     <th>Name</th>
     <th>Email</th>
     <th>Contact No.</th>
     <th>IC No.</th>
     <th>IC attachment</th>
     <th>Actions </th>
  </tr>

  <tr th:each="acc,iterationStatus  : ${accountantListing}">
     <td th:text="${iterationStatus.count}">1</td>
     <td th:text="${acc.currentTimeStamp}"></td>
     <td th:text="${acc.active}">1</td>
     <td th:text="${acc.name}">Name</td>
     <td th:text="${acc.email}">Email</td>
     <td th:text="${acc.phoneNumber}">Contact No.</td>
     <td th:text="${acc.icNumber}">IC No.</td>
     <td th:text="${acc.id}"> To be fixed: upload IC image</td>

     <td>
        <form action="#" data-th-action="@{/accountantApplication}" method="post">                                                   
           <button type="submit" name="action" th:id="${acc.id}" 
              value="Accept">Accept</button>
           <button type="submit" name="action" th:id="${acc.id}" 
              value="Reject">Reject</button>
        </form>
     </td>
  </tr>

My Spring controller is:

@RequestMapping(value="/accountantApplication", method=RequestMethod.POST, params="action=Accept")
public ModelAndView Accept() {
    ModelAndView modelAndView = new ModelAndView();
    System.out.println("######## Accepting accountant");
    modelAndView.setViewName("AccountantListing");
    return modelAndView;
}


@RequestMapping(value="/accountantApplication", method=RequestMethod.POST, params="action=Reject")
public ModelAndView Reject() {
    ModelAndView modelAndView = new ModelAndView();
    System.out.println("######## Rejecting accountant");
    modelAndView.setViewName("AccountantListing");
    return modelAndView;
}

The table shows a list of accountants. All accountants are loaded from db and displayed on the table. They need to be accepted or rejected. When I click the accept button, Accept() is called. How do I get the ID attached to button?

Or if there is better way of immplementing this. let me know too. Thanks so much

1 个答案:

答案 0 :(得分:0)

在您的表单中,您应该有一个隐藏的输入:

<form action="#" data-th-action="@{/accountantApplication}" method="post">                                                   
    <input type="hidden" name="id" th:value="${acc.id}" />
    <button type="submit" name="action" value="Accept">Accept</button>
    <button type="submit" name="action" value="Reject">Reject</button>
</form>

然后,在您的控制器中:

public ModelAndView accept(@RequestParam String id) {
    .
    .
    .
}

public ModelAndView Reject(@RequestParam String id) {
    .
    .
    .
}

另外,作为旁注,您可以替换:

@RequestMapping(value="/accountantApplication", method=RequestMethod.POST, params="action=Accept")
@RequestMapping(value="/accountantApplication", method=RequestMethod.POST, params="action=Reject")

@PostMapping(value="/accountantApplication", params="action=Accept")
@PostMapping(value="/accountantApplication", params="action=Reject")