我的jsp中有Struts2 select标签:
<s:iterator value="data.members" status="rowstatus">
<s:select id="Ct%{#rowstatus.index}" name="data.members[%{#rowstatus.index}].extData.carrierKey" headerKey="-1" headerValue="" list="carrier"
onchange="javascript:autoTab(event,this,%{#rowstatus.index},false);">
</s:select>
</s:iterator
现在这个carrier
是后端的Map,它将填充在这个select标签中。载体填充如下:
carrier = new HashMap<Integer, String>();
carrier.put(1,"LIC");
carrier.put(2, "Bajaj");
现在假设显示jsp。我在select标签中选择LIC并提交表格。假设发生了一些验证错误。我调试了应用程序,发现动作类中变量"data.members[%{#rowstatus.index}].extData.carrierKey"
的值为1.但是它的值没有填充在jsp上。
我不明白,如果data.members[%{#rowstatus.index}].extData.carrierKey
设置为值,为什么值不会填充。据我说,如果它的值是1,那么select标签必须显示LIC。
答案 0 :(得分:0)
我试用你的代码并且对我很好。
JSP:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head><title>Stack Overflow</title></head>
<body>
<h1>Stack Overflow</h1>
<s:form action="stackoverflow" method="POST">
<s:iterator value="data.members" status="rowstatus">
<s:select id="Ct%{#rowstatus.index}"
name="data.members[%{#rowstatus.index}].extData.carrierKey"
headerKey="-1"
headerValue=""
list="carrier"
onchange="javascript:autoTab(event,this,%{#rowstatus.index},false);"
>
</s:select>
</s:iterator>
<br/>
<s:submit value="Submit!" method="save" />
</s:form>
<script type="text/javascript">
function autoTab(ev, current,nextElementIndex, varBool){
document.getElementById("stackoverflow").elements[nextElementIndex].focus();
};
</script>
</body>
</html>
动作:
package com.tutorialspoint.struts2.actions;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.tutorialspoint.struts2.entities.ObjectData;
import com.tutorialspoint.struts2.entities.ObjectMember;
public class StackOverflowAction extends ActionSupport {
private static final long serialVersionUID = 9053634696133432054L;
private Map<Integer, String> carrier;
private ObjectData data;
public String execute() throws Exception {
return MyAction.SUCCESS;
}
public String save() throws Exception {
return MyAction.SUCCESS;
}
public Map<Integer, String> getCarrier() {
if(this.carrier == null) {
this.carrier = new HashMap<Integer, String>();
this.carrier.put(1,"LIC");
this.carrier.put(2, "Bajaj");
}
return carrier;
}
public ObjectData getData() {
if(this.data == null) {
this.data = new ObjectData();
ObjectMember member1 = new ObjectMember();
member1.getExtData().setCarrierKey("");
ObjectMember member2 = new ObjectMember();
member2.getExtData().setCarrierKey("");
this.data.getMembers().add(member1);
this.data.getMembers().add(member2);
}
return data;
}
}
struts.xml中:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="stackoverflow" extends="struts-default" namespace="/">
<action name="stackoverflow" class="com.tutorialspoint.struts2.actions.StackOverflowAction" method="execute">
<result name="success">/jsp/stackoverflow/test.jsp</result>
</action>
</package>
</struts>