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

时间:2022-11-26 10:34:12

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?

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

3 个解决方案

#1


0  

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.

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

#2


3  

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

为了访问参数,您的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;}
}

you can then access the parameters through the TagHandler variables.

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

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.

如果你仍然有问题仔细检查你的命名约定,Java interpeter试图猜测setter方法是什么。因此,如果参数为“FirstName”,则set方法必须为“setFirstName”,如果参数为“lastname”,则set参数必须为“setlastname”。我更愿意遵循前者,因为它是标准的Java命名约定。

#3


0  

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

为了证明这个问题的解决方案,我们可以进行类比。假设我们有从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 %>"/>

#1


0  

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.

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

#2


3  

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

为了访问参数,您的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;}
}

you can then access the parameters through the TagHandler variables.

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

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.

如果你仍然有问题仔细检查你的命名约定,Java interpeter试图猜测setter方法是什么。因此,如果参数为“FirstName”,则set方法必须为“setFirstName”,如果参数为“lastname”,则set参数必须为“setlastname”。我更愿意遵循前者,因为它是标准的Java命名约定。

#3


0  

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

为了证明这个问题的解决方案,我们可以进行类比。假设我们有从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 %>"/>