JSP自定义标签库(传递属性)

发布于 2024-07-08 09:32:05 字数 147 浏览 4 评论 0 原文

我尝试在自定义标记中使用多个属性,例如:

<mytaglib:mytag firstname="Thadeus" lastname="Jones" />

如何访问 TagHandler 代码中的属性?

I'm trying to use multiple attributes in my custom tag, e.g.:

<mytaglib:mytag firstname="Thadeus" lastname="Jones" />

How can I access the attributes in the TagHandler code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榆西 2024-07-15 09:32:05

为了访问参数,您的 TagHandler 类应该定义私有成员并提供访问器方法。

public class TagHandler extends TagSupport {
    private String firstName;
    private String lastName;

    public void setFirstName(String firstname) { firstName = firstname; }
    public void setLastName(String lastname) { lastName = lastname;}
}

然后您可以通过 TagHandler 变量访问参数。

public int doStartTag() throws JspException {
    pageContext.getOut().print(lastName + ", " + firstName);
}

如果您仍然有问题,请仔细检查您的命名约定,Java 解释器正在尝试猜测 setter 方法是什么。 因此,如果您的参数是“FirstName”,则设置方法必须是“setFirstName”,如果参数是“lastname”,则设置参数必须是“setlastname”。 我更喜欢遵循前者,因为它是标准的 Java 命名约定。

In order to access the parameters your TagHandler class should define the private members and provide accessor methods.

public class TagHandler extends TagSupport {
    private String firstName;
    private String lastName;

    public void setFirstName(String firstname) { firstName = firstname; }
    public void setLastName(String lastname) { lastName = lastname;}
}

you can then access the parameters through the TagHandler variables.

public int doStartTag() throws JspException {
    pageContext.getOut().print(lastName + ", " + firstName);
}

If you still have problems double check your naming conventions, the Java interpeter is trying to guess what the setter method is. So if your parameter is "FirstName" than the set method must be "setFirstName" if the parameter is "lastname" the set parameter must be "setlastname". I perfer to follow the former, since it is the standard Java naming convention.

蓝海似她心 2024-07-15 09:32:05

并不是你所问问题的真正答案,但我讨厌(即从未写过)TagHandler,但我喜欢 标记文件。 允许您使用 jsp 文件编写自定义标签。 您可能知道它们并且不可用/不适用 - 但我想我会提及它们以防万一。

Not really the answer to what you asked, but I hate (ie have never written) TagHandler's but I love tag files. Lets you write custom tags using jsp files. You probably know about them and are not available/applicable - but thought I'd mention them just in case.

铁憨憨 2024-07-15 09:32:05

为了演示这个问题的解决方案,让我们打个比方。 假设我们有从index.jsp 检索到的“userName”和“password”,并且我们必须在自定义标记属性中传递数据。 就我而言,它的工作原理

<body>

<%
String name=request.getParameter("name");
String password=request.getParameter("password");
%>

<%@ taglib prefix="c" uri="/WEB-INF/mytag.tld" %>

<c:logintag name="<%=name %>" password="<%=password %>"/>

To demonstrate the solution of this problem lets take an analogy . Suppose we have "userName" and "password" which is retrieved from index.jsp and we have to pass our data in custom tag attribute. In my case its working

<body>

<%
String name=request.getParameter("name");
String password=request.getParameter("password");
%>

<%@ taglib prefix="c" uri="/WEB-INF/mytag.tld" %>

<c:logintag name="<%=name %>" password="<%=password %>"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文