大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> struts的一个简单的包含select下拉框的例子

struts的一个简单的包含select下拉框的例子

时间: 2021-07-31 作者:daque

开始,固然是须要你先对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 *@created2003年6月4日 *@version1.0 */public class loginform extends actionform {//-----------------------------instance variableprivate 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 *@created2003年6月4日 *@version1.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 *@created2003年6月4日 *@version1.0 */public class htmlselectoption implements serializable {private string id;private string displayname;/** *constructor for the htmlselectoption object */public htmlselectoption() { }/** *constructor for the htmlselectoption object * *@paramid description of the parameter *@paramdisplaynamedescription 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>搞定!

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.