Java Struts2使用对象的arraylist修改对象
我的Action类中有一个对象,它内部包含一个对象的arraylist,我正在尝试为这个对象创建一个CRUD屏幕。下面给出了我的动作类和beanJava Struts2使用对象的arraylist修改对象,java,jakarta-ee,struts2,Java,Jakarta Ee,Struts2,我的Action类中有一个对象,它内部包含一个对象的arraylist,我正在尝试为这个对象创建一个CRUD屏幕。下面给出了我的动作类和bean /** * @author rohit * */ public class IRFeedMgmtAction extends ActionSupport implements ModelDriven<IRFeeds>,SessionAware,ServletRequestAware { private static org.apac
/**
* @author rohit
*
*/
public class IRFeedMgmtAction extends ActionSupport implements ModelDriven<IRFeeds>,SessionAware,ServletRequestAware {
private static org.apache.log4j.Logger log = Logger.getLogger(IRFeedMgmtAction.class);
private HttpServletRequest request;
private Map session;
private IRAccountsDAO acctsDAO;
private IRFeeds feed = new IRFeeds();
/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
public String execute()
{
return "success";
}
/**
* @return
*/
public String add()
{
IRUser user = (IRUser) session.get("user");
List<IRAccountUsers> twtUsers = acctsDAO.getTwitterAcctByOrgId(user.getOrgId());
feed.setTwtAccts(prepareTwitterAccounts(twtUsers));
return "addFeed";
}
/**
* @return
*/
public String save()
{
IRFeeds fd = getFeed();
ArrayList<IRFeedAccts> twtAccts = fd.getTwtAccts();
System.err.println(fd.getFeedUrl());
for (Iterator iterator = twtAccts.iterator(); iterator.hasNext();)
{
IRFeedAccts irFeedAccts = (IRFeedAccts) iterator.next();
System.err.println(irFeedAccts.getNumber());
}
return "saved";
}
/**
* @return
*
*/
private ArrayList<IRFeedAccts> prepareTwitterAccounts(List<IRAccountUsers> twtUsers)
{
ArrayList<IRFeedAccts> twtAccts = new ArrayList<IRFeedAccts>();
IRAccountUsers twtUser = null;
IRFeedAccts feedAccnt = null;
for (Iterator iterator = twtUsers.iterator(); iterator.hasNext();)
{
twtUser = (IRAccountUsers) iterator.next();
feedAccnt = new IRFeedAccts();
feedAccnt.setAccountId(twtUser.getSocialId());
feedAccnt.setPic(twtUser.getPic());
feedAccnt.setName(twtUser.getTwtUsrName());
feedAccnt.setNumber(30);
twtAccts.add(feedAccnt);
}
return twtAccts;
}
/**
*@作者rohit
*
*/
公共类IRFeedMgmtAction扩展了ActionSupport实现了模型驱动、SessionAware和ServletRequestAware{
私有静态org.apache.log4j.Logger log=Logger.getLogger(IRFeedMgmtAction.class);
私有HttpServletRequest;
私人Map会议;
私人伊拉克账户dao acctsDAO;
私有irfeed=新irfeed();
/*(非Javadoc)
*@see com.opensymphony.xwork2.ActionSupport#execute()
*/
公共字符串execute()
{
返回“成功”;
}
/**
*@返回
*/
公共字符串add()
{
IRUser=(IRUser)session.get(“用户”);
List twtUsers=acctsDAO.getTwitterAcctByOrgId(user.getOrgId());
feed.setTwtAccts(prepareTwitterAccounts(twtUsers));
返回“addFeed”;
}
/**
*@返回
*/
公共字符串保存()
{
IRFeeds fd=getFeed();
ArrayList twtAccts=fd.getTwtAccts();
System.err.println(fd.getFeedUrl());
for(Iterator Iterator=twtAccts.Iterator();Iterator.hasNext();)
{
IRFeedAccts IRFeedAccts=(IRFeedAccts)迭代器;
System.err.println(irFeedAccts.getNumber());
}
返回“已保存”;
}
/**
*@返回
*
*/
专用阵列列表prepareTwitterAccounts(列出TWTUSER)
{
ArrayList twtAccts=新的ArrayList();
IRAccountUsers twtUser=null;
IRFeedAccts feedAccnt=null;
for(Iterator Iterator=twtUsers.Iterator();Iterator.hasNext();)
{
twtUser=(IRAccountUsers)迭代器.next();
feedAccnt=新的IRFeedAccts();
feedAccnt.setAccountId(twtUser.getSocialId());
feedAccnt.setPic(twtUser.getPic());
feedAccnt.setName(twtUser.getTwtUsrName());
饲料箱设置编号(30);
twtAccts.add(feedAccnt);
}
返回TWTACTS;
}
我的豆子
public class IRFeeds implements java.io.Serializable {
private Integer feedId;
private Integer campId;
private String feedUrl;
private boolean active;
private Date createdOn;
private Date updatedOn;
private String createdBy;
private ArrayList<IRFeedAccts> twtAccts;
private ArrayList<IRFeedAccts> fbAccts;
private ArrayList<IRFeedAccts> fbPages;
公共类irfeed实现java.io.Serializable{
私有整数feedId;
私有整数campId;
私有字符串feedUrl;
私有布尔活动;
私人约会;
私人日期更新;
创建的私有字符串;
私人阵列列表;
私人ArrayList fbAccts;
私有数组列表;
我的JSP文件
<s:iterator value="#session.fd.twtAccts" status="twtAcct">
<tr>
<td>
<div style="width: 48px; float: left;"><img src="<s:property value="pic" />" /></div>
<div style="text-align: left;"><s:property value="name" /></div>
</td>
<td>
<s:textfield name="number"/>
</td>
<td>
<input type="text" />
</td>
<td>
<s:textfield name="signature"/>
</td>
</tr>
</s:iterator>
" />
现在我的问题是,当arraylist中的bean的值在JSP中被修改时,动作类save方法不会得到相同的值
问候,,
Rohit解决这个问题
<s:iterator id="twtFeedAccts" value="twtFeedAccts" status="twtAcct">
<tr>
<td width="250">
<img src="<s:property value="%{twtFeedAccts[#twtAcct.index].pic}" />" width="25px" height="25px" />
<s:property value="%{twtFeedAccts[#twtAcct.index].name}" />
</td>
<td width="200">
<s:textfield id="twtFeedAccts[%{#twtAcct.index}].number" name="twtFeedAccts[%{#twtAcct.index}].number" value="%{twtFeedAccts[#twtAcct.index].number}" />
</td>
<td width="200">
<s:select id="twtFeedAccts[%{#twtAcct.index}].cycle" name="twtFeedAccts[%{#twtAcct.index}].cycle" value="%{twtFeedAccts[#twtAcct.index].cycle}"
label="Select a month" list="#{'2':'2 hrs','4':'4 hrs', '6':'6 hrs', '12':'12 hrs', '24':'24 hrs'}" />
</td>
<td width="250">
<s:textfield id="twtFeedAccts[%{#twtAcct.index}].signature" name="twtFeedAccts[%{#twtAcct.index}].signature" value="%{twtFeedAccts[#twtAcct.index].signature}" size="40"/>
</td>
<td width="50">
<s:checkbox id="twtFeedAccts[%{#twtAcct.index}].selected" name="twtFeedAccts[%{#twtAcct.index}].selected" value="%{twtFeedAccts[#twtAcct.index].selected}" />
</td>
</tr>
</s:iterator>
“width=“25px”height=“25px”/>
当您提交表单时,bean将被填充
问候,,
Rohit是一个工作示例(Netbeans 6.9项目),说明了如何迭代数组或对象列表
另外,如何提交表单,以便在提交时重新创建对象列表
只需解决引用问题,就可以开始了。我已经解决了这个问题: 我的JSP:
<s:iterator value="feedbackFor" var="feedbackFor" status="stat">
<tr>
<td><s:label> <s:property value="feedbackFor" /></s:label> </td>
<td><s:label><s:property value="empName"/></s:label></td>
<s:hidden name="feedbackFor[%{#stat.index}].feedbackBy" value="%{feedbackBy}"></s:hidden>
<s:hidden name="feedbackFor[%{#stat.index}].feedbackFor" value="%{feedbackFor}"></s:hidden>
<!--<s:hidden name="feedbackFor.[%{#stat.index}].positiveFeedback" value="%{positiveFeedback}"></s:hidden>-->
<td><s:textfield name="feedbackFor[%{#stat.index}].positiveFeedback" value="%{positiveFeedback}" size="100"/></td>
<td><s:textfield name="feedbackFor[%{#stat.index}].negetiveFeedback" value="%{negetiveFeedback}" size="100"/></td>
</tr>
</s:iterator>
</table>
<s:submit value="Submit Feedback"/>
</s:form>
我的行动
public class FeedbackAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<FeedbackDTO> feedbackFor;
private String employeeId;
public List<FeedbackDTO> getFeedbackFor() {
return feedbackFor;
}
public void setFeedbackFor(List<FeedbackDTO> feedbackFor) {
this.feedbackFor = feedbackFor;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployees(){
FeedbackHelper feedbackHelper = new FeedbackHelper();
feedbackFor=feedbackHelper.getEmployeeList(employeeId);
System.out.println("The feed back populated is "+ feedbackFor + "Size is "+ feedbackFor.size());
return SUCCESS;
}
public String submitFeedback(){
System.out.println("The feed back repopulated is "+ feedbackFor + "Size is "+ feedbackFor.size());
return SUCCESS;
}
}
公共类反馈操作扩展了ActionSupport{
私有静态最终长serialVersionUID=1L;
私有列表反馈器;
私有字符串employeeId;
公共列表getFeedbackFor(){
反馈回路;
}
公共无效设置反馈(列表反馈){
这一点。反馈=反馈;
}
公共字符串getEmployeeId(){
返回员工ID;
}
public void setEmployeeId(字符串employeeId){
this.employeeId=employeeId;
}
公共字符串getEmployees(){
FeedbackHelper FeedbackHelper=新的FeedbackHelper();
feedbackFor=feedbackHelper.getEmployeeList(employeeId);
System.out.println(“填充的反馈为“+feedbackFor+”大小为“+feedbackFor.Size());
回归成功;
}
公共字符串submitFeedback(){
System.out.println(“重新填充的反馈为“+feedbackFor+”大小为“+feedbackFor.Size());
回归成功;
}
}
如果bean本身有一个列表,那么只能遍历该列表,但是在表单提交时将该列表保存在bean中会怎么样?