编辑每行表格的链接会将该行的主键发送给控制器

时间:2012-07-05 07:57:33

标签: java spring jsp spring-mvc

嗨,我是jsp和Spring的新手。我有一份员工信息表。我想在每一行中添加一个编辑链接,然后点击一个编辑链接,它会将该行的主键发送给控制器。当我单击编辑链接时,它显示错误“客户端发送的请求在语法上不正确()。”

此页面显示表格

employeeList.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Employee List</title>
    </head>
    <body>
        <div align="center">
            <h1 align="center">Employee List</h1>
            <table align="center" border="1">
            <tr><th>Pk</th><th>Employee Name</th><th>Employee email</th><th>Address</th><th>Manager Id</th><th>Edit</th><th>Delete</th></tr>
            <c:forEach var="employeeList" items="${list}">
                <tr>
                    <td><c:out value="${employeeList.id}"/></td>
                    <td><c:out value="${employeeList.name}"/> </td>
                    <td><c:out value="${employeeList.email}"/> </td>
                    <td><c:out value="${employeeList.address}"/></td>
                    <td><c:out value="${employeeList.managerId}"/></td>

                    <td><a href="editEmployee.htm?id=${id}" >Edit</a></td>
                    <td><a href="deleteEmployee.htm?id=${id}" >Delete</a></td>
                </tr>
            </c:forEach>
            </table>
        </div>
    </body>
</html>

我的控制器代码是

@RequestMapping(value="/editEmployee.htm")
public String editEmployee(@RequestParam("id") int id,ModelMap model){
    try {
        Employee employee = employeeService.getEmployeeById(id);
        model.addAttribute("employee", employee);
        return "editEmployee";
    } catch (Exception ex) {
        return "index";
    }

}

editEmployee.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Registration Page</title>
        <style>
            .error {
            color: #ff0000;
            font-style: italic;
            }
        </style>
    </head>
    <body>
        Edit Employee<br>
    </body>
</html>

我如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

将您的jsp更改为此类

<c:url var="editUrl" value="/editEmployee.htm" />
<a href="${editUrl}?id=${employeeList.id}">
    Edit
</a>
<c:url var="deleteUrl" value="/deleteEmployee.htm" />
<a href="${deleteUrl}?id=${employeeList.id}">
    Delete
</a

答案 1 :(得分:0)

@RequestMapping(value =“/ editEmployee.htm”) - 错误

@RequestMapping(value="/editEmployee/{id}")
public String editEmployee(@PathVariable int id,ModelMap model){
    try {
        Employee employee = employeeService.getEmployeeById(id);
        model.addAttribute("employee", employee);
        return "editEmployee";
    } catch (Exception ex) {
        return "index";
    }

}

还有一个href =“editEmployee.htm?id = $ {id} 这应该类似于href =“editEmployee / $ {id}