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>
-----------------------------------------------
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>
解决方案 »
- 问下struts中的submit换图片问题。
- myeclipse自动部署问题
- hql语句怎么关联两个表,初学者求救!!
- 在struts的action中request.setAttribute("dayReportList", dayReportList);这个对像,在另一个页面取不这个值
- dao与service中代码的区别
- JWS开发WebServiecs(初学者)出错,请大家看看!
- JSP页面中通过JDBC连MySQL的中文乱码问题,看过一些帖子,但还是没能解决
- 急问,现在最新版本的jdk和tomcat调试javabean的时候还需要重启动webserver吗??
- 大家没有用文本编辑器开发j2ee的吗,难道都用集成环境不成?
- SSO单点登录,AD域,CS
- 请问怎么样整合oc4j与jbuilder9并运行程序?
- 关于weblogic 更改默认 index.jsp index.html
怎样用Iterator 得到数据库中的数据。对不起,我刚刚学习java被公司逼着作struts的项目!到现在为止我只知道有一个resultset返回了数据库中的数据!请楼上在具体给讲讲!!!
谢谢!!!