本文介绍了如何在具有Displaytags的Struts2中传递Id值以执行编辑操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在struts2中使用Dispalytags进行了分页.现在,我想在表中添加另外两列,例如"EDIT"和"DELETE".在这里,我该如何传递我的ID值.我做了些什么,但它引发了NumberFormatException.下面是我的代码:

I applied pagination with Dispalytags in struts2. Now I want to add Two more columns to my table like "EDIT" and "DELETE". Here how can I pass the my ID value. I did something but it throws NumberFormatException.Below is my code:

Register.jsp

Register.jsp

<s:form action="addUser">
<s:hidden name="user.id" />
<s:textfield key="user.name" />
<s:password key="user.password" />
------------
------
</s:form>

List.jsp

 <display:table id="id" name="userList" pagesize="5" cellpadding="5px;"
                   cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">
<display:column property="name" title="name"/>
---------
<display:column title="Edit"><s:url id="editURL" action="editUser">
<s:param name="id" value="%{userList.id}"></s:param></s:url>
<s:a href="%{editURL}">Edit</s:a></display:column>
</display:table>

在这里,当我单击编辑链接时,它会引发数字格式异常

Here When I clicked on edit link it throws Number Format Exception

异常在下面

java.lang.NumberFormatException: null
in edit method
id value==null  --->here I am not getting Id value
at java.lang.Long.parseLong(Long.java:404)

下面是编辑操作":

 public String edit() {
    System.out.println("in edit");
    HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
    System.out.println("id=="+request.getParameter("id"));
    user = userDAO.listUserById(Long.parseLong(request.getParameter("id")));
    return SUCCESS;
}

推荐答案

您可以使用两种方式.

您可以将编辑选项放在用户名上.您可以将该用户名作为链接来编辑用户,而将其作为链接删除.

You can put the edit option on the username. That is username you can make as link to edit the user and another column to delete.

<display:table id="id" name="userList" pagesize="5" cellpadding="5px;"
                   cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">
    <display:column property="name"
        href="editUser" media="html" paramId="id"
        paramProperty="id" title="name" />
    <display:column title="Action" value="Delete" href="DeleteUser"
        media="html" paramId="id" paramProperty="id"/>
</display:table>

或者您可以再添加一列进行编辑.

Or you can add one more column extra for edit.

<display:table id="id" name="userList" pagesize="5" cellpadding="5px;"
                   cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">
    <display:column property="name"
         title="name" />
<display:column title="Action" value="Edit" href="EditUser"
            media="html" paramId="id" paramProperty="id"/>
    <display:column title="Action" value="Delete" href="DeleteUser"
        media="html" paramId="id" paramProperty="id"/>
</display:table>

在您的动作课中,创建一个名为 id

In your action class create a Long field called id

private Long id;
//getter and setter

在您的方法中,您可以只传递值.

And in your method you can just pass the values.

编辑方法

user=userDao..listUserById(id);

这篇关于如何在具有Displaytags的Struts2中传递Id值以执行编辑操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:50