当前位置: 代码迷 >> Web前端 >> 1.2版标签支持el话语
  详细解决方案

1.2版标签支持el话语

热度:739   发布时间:2012-11-23 00:03:43.0
1.2版标签支持el语句
1.2版的标签不支持el语句,需要在标签体中处理el语句。
2.0版的标签继承SimpleTagSupport类,已经支持el语句,不需要在标签体里面单独处理。

详细参考:http://liudaoru.iteye.com/blog/194491
ExpressionEvaluatorManager.evaluate("selectProvince", subjectionCode, String.class, this, pageContext);
五个参数:
selectProvince:标签名称
subjectionCode:要支持el语句的属性名称
String.class:属性class
this: 本标签
pageContext:本标签继承TagSupport的属性

package com.chenkun.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

public class SelectProvinceTag extends TagSupport{
    private String subjectionCode;
    public int doEndTag() throws JspException {
        return TagSupport.EVAL_BODY_INCLUDE;
    }
   
    public int doStartTag() throws JspException {
        Connection con = JdbcUtil.getConnection();
        Statement stm = null;
        ResultSet rs = null;
        StringBuilder sb = new StringBuilder("");
        sb.append("<select name=\"subjectionCode\">" + "\n");
        if(StringUtils.isBlank(subjectionCode) || subjectionCode.equals("0")){
            sb.append("<option value=\"0\" selected>--省份--</option>" + "\n");
        }else{
            sb.append("<option value=\"0\">--省份--</option>" + "\n");
        }
        try {
            stm = con.createStatement();
            rs = stm.executeQuery("select SSDM, ABBR from DIC_SSMC where IS_EXTENDED = 0");
            while(rs.next()){
                String value = rs.getString("SSDM");
                String name = rs.getString("ABBR");
                if(!value.equals(subjectionCode)){
                    sb.append("<option value=\"" + value + "\">&nbsp;" + name + "</option>" + "\n");
                }else{
                    sb.append("<option value=\"" + value + "\" selected>&nbsp;" + name + "</option>" + "\n");
                }
            }
            sb.append("</select>");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.realease(rs, stm, con);
        }
        try {
            pageContext.getOut().print(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    } 
    
    public String getSubjectionCode() {
        return subjectionCode;
    }
    
    public void setSubjectionCode(String subjectionCode) {
        try {
            this.subjectionCode = (String) ExpressionEvaluatorManager.evaluate("selectProvince", subjectionCode, String.class, this, pageContext);
        } catch (JspException e) {
            e.printStackTrace();
        }
    }
}



  相关解决方案