我的练习项目有问题。也许我是瞎子,但我无法解决这个谜语。希望这不仅仅是另一个副本。
<a href>
的servlet。
<c:forEach var="object" items="${dealer.objectList}" varStatus="status">
<div class="changableElement">
<jsp:include page="/Object/objectDetails.jsp"></jsp:include>
<a href="/MyProjekt/ObjectResolverServlet/${object.id}" id="send">Change</a>
</div>
</c:forEach>
这很好用。
ObjectResolverServlet
从后台再次获取对象,并将其作为属性放入请求中。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Mark #1");
String objectId = request.getPathInfo().substring(1);
MyObject obj = dbHandler.getMyObjectById(objectId );
System.out.println(obj);
request.setAttribute("objectDetails", obj);
response.setContentType("text/html");
getServletContext().getRequestDispatcher("/MyObjectChangePage").forward(request, response) ;
}
在MyObjectChangePage
上,用户可以操纵obj
的详细信息。
MyObjectChangePage
主要由表单,文本字段和提交按钮组成,这些按钮指向另一个servlet。
<form id="object_change_values_form" action="ObjectChangeServlet" method="post" >
<div>
<label for="name">Name:</label><br>
<input id="name" name="name" type="text" value="${obj.name}" />
<span id="nameDetails"bla bla ..</span>
</div>
<div>
<label for="origin"Origin:</label><br>
<input id="origin" name="origin" type="text" value="${obj.origin}" />
<span id="originDetails">bla bla ../span>
</div>
<div>
<label for="year">Bitte Jahrgang eingeben!</label><br>
<input id="year" name="year" type="text" value="${obj.year}" />
<span id="yearDetails">bla bla ..</span>
</div>
<div>
<label for="description">Description</label><br>
<textarea name='description' id='description'>${obj.description}</textarea>
<span id="descriptionDetails">bla bla ..</span>
</div>
<div>
<label for="priceSelling">Price</label><br>
<input id="priceSelling" name="priceSelling" type="text" value="${obj.priceSelling}" />
<span id="priceSellingDetails">bla bla ..</span>
</div>
<input id="send" type="submit" value="Change" name = "button"/>
<input id="send" type="submit" value="Cancel" name = "button"/>
</form>
ObjectChangeServlet
- 而是打印来自ObjectResolverServlet
和新行null
的输出标记#1。
我发现从'ObjectResolverServlet'调用doPost
方法。猜猜我犯了一个错误的错误。
我web.xml
的部分内容:
....
<servlet>
<servlet-name>ObjectChangeServlet</servlet-name>
<servlet-class>impl.servlets.ObjectChangeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ObjectChangeServlet</servlet-name>
<url-pattern>/ObjectChangeServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ObjectChangePage</servlet-name>
<jsp-file>/Object/object_change_values.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>ObjectChangePage</servlet-name>
<url-pattern>/ObjectChangePage</url-pattern>
</servlet-mapping>
<servlet>
<description>Gets the object by id</description>
<servlet-name>ObjectResolverServlet</servlet-name>
<servlet-class>impl.servlets.ObjectResolverServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ObjectResolverServlet</servlet-name>
<url-pattern>/ObjectResolverServlet/*</url-pattern>
</servlet-mapping>
....
答案 0 :(得分:1)
通过上述评论中的良好线索找到了解决方案。简单来说,<form .. >
标记中的操作必须以绝对方式指向处理servlet,所以我在3.中的第一次尝试是绝对错误的
<强>错强>
<form id="object_change_values_form" action="ObjectChangeServlet" method="post" >
但正确解决方案为<form id="object_change_values_form" action="/MyProjekt/ObjectChangeServlet" method="post" >