struts2相关问题

华而不实 2011-09-04 11:09:11
初学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:debug>
</body>
</html>

问题:1、selectAction中属性city的作用到现在没整明白,求看懂程序的大侠解释
2、求struts的配置文件(简单起见只要action的配置就好)
3、ServletContextListener此类在什么时候调用,(最好对此项目稍加解释)
...全文
66 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
华而不实 2011-09-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 li_xun 的回复:]
呵呵,还是看看基础的东西把
[/Quote]
我就是从基础开始看的,遇到这个东西有些糊涂了,不明白其原理了!
巽谷雨 2011-09-04
  • 打赏
  • 举报
回复
呵呵,还是看看基础的东西把
华而不实 2011-09-04
  • 打赏
  • 举报
回复
servletContextListener监听程序。以前没用过!我在程序中不用它了!
ylovep 2011-09-04
  • 打赏
  • 举报
回复
请求——servletContextListener进行拦截——分配到具体的action进行业务处理——返回,其中servletContextListener进行拦截的时候还有进一步的细节,楼主可以翻阅一下书籍
华而不实 2011-09-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ylovep 的回复:]
楼主你做这个例子想了解什么知识?想了解国际化,还是了解标签怎么用,还是action如何控制业务逻辑。要了解自己想要从实例中得到什么再去做,不然做起来很模糊
[/Quote]

我想知道selectAction、servletContextListener这两个类和Select.jsp是如何通过struts.xml文件相联系的!求大哥解释
qindwt1 2011-09-04
  • 打赏
  • 举报
回复
先学基本的级联,不用struts的标签,做一个级联出来,事实上 项目中用struts标签用的确实很少,有时候不够灵活。
完美灬缺陷 2011-09-04
  • 打赏
  • 举报
回复
感觉LZ对struts2都没什么基本概念呀。
还是到网上搜下struts2教程之类的资料看下吧、
ylovep 2011-09-04
  • 打赏
  • 举报
回复
楼主你做这个例子想了解什么知识?想了解国际化,还是了解标签怎么用,还是action如何控制业务逻辑。要了解自己想要从实例中得到什么再去做,不然做起来很模糊

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧