我的练习项目有问题。也许我是个盲人,但我解不开这个谜。希望这不仅仅是另一个重复。
<a href>
.
<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
新行中的输出 Mark #1。null
我发现从“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>
....