struts2如何实现select标签和doubleselect标签

struts2如何实现select标签和doubleselect标签

1. select标签有三种实现的形式:

(1)直接写在jsp页面里的list, map, array等结构

<s:select list="{'山东','山西','河南','河北'}" label="籍贯" />

(2)通过声明bean实体的方法调用对象中list, map, array等结构

struts2.values.Province

public class Province {
private int pid;
private String pname;

public Province(int pid, String pname) {
this.pid = pid;
this.pname = pname;
}

public void setPname(String pname) {
this.pname = pname;
}
public String getPname() {
return pname;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getPid() {
return pid;
}


public String toString() {
return new ToStringBuilder(this).append("pid", this.pid).append(
"pname", this.pname).toString();
}

}

struts2.values.Provinces.java

public class Provinces {

private List<Province> l;
private Map<Integer,Province> m;

public Provinces() {
l = new ArrayList<Province>();
l.add(new Province(1,"山东省"));
l.add(new Province(2,"山西省"));
l.add(new Province(3,"河北省"));
l.add(new Province(4,"河南省"));

m = new HashMap<Integer,Province>();
m.put(1, new Province(1,"山东省"));
m.put(2, new Province(1,"山西省"));
m.put(3, new Province(3,"河北省"));
m.put(4, new Province(4,"河南省"));
}

public List<Province> getL() {
return l;
}
public void setL(List<Province> l) {
this.l = l;
}
public Map<Integer, Province> getM() {
return m;
}
public void setM(Map<Integer, Province> m) {
this.m = m;
}
}

<s:bean name="struts2.values.Provinces" id="p" />

<s:select list="%{#p.l}" listValue="pname" listKey="pid" label="籍贯"></s:select>

(3). 保存在action类中的List, Map

注意一定是放在此action跳转到的页面中, 否则action不初始化也就没法产生List, Map

<s:form action="login" method="post">
<s:bean name="struts2.values.Provinces" id="p"></s:bean>
<s:select list="provinces" listValue="pname" listKey="pid" label="籍贯"></s:select>

2.doubleselect标签

struts2.LoginAction.java

public class LoginAction extends ActionSupport {

private List<Province> provinces;
private Map<Integer, List<City>> country;
private String name;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub

provinces = new ArrayList<Province>();
provinces.add(new Province(1,"山东省"));
provinces.add(new Province(2,"山西省"));
provinces.add(new Province(3,"河北省"));
provinces.add(new Province(4,"河南省"));

List<City> sdProvince = new ArrayList<City>();
sdProvince.add(new City(1,1,"济南"));
sdProvince.add(new City(2,1,"青岛"));
sdProvince.add(new City(3,1,"XX"));
sdProvince.add(new City(4,1,"XX"));

List<City> sxProvince = new ArrayList<City>();
sxProvince.add(new City(5,2,"太原"));
sxProvince.add(new City(6,2,"大同"));
sxProvince.add(new City(7,2,"XX"));
sxProvince.add(new City(8,2,"XX"));

List<City> hbProvince = new ArrayList<City>();
hbProvince.add(new City(9,3,"石家庄"));
hbProvince.add(new City(10,3,"承德"));
hbProvince.add(new City(11,3,"XX"));
hbProvince.add(new City(12,3,"XX"));

List<City> hnProvince = new ArrayList<City>();
hnProvince.add(new City(13,4,"郑州"));
hnProvince.add(new City(14,4,"洛阳"));
hnProvince.add(new City(15,4,"XX"));
hnProvince.add(new City(16,4,"XX"));

country = new HashMap<Integer, List<City>>();
country.put(1, sdProvince);
country.put(2, sxProvince);
country.put(3, hbProvince);
country.put(4, hnProvince);
return SUCCESS;
}

public List<Province> getProvinces() {
return provinces;
}

public void setProvinces(List<Province> provinces) {
this.provinces = provinces;
}

public Map<Integer, List<City>> getCountry() {
return country;
}

public void setCountry(Map<Integer, List<City>> country) {
this.country = country;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

网页中

<s:form action="login" method="post">

<s:doubleselect list="provinces" listKey="pid" listValue="pname"

doubleList="country[top.pid]" doubleListKey="pid"

doubleListValue="cname"doubleName="cname"></s:doubleselect>

</s:form>

这是最常见的一种报错, 如果你不幸遇到了这个错误且仔细查看代码均没有问题时, 请注意下面两个问题

org.apache.jasper.JasperException: tag 'select', field 'list': The requested list key 'p.l' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}

1.你的List, Map等结构是放在哪里的, 如果是放在action类中的, 你的网页有没有调用到这个action, 如果没有就直接使用其中的List, Map. 就会报错, 请把你要显示select或者是doubleselect的网页设置成action的跳转页并按照跳转来查看.

2.你的标签中可能存在没有填写的赋值, 如name, listKey, listValue等

你可能感兴趣的:(apache,jsp,bean)