gpt4 book ai didi

java - struts2如何从jsp读取值迭代器到action

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:26 24 4
gpt4 key购买 nike

我是java和struts2的新手,请帮忙。从一个操作中,我已读取并处理在 jsp 中创建的列表

我的问题是,如何从迭代器的操作中访问数据?

JSP页面

<s:iterator value="settoriList" var="myObj">
<tr>
<td>
<s:if test="#myObj.selected == 1">
<s:checkbox name="selected" fieldValue="%{cdSettore}" checked="checked"/>
<s:property value="cdSettore" />
</s:if>
<s:if test="#myObj.selected == 0">
<s:checkbox name="selected" value="false" fieldValue="%{cdSettore}" />
<s:property value="cdSettore" />
</s:if>
</td>
<td><s:property value="dsSettore" /></td>
<td><s:property value="dtIniVali" /></td>
<td><s:property value="dtFineVali" /></td>
</tr>
</s:iterator>
</table>
<s:hidden name="cdConve" />
<s:hidden name="process" value="saveSettori"/>
</s:form>

这是我尝试访问数据的方式

private List<GenSettori> settoriList;

public List<GenSettori> getSettoriList() {
return settoriList;
}
public void setSettoriList(List<GenSettori> settoriList) {
this.settoriList = settoriList;
}

private String[] selected;

public String[] getSelected() {
return selected;
}
public void setSelected(String[] selected) {
this.selected = selected;
}

--------
settoriList = new ArrayList<GenSettori>();
System.out.println("list sett: "+settoriList);
for(int i=0;i<getSettoriList().size();i++){
if (getSelected()[i]!=""){
System.out.println("not selected "+getSelected()[i]);
}
}

---------

我的页面由选定的字段、描述和日期组成。

提前感谢您的帮助

最佳答案

您可以通过以下几行从jsp获取字符串数组到操作类

在jsp中

<s:hidden name="cdSettore" value="%{cdSettore}"></s:hidden>

如果您愿意,可以使用

<s:if test="#myObj.selected == 0">
<s:hidden name="cdSettore" value="%{cdSettore}"></s:hidden>
</s:if>

一次都没有被选中

实际操作类

private String[] cdSettore;

public void setCdSettore(String[] cdSettore) {
this.cdSettore = cdSettore;
}
public String[] getCdSettore() {
return cdSettore;
}

for(int i=0;i<getCdSettore().length;i++){
System.out.println("not selected :"+getCdSettore()[i]);
}

或者你可以这样做

在jsp中

<s:hidden name="cdSettore" value="%{cdSettore}"></s:hidden>
<s:if test="#myObj.selected == 1">
<s:checkbox name="selected" fieldValue="%{cdSettore}" checked="checked"/>
<s:property value="cdSettore" />
</s:if>
<s:if test="#myObj.selected == 0">
<s:checkbox name="selected" fieldValue="%{cdSettore}" />
<s:property value="cdSettore" />
</s:if>

在行动类

  for(int i=0;i<getCdSettore().length;i++){
int flag=0;
for(int j=0;j<getSelected().length;j++){
if(getCdSettore()[i].equalsIgnoreCase(getSelected()[j]))
{
flag++;
}
}
if(flag==0)
{
System.out.println("not selected :"+getCdSettore()[i]);
}
}

关于java - struts2如何从jsp读取值迭代器到action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com