JSP页面如何找到正确的参数设置器

huangapple go评论91阅读模式
英文:

How do JSP pages find the correct setters for parameters

问题

以下是翻译好的内容:

假设我有以下的 JSP 页面:

<jsp:useBean id="bean" scope="page" class="com.test.jsp.beans.TestBean"/>
<jsp:setProperty name="bean" property="*"/>
...
<input type="text" name="test" value="test value"/>
...

以及对应的 Java Bean:

package com.test.jsp.beans;

public class TestBean {
    public String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

Java 是如何知道将 <input> 标签中的值传递给 setTest() 方法的?

我想要理解它们是如何连接的,我猜测使用了反射机制。

Java 是查找设置方法还是变量?

设置方法的名称是否需要是 set + <input> 的名称?

设置方法是否需要只包含一个参数?

该参数的名称是否需要与 <input> 标签的名称相同?

设置方法是否需要参数?

大写是否重要?

等等...

英文:

Say I have the following JSP page:

<jsp:useBean id="bean" scope="page" class="com.test.jsp.beans.TestBean"/>
<jsp:setProperty name="bean" property="*"/>
...
<input type="text" name="test" value="test value"/>
...

and the bean:

package com.test.jsp.beans;

public class TestBean {
    public String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

How does java know to pass the value from the <input> tag to the setTest() method?

I would like to understand the inner workings of how they are linked, I assume reflection is used.

Does java look for the setter method or does it look for the variable?

Does the setter name need to be set + <input> name?

Does the setter need to contain exactly one parameter?

Does that parameter need to be the same name as the <input> tag?

Does the setter even need parameters?

Does capitalization matter?

etc...

答案1

得分: 1

在官方 API 中存在一个名为 java.beans 的包。

用于分析 bean 类的工具的起点是 Introspector,它具有 getBeanInfo 方法,其类文档说明如下:

> 如果在类上找不到显式的 BeanInfo,我们将使用低级反射来研究类的方法,并应用标准设计模式来识别属性访问器、事件源或公共方法。然后,我们继续分析类的超类,并从中添加信息(可能是从超类链上传播的信息)。

这明确指出(公共)方法很重要。

与包中的 PropertyDescriptor 进行比较,该类提供属性的元信息,以及返回 Reflection Method 类实例的 getReadMethod()getWriteMethod()

属性访问器方法的模式在 Bean 规范的第7章和8.3节中进行了描述,但您已经展示了了解基础知识。通过了解 java.beans 包,您可以通过提供显式的 BeanInfo 实现来集成偏离标准模式的类。

英文:

There is a package java.beans in the official API.

A starting point for tool trying to analyze a bean class is the Introspector class having the getBeanInfo method, whose class doc states:

> If we don't find explicit BeanInfo on a class, we use low-level reflection to study the methods of the class and apply standard design patterns to identify property accessors, event sources, or public methods. We then proceed to analyze the class's superclass and add in the information from it (and possibly on up the superclass chain).

It clearly says that the (public) methods matter.

Compare with the PropertyDescriptor class of the package, which provides the meta information of a property, along with getReadMethod() and getWriteMethod() returning instances of the Reflection Method class.

The patterns for property accessor methods are described in chapter 7 and 8.3 of the Bean Specification, but you’ve already shown to know the basics. With the knowledge of the java.beans package, you may integrate classes deviating from the standard pattern, by providing explicit BeanInfo implementations.

答案2

得分: 0

Java正在检查请求参数。属性值上的星号表示在提交表单时,将自动设置与请求参数匹配的所有bean属性。

设置器名称是否需要设置为名称? 不需要,在输入元素中的名称将被转换为请求参数,然后与bean中的属性进行匹配。不需要显式设置setter方法的名称。

英文:

Java is inspecting the request parameters. The askeriks sign on the property value means that all bean proiperties which match the request parameters will be set automaticaly when the form is submitted.

Does the setter name need to be set + <input> name? No, the name in the input element will be converted to request parameter than matched against the properties in the bean. No need to explicitly set the name of the setter method.

huangapple
  • 本文由 发表于 2020年1月30日 19:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定