s:doubleselect使用出现问题总结
最近使用s:doubleselect,关联省和地市,出现了跟大家一样的问题,地市显示不出来,ie提示:XXXTemp为空或不是对象。
经反复测试查找,发现问题出现的原因如下:出现其中的一项都可能导致这个问题。
一:doubleName="userId.cityNo",这样定义是想取的是类中的一个变量,不能这样使用,不能有‘.’出现,必须是cityNo才可以。
二:doubleList="cityList",cityList必须在public class myclass extends ActionSupport类中定义,不能单独定义出来,也不能在另外的一个类中定义。
我使用的是struts2构建的。
下面是我的代码:
java类:
package tutorial;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.util.OgnlValueStack;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
public class UITagExample extends ActionSupport implements Validateable {
List vehicalTypeList = new ArrayList();
Map vehicalSpecificMap = new HashMap();
public UITagExample() {
VehicalType car = new VehicalType("CarKey", "Car");
VehicalType motorcycle = new VehicalType("MotorcycleKey", "Motorcycle");
VehicalType annimal = new VehicalType("annimalKey", "annimal");
vehicalTypeList.add(car);
vehicalTypeList.add(motorcycle);
vehicalTypeList.add(annimal);
List cars = new ArrayList();
cars.add(new VehicalSpecific("MercedesKey", "Mercedes"));
cars.add(new VehicalSpecific("HondaKey", "Honda"));
cars.add(new VehicalSpecific("FordKey", "Ford"));
List motorcycles = new ArrayList();
motorcycles.add(new VehicalSpecific("SuzukiKey", "Suzuki"));
motorcycles.add(new VehicalSpecific("YamahaKey", "Yamaha"));
List annimals = new ArrayList();
annimals.add(new VehicalSpecific("a1Key", "a1"));
annimals.add(new VehicalSpecific("a2Key", "a2"));
vehicalSpecificMap.put(car, cars);
vehicalSpecificMap.put(motorcycle, motorcycles);
vehicalSpecificMap.put(annimal, annimals);
}
public List getVehicalTypeList() {
return vehicalTypeList;
}
public List getVehicalSpecificList() {
OgnlValueStack stack = (OgnlValueStack) ServletActionContext.getValueStack(ServletActionContext.getRequest());
Object vehicalType = stack.findValue("top");
if (vehicalType != null ){
if( vehicalType instanceof VehicalType) {
List l = (List) vehicalSpecificMap.get(vehicalType);
return l;
} }
return Collections.EMPTY_LIST;
}
public List getVehicalTypeList() {
return vehicalTypeList;
}
public List getVehicalSpecificList() {
OgnlValueStack stack = (OgnlValueStack) ServletActionContext.getValueStack(ServletActionContext.getRequest());
Object vehicalType = stack.findValue("top");
if (vehicalType != null ){
if( vehicalType instanceof VehicalType) {
List l = (List) vehicalSpecificMap.get(vehicalType);
return l;
}
}
return Collections.EMPTY_LIST;
}
public static class VehicalType {
String key;
String description;
public VehicalType(String key, String description) {
this.key = key;
this.description = description;
}
public String getKey() { return this.key; }
public String getDescription() { return this.description; }
public boolean equals(Object obj) {
if (! (obj instanceof VehicalType)) {
return false;
}
else {
return key.equals(((VehicalType)obj).getKey());
}
}
public int hashCode() {
return key.hashCode();
}
}
public static class VehicalSpecific {
String key;
String description;
public VehicalSpecific(String key, String description) {
this.key = key;
this.description = description;
}
public String getKey() { return this.key; }
public String getDescription() { return this.description; }
public boolean equals(Object obj) {
if (! (obj instanceof VehicalSpecific)) {
return false;
}
else {
return key.equals(((VehicalSpecific)obj).getKey());
}
}
public int hashCode() {
return key.hashCode();
}
}
}
jsp代码:
文件uiTagExample.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>UI Tags Example</title>
<s:head/>
</head>
<body>
<s:actionerror/>
<s:actionmessage/>
<s:fielderror />
<s:form name="test" id="test1"action="example" method="post" enctype="multipart/form-data" tooltipConfig="#{'jsTooltipEnabled':'true'}">
<s:doubleselect
tooltip="Choose your Vehical"
label="Favourite Vehical"
name="favouriteVehicalType"
list="vehicalTypeList"
listKey="key"
listValue="description"
value="'MotorcycleKey'"
doubleValue="'YamahaKey'"
doubleList="vehicalSpecificList"
doubleListKey="key"
doubleListValue="description"
doubleName="favouriteVehicalSpecific" headerKey="-1"
headerValue="---------- Please Select ----------"
emptyOption="true" />
</s:form>
</body>
</html>
文件index.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello</title>
<s:head />
</head>
<body>
<h2 >s:doubleselect</h2 >
<s:form action="uiTagExample">
Name: <s:textfield name="name" />
<s:submit />
</s:form>
</body>
</html>
struts.xml文件中加入
<package name="tutorial" extends="struts-default">
<action name="uiTagExample" class="tutorial.UITagExample">
<result>uiTagexample.jsp</result>
<result name="input">uiTagexample.jsp</result>
</action>
</package>