5
<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 ?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Bawn
  • 509
  • 3
  • 13
  • 36

2 Answers2

21

Here is a complete example:

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

<%@ 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.Person referred to by a com.Employee reference.

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

For example: name matches with getName and jobTitle matches to getJobTitle. Below is the Employee class. I have not included the Person interface.

Employee.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.
Sagar
  • 3
  • 2
Alex Theedom
  • 1,604
  • 15
  • 16
  • 1
    Very good answer actually, +1. It made me understand something about jsp beans. But something troubled me while i read : the setProperty requiring the **getter** name. It is actually not, after testing it; it reveals that the setProperty matches the input element with the name of the **setter** method. If you have a java property named *foo*, with getter *getBar()* and setter *setJoe()*, the input element has to be named *"joe"* to match the bean setter method. – Charly Sep 22 '16 at 08:52
6

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

Andy897
  • 6,915
  • 11
  • 51
  • 86