将会话属性传递到自定义JSP标记

时间:2013-09-02 18:02:31

标签: java jsp session struts2

是否可以将会话中的对象传递给JSP标记属性?

即。我有一个类存储有关菜单节点的信息。 我将其对象存储在"app.menu"键下的会话中。

我创建了菜单标签处理程序:

public class MenuTagHandler extends TagSupport {

    private static final long serialVersionUID = 7116890687703774365L;

    private Menu menu;
    private String id;
    private String cssClass;
    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public int doStartTag() throws JspException {
        logger.info("tagSupport doStartTag method entered");

        return SKIP_BODY;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

}

TLD文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>menu</short-name>
    <description>Vertical menu tag</description>
    <tag>
        <name>cloudUMLMenu</name>
        <tag-class>clouduml.menu.MenuTagHandler</tag-class>
        <description>Menu tag handler</description>
        <attribute>
            <name>menu</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>clouduml.menu.Menu</type>
        </attribute>
        <attribute>
            <name>id</name>
            <required>false</required>
        </attribute>
        <attribute>
            <name>cssClass</name>
            <required>false</required>
        </attribute>
    </tag>
</taglib> 

JSP文件中的调用:

<menu:cloudUMLMenu menu="#session['app.menu']"/>

不幸的是,我总是得到例外:

org.apache.jasper.JasperException: Unable to convert string "#session['clouduml.menu']" to class clouduml.menu.Menu

我可以将密钥"app.menu"传递给menu属性,并将MenuTagHandler传递给会话中的菜单数据,但我想知道是否可以使用OGNL

2 个答案:

答案 0 :(得分:5)

如果要在会话中访问Menu对象,则无需将其设置为标记类的属性。请尝试以下操作,其中包括删除班级的Menu成员:

public class MenuTagHandler extends TagSupport {

    private String id;
    private String cssClass;
    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public int doStartTag() throws JspException {
        logger.info("tagSupport doStartTag method entered");

        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        HttpSession session = request.getSession();

        Menu menu = (Menu)session.getAttribute("app.menu");

        // do whatever you need to do with the menu data

        return SKIP_BODY;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }
}

答案 1 :(得分:1)

与由JSP引擎解析的EL不同,JSP中的OGNL表达式只是字符串。仅当标记将该字符串解析为OGNL表达式时,它才会从ValueStack创建或生成对象。您需要将值(在您的情况下,菜单)视为String,然后将其解析为OGNL表达式。这也是Struts2标签的工作方式。请注意,您还可以使用EL访问会话并将值传递给自定义标记。