jsp:setproperty what does property=“*” mean?

前端 未结 2 1624
梦如初夏
梦如初夏 2021-01-19 04:26

What does this mean?

I know the definition is, \"Sets a property in the specified Java

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 05:27

    Here is a complete example:

    Form.html

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

    processForm.jsp

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

    The action creates an object of type com.Person referred to by a com.Employee reference.

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

提交回复
热议问题