【问题标题】:Use tag attributes in java code in jsp tag file在jsp标签文件的java代码中使用标签属性
【发布时间】:2014-11-12 00:06:15
【问题描述】:

.tag 文件的开头,我有一些由<%@attribute name="listname" required="true"%> 之类的东西声明的属性。

我可以使用${listname} 打印它。但我想在同一页面内的一个 java 代码中使用这个变量。

类似的,

<%
    String listname = ${listname};
    ...Some more code...
%>

我该怎么做。

如果有帮助,我正在使用 apache tomcat6。

我是这个环境甚至 Java 的新手。如果我使用了一些错误的术语,请原谅并纠正我。

【问题讨论】:

  • 使用属性和 JSTL 的想法正在摆脱使 JSP 难以理解、编辑和/或验证的 scriptlet (&lt;% %&gt;)。使用 MVC 方法并在 servlet 中获取所有逻辑。
  • 谢谢@SJuan76,我已经按照你说的做了。但这里没有逻辑。这里的代码将是基于listname 打印格式化输出的代码。这是视图所做的部分,对吧?
  • 还有一个附带的问题,我们如何直接从 servlet 传递这种属性?我使用标签属性从其他jsp传递它。我熟悉request.set/getAttribute
  • 您最好使用适当的标签。无论如何,属性要么在会话中,要么在请求对象中。根据您设置的位置,您可以执行getAttribute("listname")
  • @SJuan76,我使用&lt;t:tagname listname='somestring'&gt;&lt;/t:tagname&gt; 传递它。我尝试同时做session.getAttribute("listname")request.getAttribute("listname"),但它似乎不存在。

标签: java jsp tomcat tomcat6 jsp-tags


【解决方案1】:

您可以使用jspContext 中的属性获取传递给自定义标签的属性:-

<%@ tag description="Category Options" trimDirectiveWhitespaces="true"  pageEncoding="UTF-8" %>
<%@ attribute name="depth" required="false" type="java.lang.Integer" rtexprvalue="true"%>

<%
Integer depth = (Integer)jspContext.getAttribute("depth");
// do stuff with depth
%>

【讨论】:

    【解决方案2】:

    您是否考虑过在 Java 类而不是 JSP 中进行处理?配置与使用 .tag 文件有点不同:

    web.xml

    <jsp-config>
        <taglib>
            <taglib-uri>mytaglib</taglib-uri>
            <taglib-location>/WEB-INF/tags/mytaglib.tld</taglib-location>
        </taglib>
        ...
    </jsp-config>
    

    mytaglib.tld

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
    
        <tlib-version>1.0</tlib-version>
        <short-name>mylib</short-name>
        <uri>/WEB-INF/tags/mytaglib</uri>
        <tag>
            <name>checkbox</name>
            <tag-class>com.myapp.tag.MyTagSupport</tag-class>
            <body-content>empty</body-content>
            <attribute>
                <name>name</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            ...
        </tag>
        ...
    </taglib>
    

    JSP 包含

    <%@taglib uri="mytaglib" prefix="ml" %>
    

    标签支持类

    package com.myapp.tag;
    
    import javax.servlet.jsp.tagext.BodyTagSupport    
    
    public class MyTagSuppoort extends BodyTagSupport {
    
        private String name = name;
    
        // Values are autowired by the JSTL API
        public void setName ( String name ) {
            this.name = name;
        }
    
    }
    

    这也是如何在标签支持类中实现的一个不错的示例:

    http://www.tutorialspoint.com/jsp/jsp_custom_tags.htm

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多