【问题标题】:Returning null value when i am trying to delete in spring mvc当我试图在spring mvc中删除时返回空值
【发布时间】:2017-11-24 07:18:17
【问题描述】:

Controller.java

@Controller
public class DashboardController {

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String deleteRow(@RequestParam("vehicleNo") int vehicleNo) {
        userService.deleteRow(vehicleNo);
        System.out.println("delete... " + vReg.getVehicleNo());
        return ("/dashboard");
    }

}

DaoImpl

 public void deleteRow(Integer vehicleNo) {
    String Sql = null;
    Sql = "delete from vehiclereg where vehicleNo= " + vehicleNo;
    jdbcTemplate.update(Sql, vehicleNo);
    System.out.println("Deleted Record with vehicleNo = " + vehicleNo);
}

【问题讨论】:

  • 你的问题是?
  • 首先,您最好使用RequestMethod.DELETE 进行删除 操作。在 DAO 中也是一个错误代码 - 您将参数连接到 SQL,然后使用此参数调用 jdbcTemplate.update,但它已经在 Sql 字符串中。尝试不带参数调用jdbcTemplate.update(Sql)。或者你可以将它传递给 jdbcTempalte 但在这种情况下不要将它们连接到 Sql Sql ="delete from vehiclereg where vehicleNo= ?"; jdbcTemplate.update(Sql, vehicleNo);

标签: java spring spring-mvc


【解决方案1】:

我认为不需要向这个函数传递 2 个参数。试试这个...

jdbcTemplate.update(Sql);

【讨论】:

    【解决方案2】:

    返回相同的值,

    JSP 主页

    <table  id="myTable"  border="1" cellspacing="10" >
                            <thead align="center" >
                                <tr>
                                    <th data-priority="6">ID</th>
                                    <th data-priority="5">Owner Name</th>
                                    <th data-priority="4">Vehicle No</th>
                                    <th data-priority="3">License No.</th>
                                    <th data-priority="2">Address</th>
                                    <th data-priority="1">Pollution Date </th>
                                    <th data-priority="6">Action</th>
    
                                </tr>
                            </thead>
                            <c:forEach var="vreg" items="${vRegList}" varStatus="loop">
    
                            <tbody>
    
                                <tr>
                                    <th scope="row">${loop.index+ 1}</th>
                                    <td>${vreg.fullname}</td>
                                    <td>${vreg.vehicleNo}</td>
                                    <td>${vreg.licenceNo}</td>
                                    <td>${vreg.address}</td>
                                    <td>${vreg.date3}</td>
                                    <td> <a class="glyphicon glyphicon-pencil" href=edit?vehicleNo=${vreg.vehicleNo}></a> |
    
                                        <a class="glyphicon glyphicon-trash" onclick="return confirm('Are you sure you want to delete?')" href="delete?vehicleNo=${vreg.vehicleNo}"></a>|
                                       <a class="glyphicon glyphicon-envelope"  href="sms"></a>   </td>         
                                </tr>
        </tbody>
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2020-06-30
      • 2018-05-12
      • 1970-01-01
      相关资源
      最近更新 更多