the way to get a value from a hidden type correctly

泪湿孤枕 提交于 2019-12-25 01:49:37

问题


in a html table i construct in each row an edit button like the following:

 retour.append("<input type=\"hidden\" id=\"id_"+nomTab+"_"+compteur+"\"  value=\""+object.getIdDailyTimeSheet()+"\"  name=\"hd_"+compteur+"\" />");

this is the hidden type then i do the following:

retour.append("<button  id=edit name=edit type=button  onClick= editarow()>");
retour.append("<img src=edit.gif />");
retour.append("</button>");
retour.append("</td>");

here i am using the hidden type to differentiate between my rows with it. Now I am trying to get the parameter called here: value=\""+object.getIdDailyTimeSheet() in my servlet to do an update query based on the IdDailyTimeSheet. I didn't until know find the way to get this value every time i click the edit button (i do its submit with the javascript).

thanks for help.


回答1:


You can send the id parameter with the GET HTTP method in each row:

<a href="[URL]?id=[id]"><img src="edit.gif"/></a>

where:

  • URL is the URL to which you submit this form to and
  • id is what you build with "id_" + nomTab + "_" + compteur



回答2:


You can get the id from the HttpServletRequest request variable in doGet()/doPost() methods using the getParameter() method. Example: request.getParameter("edit"). "edit" is the name of the input field.

Your html code is not valid. You should quote your attributes. Also you might consider doing the html output in JSP instead of appending strings in a servlet.

Like Bruno said. It might be easyer to create href links width an id request parameter instead of forms with hidden input fields.




回答3:


You're unnecessarily overcomplicating things. First, HTML should not be emitted by a Servlet, but should be embedded as template in a JSP. Second, to achieve what you want, each button must sit in its own <form> element. Here's a kickoff example:

Servlet which loads the table data:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException) {
    List<Item> items = itemDAO.list();
    request.setAttribute("items", items);
    request.getRequestDispatcher("list.jsp").forward(request, response);
}

list.jsp which displays the table data:

<table>
    <c:forEach items="${items}" var="item">
        <tr>
            <td>${item.someProperty}</td>
            <td>
                <form action="servletUrl" method="post">
                    <input type="hidden" name="id" value="${item.id}">
                    <input type="submit" name="edit" value="edit">
                </form>
            </td>
        </tr>
    </c:forEach>
</table>

Servlet which processes the edit:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException) {
    Long id = Long.valueOf(request.getParameter("id"));
    Item item = itemDAO.find(id);
    request.setAttribute("item", item);
    request.getRequestDispatcher("edit.jsp").forward(request, response);
}

No need for Javascript hacks to pass the row ID.



来源:https://stackoverflow.com/questions/2905366/the-way-to-get-a-value-from-a-hidden-type-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!