使用下面这个jsp的前 你需要写一个Stu类 生成set和get方法
Public Class Stu{ private int idprivate String namepublic void setId(int id){ this id=id} public int getId(){ return this id} public void setName(String name){ this name=name} public String getName(){ return this name} }
写个简单的servlet
public class gotoMall extends HttpServlet { public void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { ArrayList<Stu>stus=new ArrayList<Stu>()Stu stu =new Stu()stu setId( )stu setName("aa")stu setId( )stu setName("bb")stus add(stu )stus add(stu )request setAttribute("stus" stus)request getRequestDispatcher("/WEB INF/admin/my jsp") forward(request response)} public void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { this doGet(request response)} }
my jsp
lishixinzhi/Article/program/Java/JSP/201311/19829
用jsp做树形下拉框可以用java自定义标签实现。
参考代码如下:
package com.moonNigh.tagSupportimport java.io.IOException
import javax.servlet.http.HttpServletRequest
import javax.servlet.jsp.JspException
import javax.servlet.jsp.JspWriter
import javax.servlet.jsp.tagext.TagSupport
/**
*
*
* 树形下拉选择控件
*
*/
public class SelectorTag extends TagSupport {
private static final long serialVersionUID = 9878861374414215L
//标签name属性
private String name
private String imgPath
//所需javascript文件的路径
private String scriptPaht
//所需css文件的路径
private String cssPath
//项目的根路径
private String rootPath
//标签的value属性
private String value
private String text
private String path
/*
* 标签的actionUrl属性
* 联想查询结果数据通过向actionUrl属性指定的url请求得到
*/
private String actionUrl
private HttpServletRequest request=null
public String getActionUrl() {
return actionUrl
}
public void setActionUrl(String actionUrl) {
this.actionUrl = actionUrl
}
public String getValue() {
return value
}
public void setValue(String value) {
this.value = value
}
public String getImgPath() {
return imgPath
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath
}
public String getScriptPaht() {
return scriptPaht
}
public void setScriptPaht(String scriptPaht) {
this.scriptPaht = scriptPaht
}
public String getCssPath() {
return cssPath
}
public void setCssPath(String cssPath) {
this.cssPath = cssPath
}
public String getText() {
return text
}
public void setText(String text) {
this.text = text
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public SelectorTag()
{
}
/**
* 初始化变量
*/
private void initAbttributes()
{
request=(HttpServletRequest)this.pageContext.getRequest()
rootPath=request.getContextPath()
this.imgPath="/images/"
this.scriptPaht="/js/"
this.cssPath="/css/"
}
@Override
public int doStartTag() throws JspException {
initAbttributes()
path=rootPath+"/jsp/tags/treeSelectorPage.jsp?id="+id+"&actionUrl="+actionUrl
JspWriter out=pageContext.getOut()
try {
String tName=name
//引入javascript文件
out.println("<script type='text/javascript' charset='GB2312' src='"+rootPath+scriptPaht+"selector.js'></script>")
//引入css文件
out.println("<link rel='stylesheet' href='"+rootPath+cssPath+"selector.css' type='text/css' />")
StringBuilder tag=new StringBuilder("<input type='text' ")
tag.append("id='").append(id).append("'")
tag.append(" value='").append(text==null?"":text).append("'")
tag.append(" onclick='return showSelector(\"")
tag.append(id).append("\",\"").append(path).append("\")' readonly>")
tag.append("<input type='hidden' name='")
.append(tName).append("' id='").append(id).append("_value")
.append("' value='").append(value==null?"":value).append("'>")
out.println(tag.toString())
} catch (IOException e) {
e.printStackTrace()
}
return SKIP_BODY
}
}
运行结果:
jsp设置二级联动的下拉列表的例子如下:
<html><head>
<title>This is a test!</title>
</head>
<body>
<form name="frm">
<select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)">
<option selected>请选择</option>
<option value="1">脚本语言</option>
<option value="2">高级语言</option>
<option value="3">其他语言</option>//下拉列表数据
</select>
<select name="s2">
<option value="请选择" selected>请选择</option>
</select>
</form>
<script language="javascript">
//获取一级菜单长度
var select1_len = document.frm.s1.options.length
var select2 = new Array(select1_len)
//把一级菜单都设为数组
for (i=0 i<select1_len i++)
{ select2[i] = new Array()}
//定义基本选项
select2[0][0] = new Option("请选择", " ")
select2[1][0] = new Option("PHP", " ")
select2[1][1] = new Option("ASP", " ")
select2[1][2] = new Option("JSP", " ")
select2[2][0] = new Option("C/C++", " ")
select2[2][1] = new Option("Java", " ")
select2[2][2] = new Option("C#", " ")
select2[3][0] = new Option("Perl", " ")
select2[3][1] = new Option("Ruby", " ")
select2[3][2] = new Option("Python", " ")
//联动函数
function redirec(x)
{
var temp = document.frm.s2
for (i=0i<select2[x].lengthi++)
{ temp.options[i]=new Option(select2[x][i].text,select2[x][i].value)}
temp.options[0].selected=true
}
</script>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)