如何从TagHandler中的Custom标签传递值?

时间:2011-07-13 19:11:13

标签: java jsp jstl

如何在JSP页面上获取作为标记属性传递的值?

<abc:myTag name="${userName}", empId="${empId}">
  <b> Displaying user's info in HTML </b>
<abc:myTag />

我需要在我的TagHandler类中访问userName和empId的值:

public class myTag extends TagSupport {

       protected String name = null;
       protected String empId = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getEmpId() {
        return empId;
    }


    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public int doStartTag() throws javax.servlet.jsp.JspException {

      //Need UserName and EmpID fields in this method.
      //How?

        return Tag.SKIP_BODY;

       }

}

1 个答案:

答案 0 :(得分:0)

调用标记时不需要逗号。

<abc:myTag name="${userName}" empId="${empId}">   
<b> Displaying user's info in HTML </b> 
<abc:myTag />

试试这个。

public int doStartTag() throws javax.servlet.jsp.JspException {        
//Access UserName and EmpID fields in this method. 

name = (String)pageContext.getAttribute("name",PageContext.PAGE_SCOPE);
empId = (String)pageContext.getAttribute("empId",PageContext.PAGE_SCOPE);
return Tag.SKIP_BODY;         
}