今天寫的一個form表單提交時總是報錯找不到mapping,form如下:
1 <form action="toUpdate.do" method="post"> 2 id:<input type="text" name="id" value="${list.id }" readonly="readonly"><br> 3 name:<input type="text" name="name" value="${list.name }"><br> 4 sex:男:<input type="radio" name="sex" value="1" ${list.sex==1?"checked='checked'":"" }> 5 女:<input type="radio" name="sex" value="0" ${list.sex==0?"checked='checked'":"" }><br> 6 birthday:<input type="text" name="birthday" value="${list.birthday }"><br> 7 phone:<input type="text" name="phone" value="${list.phone }"><br> 8 <input type="submit" value="ok"> 9 </form>
檢查發現每次提交時地址欄中多出了一部分:
這是由於在跳轉頁面時,由於使用了@PathVariable這個注解,所以在地址中插入了一個id
1 <a href="toAddPage.do">添加</a> 2 <table border="1"> 3 <tr> 4 <td>id</td> 5 <td>name</td> 6 <td>sex</td> 7 <td>birthday</td> 8 <td>phone</td> 9 <td colspan="2">操作</td> 10 </tr> 11 <c:forEach items="${list }" var="l"> 12 <tr> 13 <td>${l.id }</td> 14 <td>${l.name }</td> 15 <td>${l.sex==1?"男":"女" }</td> 16 <td> 17 <f:formatDate value="${l.birthday }" pattern="yyyy年MM月dd日"/> 18 </td> 19 <td>${l.phone }</td> 20 <td><a href="${l.id }/toDelete.do">刪除</a></td> 21 <td><a href="${l.id }/toUpdatePage.do">修改</a></td> //可以看到a標簽的超鏈接地址中多了一個id 22 </tr> 23 </c:forEach> 24 </table>
這就導致在執行頁面跳轉后url里就多了一部分,這時再像上面的form表單那樣填寫action時就會使地址欄信息不匹配,出現500錯誤
此時form表單的action中應該加上項目路徑,如下:
1 <form action="/t0402/toUpdate.do" method="post"> 2 id:<input type="text" name="id" value="${list.id }" readonly="readonly"><br> 3 name:<input type="text" name="name" value="${list.name }"><br> 4 sex:男:<input type="radio" name="sex" value="1" ${list.sex==1?"checked='checked'":"" }> 5 女:<input type="radio" name="sex" value="0" ${list.sex==0?"checked='checked'":"" }><br> 6 birthday:<input type="text" name="birthday" value="${list.birthday }"><br> 7 phone:<input type="text" name="phone" value="${list.phone }"><br> 8 <input type="submit" value="ok"> 9 </form>
這樣就不會再報錯了