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

时间:2022-11-26 10:38:45

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

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

This is my TagHandler class:

这是我的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;
    }


}

This is my TLD file:

这是我的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>

And this is my JSP page:

这是我的JSP页面:

<%@ 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???

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

2 个解决方案

#1


18  

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).

检查标记元素中属性的大小写 - 它们应该与setter的大小写相匹配,而不是成员变量的大小写(顺便说一句,它应该是私有的)。

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.

规则是属性名称的首字母大写,然后结果以'set'为前缀,以得到setter名称。

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.

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

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

将相同的规则应用于'lastname'属性,以获得'lastName',您应该开展业务。

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.

附:使用一个好的IDE,比如IntelliJ,在这种情况下会有所帮助,因为它会为你的属性建议有效的名称,从而为你节省大量的时间。

#2


2  

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

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

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.

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

#1


18  

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).

检查标记元素中属性的大小写 - 它们应该与setter的大小写相匹配,而不是成员变量的大小写(顺便说一句,它应该是私有的)。

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.

规则是属性名称的首字母大写,然后结果以'set'为前缀,以得到setter名称。

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.

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

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

将相同的规则应用于'lastname'属性,以获得'lastName',您应该开展业务。

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.

附:使用一个好的IDE,比如IntelliJ,在这种情况下会有所帮助,因为它会为你的属性建议有效的名称,从而为你节省大量的时间。

#2


2  

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

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

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.

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