0

我的练习项目有问题。也许我是个盲人,但我解不开这个谜。希望这不仅仅是另一个重复。

  • 1.jsp里面有一个list,可以抽象的呈现对象。从那里我将每个链接到一个带有<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>
    

    这工作正常。

  • 2.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

  • 3.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>
    

  • 4.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>
      ....
    

  • 4

    1 回答 1

    1

    由于上述评论中的良好线索找到了解决方案。只是<form .. >tag中的action必须以绝对的方式指向处理servlet,所以我在3.中的第一次尝试是绝对错误的

    错误的 <form id="object_change_values_form" action="ObjectChangeServlet" method="post" >

    正确的解决方案是
    <form id="object_change_values_form" action="/MyProjekt/ObjectChangeServlet" method="post" >

    于 2012-05-14T13:42:54.507 回答