0

我的jsp中有简单的表格。

<table class="table table-bordered">
    <tr>
        <th>#</th>
        <th>Name</th>
        <th>Surname</th>
        <th>Age</th>
        <th>Email</th>
    </tr>
    <c:forEach var="employee" items="${employeeList}">
        <tr class=row>
            <td>${employee.id}</td>
            <td>${employee.name}</td>
            <td>${employee.surname}</td>
            <td>${employee.age}</td>
            <td>${employee.email}</td>
        </tr>
    </c:forEach>
</table>

我的控制器:

   @Controller
public class BrowseController {

    @Autowired
    private SpringJdbcDao springJdbcDao;

    public void setSpringJdbcDao(SpringJdbcDao springJdbcDao) {
        this.springJdbcDao = springJdbcDao;
    }

    @RequestMapping(value = "/browse")
    public String browseEmployees(Model model) {
        List<Employee> employeeList = new ArrayList<Employee>(springJdbcDao.getEmployeesList());
        model.addAttribute("employeeList", employeeList);
        return "browse";
    }

    @RequestMapping(value = "/browse/details/{id}", method = RequestMethod.GET)
    public String viewDetails(@PathVariable("id") int id, Model model) {
        Employee employee = springJdbcDao.getEmployeeById(id);
        model.addAttribute("data", employee);
        return "details";
    }
}

我需要通过单击表格的行重定向到“/browse/details/{id}”以查看有关员工的详细信息。我怎样才能做到这一点?

4

1 回答 1

0

建议在行中添加一个data-属性,以便更简单地获取员工 ID:

<tr class=row data-emp_id="${employee.id}"></tr>

现在单击行时对服务器进行 AJAX 调用:

var dataType='html';/* this is default and argument for `$.get` not needed for default, is shown for understanding*/
$('tr').click(function(){        
   $.get('/path/to server/', { id: $(this).data('emp_id') }, function( response){
       /* do something with response be it html, xml or JSON response*/
   }, dataType)
});

在服务器接收id任何表单控件元素name="id"并发送回相应的数据

于 2013-01-26T23:05:54.283 回答