Java jsp:setproperty property="*" 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19274554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:37:21  来源:igfitidea点击:

jsp:setproperty what does property="*" mean?

javajsp

提问by Bawn

<jsp:setproperty name="Test" property="*">

What does this mean?

这是什么意思?

I know the definition is, "Sets a property in the specified JavaBean instance". So what is it setting a property in the javaBean test too ?

我知道定义是“在指定的 JavaBean 实例中设置属性”。那么它在 javaBean 测试中设置一个属性是什么?

采纳答案by Andy897

an asterisk (*) is used as the property attribute value of the action. This means that all bean properties with names that match request parameters sent to the page are set automatically

星号 (*) 用作操作的属性属性值。这意味着名称与发送到页面的请求参数匹配的所有 bean 属性都会自动设置

回答by Alex Theedom

Here is a complete example:

这是一个完整的例子:

Form.html

表单.html

<form method="POST" action="processForm.jsp">
    <input name="name"/>
    <input name="username"/>          
    <input name="jobTitle"/>
    <input name="city"/>
<input type="submit">   

The form collects input from the user and posts it to the processForm.jsp page.

该表单收集来自用户的输入并将其发布到 processForm.jsp 页面。

processForm.jsp

processForm.jsp

<%@ page import = "com.Employee"%>

...

<jsp:useBean id="employee" type="com.Person" class="com.Employee">
    <jsp:setProperty name="employee" property="*"/>
</jsp:useBean>

The <jsp:useBean>action creates an object of type com.Employeereferred to by a com.Personreference.

<jsp:useBean>动作产生的类型的对象com.Employee由称为com.Person参考。

The <jsp:setProperty>action matches the name of each of the input elements with the name of the getter method in the Employeeobject.

<jsp:setProperty>操作将每个输入元素的名称与Employee对象中的 getter 方法的名称相匹配。

For example: namematches with getNameand jobTitlematches to getJobTitle. Below is the Employeeclass. I have not included the Personinterface.

例如:name匹配getNamejobTitle匹配getJobTitle。下面是Employee类。我没有包含Person接口。

Employee.java

雇员.java

public class Employee implements Person{

    private String name;
    private String username;
    private String jobTitle;
    private String city;

    public String getJobTitle() {
           return jobTitle;
   }
    public void setJobTitle(String jobTitle) {
           this.jobTitle = jobTitle;
   }
    public String getName() {
           return name;
   }
    public void setName(String name) {
           this.name = name;
   }
    public String getCity() {
           return city;
   }
    public void setCity(String city) {
           this.city = city;
   }
    public String getUsername() {
           return username ;
   }
    public void setUsername(String username) {
           this.username = username;
   }        
}

Things to note about this standard action.

关于这个标准动作的注意事项。

  1. the name of the input element MUST be matchable with a getter method of the target object. name --> getName etc.
  2. Becareful with types. You cannot match to a Map or an Array
  3. The same is true if the property is an object. It will need to be treated separately.
  4. If the type of the property in Employee is int and the value entered in the form is not convertable to an int then a java.lang.NumberFormatException will be thrown.
  1. 输入元素的名称必须与目标对象的 getter 方法匹配。名称 --> getName 等
  2. 小心类型。您不能匹配 Map 或 Array
  3. 如果属性是对象,情况也是如此。它需要单独处理。
  4. 如果 Employee 中的属性类型是 int 并且表单中输入的值不能转换为 int ,那么将抛出 java.lang.NumberFormatException 。