关于struts2在jsp页面如何提交list<自定义对象>?

wolfcrazy 2010-05-07 02:05:44
action 代码片段:
...
private List<CustomObj> list;

set...

get...

...

jsp代码片断:
<s:iterator value="list" status="stat">
<s:textfield name="list[%{#stat.index}].a"/>
<s:textfield name="list[%{#stat.index}].b"/>
</s:iterator>

我现在希望在页面编辑CutomObj对象的字段,搜索了一下,有的说直接这样写就可以;也有的说要加一个转换类,我都试过了,但都不成功,set方法根本不会被调用。希望大侠们指点一下,谢谢~~~
...全文
555 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mr_huolong 2010-05-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 licip 的回复:]
list传到action
我们处理一般是action的list传到页面去的。
[/Quote]

这个方法不错
zml6308 2010-05-07
  • 打赏
  • 举报
回复
在页面把数据构建成json数据,然后在后台转换成list就可以完成了
前台通过循环构建
jobJson = jobJson + "}";
if (jobindex > 1) {
jobJson = jobJson + ",";
for (i = 2; i <= jobindex; i++) {
var Company1 = $("#Company" + i).val();
var Type1 = $("#Type" + i).val();
var Size1 = $("#Size" + i).val();
var Industry1 = $("#Industry" + i).val();
var Department1 = $("#Department" + i).val();
var postName1 = $("#postName" + i).val();
var workDate1 = $("#workDate" + i).val();
var postSalary1 = $("#postSalary" + i).val();
var jobDesc1 = $("#jobDesc" + i).val();
jobJson = jobJson + "{" + "\"company\"" + ":\"" + Company1 + "\",";
jobJson = jobJson + "\"type\"" + ":\"" + Type1 + "\",";
jobJson = jobJson + "\"size\"" + ":\"" + Size1 + "\",";
jobJson = jobJson + "\"industry\"" + ":\"" + Industry1 + "\",";
jobJson = jobJson + "\"department\"" + ":\"" + Department1 + "\",";
jobJson = jobJson + "\"title\"" + ":\"" + postName + "\",";
jobJson = jobJson + "\"time\"" + ":\"" + workDate1 + "\",";
jobJson = jobJson + "\"salary\"" + ":\"" + postSalary1 + "\",";
jobJson = jobJson + "\"jobDesc\"" + ":\"" + jobDesc1 + "\"";
jobJson = jobJson + "}";
if (i < jobindex) {
jobJson = jobJson + ",";
}
}
}
jobJson = jobJson + "]";

后台转换成list
public List getList4Json(String jsonString, Class pojoClass) {

JSONArray jsonArray = JSONArray.fromObject(jsonString);
JSONObject jsonObject;
Object pojoValue;

List list = new ArrayList();
for (int i = 0; i < jsonArray.size(); i++) {

jsonObject = jsonArray.getJSONObject(i);
pojoValue = JSONObject.toBean(jsonObject, pojoClass);
list.add(pojoValue);

}
return list;

}
licip 2010-05-07
  • 打赏
  • 举报
回复
list传到action
我们处理一般是action的list传到页面去的。
wolfcrazy 2010-05-07
  • 打赏
  • 举报
回复
楼上的兄弟,看的我头晕,能看看我的问题出在哪里吗?我现在页面显示都没有问题,就是list传不到action中去。
lovezx1028 2010-05-07
  • 打赏
  • 举报
回复
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>数据显示</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<script type="text/javascript">

function del(id)
{
if(confirm("是否要删除吗?"))
{
//method = "post";
action = "curd!command.do?action=delete&uid="+id;
window.location.href="curd!command.do?action=list";
submit();


}
}

function addPage(btn)
{
var btnvalue=btn.value;
if(btnvalue=="go")
{
var id=document.frm.name.value;
if(id.length>0)
{
document.frm.action="curd!command.do?action=list&startIndex="+(id-1)*5;
document.frm.submit();
}else
{
alert('please input the message');
document.getElementsByName('name').focus();
}
}
else
{

window.location.href="curd!command.do?action=list";
submit();
}

}
</script>



<body>
<form action="">
<table border="0" cellpadding="4" cellspacing="4">

<tr>
<td>ID</td>
<td>Name</td>
<td>pass</td>
<td>add</td>
<td>delete</td>
<td>update</td>
<td>details</td>
</tr>
<s:iterator value="#request.list" var="list" status="s">


<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="password"/></td>
<td><a href="javascript:del(<s:property value="id"/>);"><img src="image/add.gif"/></a></td>
<td><a href="curd!command.do?action=delete&uid=<s:property value="id"/>"><img src="image/del.gif"/></a></td>
<td><a href="curd!command.do?action=update&uid=<s:property value="id"/>"><img src="image/up.gif"/></a></td>
<td><a href="curd!command.do?action=detail&uid=<s:property value="id"/>"><img src="image/detail.gif"/></a></td>
</tr>
</s:iterator>
</table>
<table width="100%" border="0" cellpadding="2" cellspacing="0" bordercolor="#4dddE7">

<tr bgcolor="#FFFFFF">
<td width="17%">${ ps.totalCount }
<a href="curd!command.do?action=list&startIndex=0"><img src="image/shou_r19_c4.jpg"/></a>
<a href="curd!command.do?action=list&startIndex=${ps. previousIndex}"><img src="image/shou_r19_c3.jpg"/></a>
<s:iterator var="i" value="#request.page">
<s:if test="#i==-1">
...
</s:if>

<s:elseif test="#i==#request.ps.currentPage">
<a href="curd!command.do?action=list&startIndex=${(i-1)*5 }" target="mainFrame" style="color:red;" >${i }</a>
</s:elseif>
<s:else>
<a href="curd!command.do?action=list&startIndex=${(i-1)*5 }" target="mainFrame">${i }</a>
</s:else>

</s:iterator>

跳转到第<select name="startIndex">
<option value="-1">please</option>
<s:iterator var="it" value="#request.item" status="st">
<s:iterator value="it" var="d" status="s">
<option value="${d }">the <fmt:formatNumber pattern="#" type="number" value="${(d/5)+1}" maxFractionDigits="0"/> page</option>
</s:iterator>

</s:iterator>
</select>页



<a href="curd!command.do?action=list&startIndex=${ps.nextIndex}"><img src="image/shou_r19_c2.jpg"/></a>

<a href="curd!command.do?action=list&startIndex=${ps.lastPage }"><img src="image/shou_r19_c5.jpg"/></a>
${ ps.pages } / ${ ps.currentPage }

</td>
</tr>




</table>




</form>


<form action="#" name="frm" method="post">
<input id ="name" type="text" value="" name="d"/>
<input type="submit" name="submit" value="go" onclick="addPage(this);">


<table>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<c:forEach items="${list}" var="item" varStatus="s">
<c:if test="${s.index%2==0}"><tr></c:if>
<td>${item.name}</td>
<c:if test="${l%2==1}"></tr></c:if>
</c:forEach>








</table>
</form>
</body>
</html>
wolfcrazy 2010-05-07
  • 打赏
  • 举报
回复
自己顶上去

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧