jsp:setproperty 属性=“*” 未设置所有属性

jsp:setproperty property=“*” not set all properties

我有 jsp 个页面,其中包含

<jsp:useBean id="RcvMsgTransferTanseekBean" class= "com.test.RcvMsgTransferTanseekBean" />  
<jsp:setProperty name="RcvMsgTransferTanseekBean" property="*" />

和包含

的bean
private String nId = "";
public void setNId (String value){
    this.nId = value;  
}

public String getNId(){
    return this.nId;
}

我向 jsp 页面发送请求

test.jsp?letId=479438&dstId=522375&nId=138393&subject=66666666666&letForInfo=1&shNotMan=true

我的问题是 nId 参数仍然是空的。

当我在 jsp

中添加新行时
<jsp:setProperty name="RcvMsgTransferTanseekBean" property="nId" />

它工作正常,为什么 'property="*"' 没有像预期的那样工作?

问题出在 属性.
的名称中 对于单个前导字母,CamelCase 的大写和取消大写并不像您期望的那样工作。 为了设置 属性 的值,使用了 setter 方法。
nId 的 setter 是 setNId.

Java Bean Spacification 说:

8.8 Capitalization of inferred names.

When we use design patterns to infer a property or event name, we need to decide what rules to follow for capitalizing the inferred name. If we extract the name from the middle of a normal mixedCase style Java name then the name will, by default, begin with a capital letter. Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,

“FooBah” becomes “fooBah” “Z” becomes “z” “URL” becomes “URL”

We provide a method Introspector.decapitalize which implements this conversion rule.

Documentation for Introspector:

public static String decapitalize(String name)

Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone.

Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

所以为了找到参数和setter之间的匹配:

  • setter 资本化。所以从 setNId,我们得到 NId
  • 如果你提交了一个参数NId,值进入javaBean,
  • 但是如果您有 nId,则找不到匹配的 setter。

解决方法:避免这种情况:

在 属性 个名字的开头使用一个以上的小写字母。

在这种特殊情况下,您可以使用 numId.

而不是 nId

或将 setter/getter 名称更改为 setnId()getnId()

使用具有此类 属性 和 getter/setter 名称的 Bean 的意外行为

  • <jsp:setProperty name="RcvMsgTransferTanseekBean" property="*" />
    只是跳过 属性 nId。没有错误。没有例外。
  • <jsp:setProperty name="RcvMsgTransferTanseekBean" property="nId"/>
    抛出 org.apache.jasper.JasperException: PWC6054: 在 'com.test.RcvMsgTransferTanseekBean'
  • 类型的 bean 中找不到关于 属性 'nId' 的任何信息
  • ${RcvMsgTransferTanseekBean.nId}
    throws org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: class 'com.test.RcvMsgTransferTanseekBean' 没有 属性 'nId'.