JSP自定义标签库(找不到属性的setter方法)

发布于 2024-07-07 21:11:28 字数 2093 浏览 6 评论 0原文

我在使用自定义标记时遇到问题:-

org.apache.jasper.JasperException: /custom_tags.jsp(1,0) 无法找到属性的 setter 方法:firstname

这是我的 TagHandler 类:

package com.cg.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class NameTag extends TagSupport{

    public String firstname;
    public String lastname;

    public void setFirstName(String firstname){

        this.firstname=firstname;
        }
    public void setLastName(String lastname){

        this.lastname=lastname;
        }

    public int doStartTag() throws JspException {
        try {
            JspWriter out=pageContext.getOut();
            out.println( "First name:  "+firstname+ "Last name: "+lastname);

        } catch (Exception ex) {
            throw new JspException("IO problems");
        }
        return SKIP_BODY;
    }


}

这是我的 TLD 文件:

?xml version="1.0" encoding="UTF-8"?>
<taglib>
     <tlibversion>1.1</tlibversion>
     <jspversion>1.1</jspversion>
     <shortname>utility</shortname>
     <uri>/WEB-INF/nametagdesc.tld</uri>
     <info>
       A simple tag library for the examples
     </info>
   <tag>
       <name>name</name>
       <tagclass>com.cg.tags.NameTag</tagclass>
       <bodycontent>empty</bodycontent>
      <attribute>
      <name>firstname</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
      <name>lastname</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
 </tag>
</taglib>

并且这是我的 JSP 页面:

<%@ taglib uri="/WEB-INF/nametagdesc.tld" prefix="cg"  %>

<cg:name firstname="fname" lastname="lname"/>

我已经检查过代码是否已重新编译并正确部署等等......

所以,问题是,为什么它找不到 setter 方法???

I'm having trouble with a custom tag:-

org.apache.jasper.JasperException: /custom_tags.jsp(1,0) Unable to find setter method for attribute : firstname

This is my TagHandler class:

package com.cg.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class NameTag extends TagSupport{

    public String firstname;
    public String lastname;

    public void setFirstName(String firstname){

        this.firstname=firstname;
        }
    public void setLastName(String lastname){

        this.lastname=lastname;
        }

    public int doStartTag() throws JspException {
        try {
            JspWriter out=pageContext.getOut();
            out.println( "First name:  "+firstname+ "Last name: "+lastname);

        } catch (Exception ex) {
            throw new JspException("IO problems");
        }
        return SKIP_BODY;
    }


}

This is my TLD file:

?xml version="1.0" encoding="UTF-8"?>
<taglib>
     <tlibversion>1.1</tlibversion>
     <jspversion>1.1</jspversion>
     <shortname>utility</shortname>
     <uri>/WEB-INF/nametagdesc.tld</uri>
     <info>
       A simple tag library for the examples
     </info>
   <tag>
       <name>name</name>
       <tagclass>com.cg.tags.NameTag</tagclass>
       <bodycontent>empty</bodycontent>
      <attribute>
      <name>firstname</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
      <name>lastname</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
 </tag>
</taglib>

And this is my JSP page:

<%@ taglib uri="/WEB-INF/nametagdesc.tld" prefix="cg"  %>

<cg:name firstname="fname" lastname="lname"/>

I have checked that the code is recompiled and deployed correctly etc etc....

So, the question is , why can't it find the setter method???

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

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

发布评论

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

评论(2

梦中的蝴蝶 2024-07-14 21:11:28

检查标签元素中属性的大小写 - 它们应该与设置器的大小写匹配,而不是成员变量的大小写(顺便说一下,成员变量可能应该是私有的)。

规则是属性名称的第一个字母大写,然后结果以“set”为前缀,以得出设置器名称。

在您的例子中,您已调用属性 'firstname',因此该规则会导致 JSP 编译器查找 'setFirstname' 方法。 当您将 setter 命名为 'setFirstName'(大写“N”)时,您应该使用 'firstName'(也大写“N”)作为属性名称。

'lastname' 属性应用相同的规则,得到 'lastName',您就可以开始工作了。

PS 在这种情况下,使用一个好的 IDE(例如 IntelliJ)会有所帮助,因为它会建议属性的有效名称,从而节省您很多麻烦。

Check the case of the attributes in your tag element - they should match the case of the setter, not the case of the member variables (Which should probably be private, by the way).

The rule is that the attribute name has its first letter capitalised and then the result is prefixed by 'set', to arrive at the setter name.

In your case, you've called the attribute 'firstname', so the rule results in the the JSP compiler looking for the 'setFirstname' method. As you've named your setter 'setFirstName' (with a capital 'N'), you should use 'firstName' (Also with a capital 'N') for the attribute name.

Apply the same rule to the 'lastname' attribute, to arrive at 'lastName', and you should be in business.

P.S. Using a good IDE, like IntelliJ, would have helped in this case, as it would have suggested the valid names for your attributes, saving you a lot of head scratching.

窗影残 2024-07-14 21:11:28

您示例中的 TLD 文件看起来像是无稽之谈,我不知道是否是因为您没有正确格式化它。

自定义标记的 tag 元素应该有一个 attribute 元素,该元素与您要公开的每个属性相对应。 类似于:

<tag>
  <name>...</name>
  <tag-class>...</tag-class>
  <body-content>...</body-content>
  <display-name>...</display-name>
  <description>...</description>

  <attribute>
    <name>firstName</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    <description>...</description>
  </attribute>
</tag>

请注意,默认情况下属性是字符串。 这可以通过在 attribute 元素中添加 type 元素来覆盖。

The TLD file in your example looks like nonsense, I don't know if it's because you've not formatted it correctly.

The tag element for your custom tag should have an attribute element that corresponds to each attribute you want to expose. Something like:

<tag>
  <name>...</name>
  <tag-class>...</tag-class>
  <body-content>...</body-content>
  <display-name>...</display-name>
  <description>...</description>

  <attribute>
    <name>firstName</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    <description>...</description>
  </attribute>
</tag>

Note that by default attributes are Strings. This can be overridden by adding a type element within the attribute element.

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