Struts中标签中怎么设定初始显示值

jungle_sj 2004-04-29 03:07:57
<html:select name="syslogqryActionForm" property="selectedID">
<html:options collection ="selects" property="value" labelProperty="label"/>
</html:select>


怎么设定初始显示值,比如说,下拉框中一共有"label1","label2","label3","label4","label5"五项,我想在页面初始的时候让选择框中出现的默认项是"label3"该怎么做的呢?

...全文
349 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengfeitian 2004-05-02
  • 打赏
  • 举报
回复
to fuzuyuan(happyboy) ,写的真详细!学得够深入,学习中!
fuzuyuan 2004-04-30
  • 打赏
  • 举报
回复
onClick="form.submit()"
aaassd 2004-04-30
  • 打赏
  • 举报
回复
那么如何在选定之后就做动作,象51job一样?
fuzuyuan 2004-04-30
  • 打赏
  • 举报
回复
这个问题很简单,你只用在ActionForm中给<select>标签传过来的值加一个默认值就可以了!
下面是程序:
用于显示的页面
-------------------------------------------------------
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GBK" import="mypackage.SelBean"%>
<%
java.util.Vector options = new java.util.Vector();
options.add(new SelBean("sel 1", "Value 1"));
options.add(new SelBean("sel 2", "Value 2"));
options.add(new SelBean("sel 3", "Value 3"));
options.add(new SelBean("sel 4", "Value 4"));
options.add(new SelBean("sel 5", "Value 5"));
pageContext.setAttribute("options", options);
%>
<html:html>
<head>
<title>
select
</title>
</head>
<body bgcolor="#ffffff">
<html:form action="test.do" method="post">
<html:select property="sel">
<html:options collection="options" property="value" labelProperty="label"/>
</html:select>
<html:submit property="提交"/>
</html:form>
</body>
</html:html>
---------------------------------------------------
用到的bean,很显然,它是用来存储<option>的value和text的值。

package mypackage;
public class SelBean {
public SelBean(String label, String value) {
this.label = label;
this.value = value;
}
private String label;
public String getLabel() {
return this.label;
}
public void setLabel(String label) {
this.label = label;
}
private String value;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
---------------------------------------------
用到的Action

package mypackage;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class SelectAction extends Action {
public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm,

HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
/**@todo: complete the business logic here, this is just a skeleton.*/
SelectActionForm selectActionForm = (SelectActionForm) actionForm;
throw new java.lang.UnsupportedOperationException("Method perform() not yet

implemented.");
}
}
---------------------
用到的ActionForm,产生你要的效果的关键就在这个里面,你好好看看

package mypackage;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class SelectActionForm extends ActionForm {
public String getSel() {
return sel;
}
public void setSel(String sel) {
this.sel = sel;
}
private String sel="Value 4";//在这里,我们给一个默认值就可以达到你要的效果
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest

httpServletRequest) {
/**@todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}
------------------------------------------------------------------
struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration

1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<form-beans>
<form-bean name="selectActionForm" type="mypackage.SelectActionForm" />
</form-beans>
<global-forwards>
</global-forwards>
<action-mappings>
<action name="selectActionForm" type="mypackage.SelectAction" validate="true"

input="/select.jsp" scope="request" path="/test" />
</action-mappings>
</struts-config>
package com.list.struts.util; import java.util.ArrayList; public class Page { //当前跳转的页面情况 private String pagetype; private PaginationBean pagination; private ArrayList result; //构造函数实例化接收一个pagetype和PaginationBean对象 public Page(String pagetype, PaginationBean pagination) { this.pagetype = pagetype; this.pagination = pagination; } /** * * */ public boolean isEmpty() { if (pagetype == null || pagetype.equals("")) { return true; } else { return false; } } /** * * * */ public ArrayList getResult() { if (!isEmpty()) { if (pagetype.equals("nextPage")) { result = pagination.getNextPage(); } else if (pagetype.equals("previousPage")) { result = pagination.getPreviouspage(); } else if (pagetype.equals("lastPage")){ result = pagination.getLastPage(); } else if (pagetype.equals("firstPage")){ result = pagination.getFirstPage(); } else{ result=pagination.getJumpPage(pagetype); } } else { result = pagination.getProducts(); } return result; } /** * * * */ //此对象保存了页面的所有情况,包括多少页等信息 public PaginationBean getPagePagination() { return pagination; } /** * * */ //实例化PaginationBean的构造函数 public void Init(ArrayList result) { this.result = result; pagination = new PaginationBean(result); } } ------------------------------ import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.list.struts.form.ListForm; import com.list.struts.util.Page; import com.list.struts.util.PageBean; import com.list.struts.util.PaginationBean; import com.list.struts.vo.newgetResouce; public class ListAction extends Action { /* * Generated Method

81,122

社区成员

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

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