我在struts2中使用Dispalytags应用了分页。现在我想在我的表中添加两列,例如“编辑”和“删除”。这里我如何传递我的ID值。我做了一些事情,但它抛出了 NumberFormatException。 下面是我的代码:
注册.jsp
<s:form action="addUser">
<s:hidden name="user.id" />
<s:textfield key="user.name" />
<s:password key="user.password" />
------------
------
</s:form>
列表.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>
这里,当我单击编辑链接时,它抛出数字格式异常
异常如下
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;
}
最佳答案
您可以使用两种方式。
您可以将编辑选项放在用户名上。这是用户名,您可以将其作为链接来编辑用户和删除另一列。
<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>
或者您可以再添加一列进行编辑。
<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 的 Long 字段
private Long id;
//getter and setter
在您的方法中,您可以只传递值。
编辑方法
user=userDao..listUserById(id);
关于java - 如何在Struts2中使用Displaytags传递Id值来执行编辑操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20812145/