首先,当然是需要你先对struts有一定的了解:)
1. 定义相应页面(client.jsp)的form bean,这里假设为clientform;注意在struts_config.xml中定义映射关系;client.jsp中包含了你需要的html form内容,比如一个select下拉框;
这里是form bean的代码(其实就是一个java bean,继承了actionform,然后需要重载reset和validate方法):
———————————————–
package com.egi.core.ioblock.form;
import javax.servlet.http.httpservletrequest;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionerrors;
/**
* copyright: copyright (c) 2002</p> <p>
*@author sjoy
*@created 2003年6月4日
*@version 1.0
*/
public class loginform extends actionform {
//—————————–instance variable
private string appname = null;
private string type = null;
public string getappname() {
return appname;
}
public void setappname(string appname) {
this.appname = appname;
}
public string gettype() {
return type;
}
public void settype(string type) {
this.type = type;
}
public void reset(actionmapping mapping, httpservletrequest request) {
appname = null;
type = null;
}
public actionerrors validate(actionmapping mapping, httpservletrequest request) {
actionerrors errors = new actionerrors();
if (appname == null || appname.length() < 1) {
errors.add("application name", new actionerror("error.appname.required"));
}
return errors;
}
}
———————————————–
这里是actionservlet代码,继承action:
———————————————–
package com.egi.core.ioblock.action;
import java.io.ioexception;
import java.sql.sqlexception;
import javax.servlet.servletexception;
import javax.servlet.http.httpsession;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.action;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionforward;
import com.egi.core.ioblock.form.loginform;
import com.mainet.core.spreadsheet.db.menustreetable;
import com.mainet.core.spreadsheet.projectfactory;
/**
* copyright: copyright (c) 2002</p> <p>
*@author sjoy
*@created 2003年6月4日
*@version 1.0
*/
public class loginaction extends action {
public actionforward perform(actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws ioexception, servletexception {
actionerrors errors = new actionerrors();
string appname = ((loginform) form).getappname();
//下面是你所需要的一些逻辑
…
httpsession session = request.getsession();
…
return mapping.findforward("success");
}
}
———————————————–
2. 写一个bean,专门用来保存select的option集合。代码如下:
———————————————–
package com.egi.core.ioblock.util;
import java.io.serializable;
/**
* description: this class is a bean, used to represent one option in an html
* drop-down select list. it contains two properties – see {@link
* getdisplayname()} and {@link getinternalid()} for a description. useful in a
* struts form class for constructing a select list to pass to the jsp with the
* <tt><html:select></tt> and <tt><html:option></tt> tags.</p> <p>
*@author sjoy
*@created 2003年6月4日
*@version 1.0
*/
public class htmlselectoption implements serializable {
private string id;
private string displayname;
/**
* constructor for the htmlselectoption object
*/
public htmlselectoption() { }
/**
* constructor for the htmlselectoption object
*
*@param id description of the parameter
*@param displayname description of the parameter
*/
public htmlselectoption(string id, string displayname) {
this.id = id;
this.displayname = displayname;
}
public string getdisplayname() {
return displayname;
}
public void setdisplayname(string displayname) {
this.displayname = displayname;
}
public string getid() {
return id;
}
public void setid(string id) {
this.id = id;
}
}
———————————————–
3. ok,接下来从db或者其它地方去取下拉列表中的具体内容;
java.util.iterator iter = ….;//这里假设从数据库中取得数据
java.util.arraylist list = new java.util.arraylist();
string obj;
while(iter.hasnext()){
obj = (string)iter.next();
list.add(new com.egi.core.ioblock.util.htmlselectoption(obj,obj));
}
pagecontext.setattribute("appnames", list);
注意:这段逻辑也可以写在clienetform中通过javabean的方式在页面上获得这个集合。
4. 然后就是页面上使用啦:)
<html:select property="type">
<html:options collection="appnames" property="id"
labelproperty="displayname"/>
</html:select>
搞定!
