How to pass Id value to perform Edit operation in

2019-08-06 11:50发布

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

<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

Exception is below

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)

Below is Edit Action:

 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;
}

1条回答
做个烂人
2楼-- · 2019-08-06 12:08

You can use two way.

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>

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.

edit method

user=userDao..listUserById(id);
查看更多
登录 后发表回答