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
jsp:setproperty what does property="*" mean?
提问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: name
matches with getName
and jobTitle
matches to getJobTitle
. Below is the Employeeclass. I have not included the Personinterface.
例如:name
匹配getName
和jobTitle
匹配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.
关于这个标准动作的注意事项。
- the name of the input element MUST be matchable with a getter method of the target object. name --> getName etc.
- Becareful with types. You cannot match to a Map or an Array
- The same is true if the property is an object. It will need to be treated separately.
- 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.
- 输入元素的名称必须与目标对象的 getter 方法匹配。名称 --> getName 等
- 小心类型。您不能匹配 Map 或 Array
- 如果属性是对象,情况也是如此。它需要单独处理。
- 如果 Employee 中的属性类型是 int 并且表单中输入的值不能转换为 int ,那么将抛出 java.lang.NumberFormatException 。