to: samkuang(执着前行) 用什么实现都行,可是方法呢?
解决方案 »
- 如何让工程自动生成日志--新手
- struts2 type="redirect"
- cookie共享的问题
- webservices传输的xml编码默认的是utf-8,要改成GBK在哪里改啊
- js值调用
- 大家帮我写写这个SQL语句,oracle里面的
- servlet中response.setHeader("Content-Disposition", "attachment;Filename=..")下载问题
- 求助,怎样能主框架的TITLE调用子框架的TITLE?
- 如何在servlet中实现jsp里<jsp:setProperty>的功能
- 求救~Hibernate 调用了一次方法,sql语句打印了两次
- 大家好,小弟最请教大家一个问题,关于打包的问题,参与都有分可送
- Struts中,ActionForm出现乱码,请高手指教
因为对于这样的下拉列表,在你的应用中,可能多个地方都要用!
所以,可以一劳永逸!你可以写一个可以接受参数的下拉列表!
根据你的输入参数进行查询。!
1:写.java文件
应该包含你的参数的 set 和 get 方法。
然后覆写 doStartTag() 方法。 public int doStartTag() throws JspException {
initInnerObject();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getDataSource.getConnection();
String sql =
"select 。";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
out.print(.......);.....................//这里可以用一个循环,输出你查询到的内容。
conn.close();
return EVAL_BODY_BUFFERED;
}
catch (Exception e) {
throw new JspException("error method attribute: ");
}
}
2 编写.tld 文件。注意,应该在其中著名你的必输项和选输项3 在 web.xml 中引用该标签。4 在 页面中引入该标签5 使用该标签
---------------------------
如果对详细写法还不了解,你可以查找写资料(很多的),你也可以联系我,因为我这里的标签代码实现的内容复杂,太多,也不容易理解,所以没有给你全部列出。
* Return an option string for a select input
*
* @param sActuralValue the actural value of the input
* @param sSelectItem the value of this option
* @param sDisplay the text to be displayed for this option
* @return the option string
*/
public String toSelectString(String sActualValue, String sSelectItem, String sDisplay)
{ if (sActualValue.trim().equals(sSelectItem))
return ("<option value='" + sSelectItem + "' selected>" + sDisplay + "</option>");
else
return ("<option value='" + sSelectItem + "'>" + sDisplay + "</option>");
}
----------------------------------------------------------
注:javaBean中用set和get方法,把记录保存到数组中;
Jsp页面中,作数组循环,直接调用toSelectString(),把参数写进去,sActualValue--列表提交后的值,sSelectItem --- id号(从数组中取得),sDisplay -- 列表显示的值(同前);
这样写
我先省略查询前面的声明啦~楼主
sql="";//你要查询的东东
Result rs=stmt.executeQuery(sql);
out.println("<select>");
whilt(rs.next()){
out.println("<option>"+rs.getString(1)+"</option>")
}
out.println("</select>")
这个加在jsp网页里面就成啦~
<select name="username" >
<option selected>请选择登陆人员名称</option>
<% while(rs.next())
{tmp=rs.getString("PCode").trim()+"-"+rs.getString("PName").trim();
%>
<option ><%=tmp%></option>
<%}%>
</select>
</p>