日期:2014-05-17  浏览次数:20816 次

struts2相关问题
初学struts无人指导,在书上看到这样一个小项目相关代码如下,整了好几天还是有些模糊,求大家的帮助。项目是实验struts中的list在<s:select>中的使用,效果是:
在浏览器页面中先出现ServletContextListener类中三个大城市,当选择改变时提交表单得到相应的小城市。(好像叫级联菜单的效果),跪求大侠的帮助

ServletContextListener类:
package com.test.select;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ApplicationListenter implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
Map<Integer,String> countries = new HashMap<Integer,String>();
countries.put(1, "US");
countries.put(2, "Canada");
countries.put(3, "China");
ServletContext servletContext = arg0.getServletContext();
System.out.print("ddddddddddddd");
servletContext.setAttribute("countries",countries);
}
}

selectAction类
package com.test.select;
import com.opensymphony.xwork2.ActionSupport;
public class selectAction extends ActionSupport {
private int city;
private int country;

public City[] getCities(){
System.out.println("aaaaaaaaaaaaaaaaa");
City[] cities = null;
if(country == 1){
cities = new City[3];
cities[0] = new City(1,"Atlanta");
cities[1] = new City(2,"Chicago");
cities[2] = new City(3,"Detrori"); 
} else if(country == 2){
cities = new City[3];
cities[0] = new City(4,"aaaaa");
cities[1] = new City(5,"bbbbb");
cities[2] = new City(6,"ccccc");
} else if(country == 3){
cities = new City[3];
cities[0] = new City(7,"ddddd");
cities[1] = new City(8,"eeeeeeee");
}else {
cities = new City[0];
}
return cities;
}
public int getCity() {
return city;
}
public void setCity(int city) {
this.city = city;
}
public int getCountry() {
return country;
}
public void setCountry(int country) {
this.country = country;
}
}

City类:
package com.test.select;

public class City {
private int id;
private String name;
public City(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Select.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>select Tag Example</title>
  </head>  
  <body>
  <s:form>
<s:select name="country" label="Country" emptyOption="true" list="#application.countries" onchange="this.form.submit()"/>
<s:select name="city" label="City" list="cities" listKey="id" listValue="name"/>
<s:submit></s:submit>
</s:form>
<s:debug></s:de