请求将在春季错误处理:

时间:2016-10-05 09:11:31

标签: java spring jsp spring-mvc tomcat

我正在测试春天的一些功能。我的目标是从表格中编辑记录。以下是用于显示现有记录的jsp代码。

<table border="1">
    <tr>
        <td>ID</td>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Telephone</td>
        <td>Email</td>
        <td></td>
        <td></td>
    </tr>
    <c:forEach var="emp" items="${employees}">
        <tr>
            <td>${emp.id}</td>
            <td>${emp.firstName}</td>
            <td>${emp.lastName}</td>
            <td>${emp.telephone}</td>
            <td>${emp.email}</td>
            <td><a href="<c:url value="getEmpForUpdate/${emp.id}" />">Update</a></td>
            <td><a>Delete</a></td>
        </tr>
    </c:forEach>
</table>

我想我在上面的代码中使用的hiper链接出了问题。

<a href="<c:url value="getEmpForUpdate/${emp.id}" />">Update</a>

当我点击记录时,URL将如下所示。

http://localhost:9876/SpringAnnotationDemo_Tomcat/getEmpForUpdate/4

显示编辑记录的表格。下面是我用于编辑记录的代码。

<form:form method="POST" action="/updateEmployee">
    <table>

        <tr>
            <td><form:label path="firstName">F Name</form:label></td>
            <td><form:input path="firstName" /></td>
        </tr>
        <tr>
            <td><form:label path="lastName">L Name</form:label></td>
            <td><form:input path="lastName" /></td>
        </tr>
        <tr>
            <td><form:label path="telephone">Phone</form:label></td>
            <td><form:input path="telephone" /></td>
        </tr>
        <tr>
            <td><form:label path="email">E-Mail</form:label></td>
            <td><form:input path="email" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Submit" /></td>
        </tr>
    </table>
</form:form>

在编辑上面的详细信息后,当我点击输入时,URL应如下所示 http://localhost:9876/SpringAnnotationDemo_Tomcat/updateEmployee

但是就像下面一样 http://localhost:9876/SpringAnnotationDemo_Tomcat/getEmpForUpdate/updateEmployee

My controller method is like below
    @RequestMapping(value="/updateEmployee",method = RequestMethod.POST)
    public ModelAndView updateEmployeeUsingObject(@ModelAttribute Employee employee){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(employee.getId());
        return modelAndView;
    }
你能告诉我出错的地方吗?为什么网址不符合预期。

1 个答案:

答案 0 :(得分:2)

导航后,您的网页位于网址http://localhost:9876/SpringAnnotationDemo_Tomcat/getEmpForUpdate,因此您需要更新/更改您的操作"../updateEmployee",因为它是相对网址

或更好的你给网址

<form:form action="${pageContext.request.contextPath}/updateEmployee" method="post">