<jsp:setProperty>
Introduction:
Description:
test.jsp
Introduction:
1. The <jsp:setProperty> element sets the value of one or more properties in a Bean, using the Bean's setter methods.
2. You must declare the Bean with <jsp:useBean> before you set a property value with <jsp:setProperty>. Because <jsp:useBean> and <jsp:setProperty> work together.
3. the Bean instance names they use must match (that is, the value of name in <jsp:setProperty> and the value of id in <jsp:useBean> must be the same).
Syntax:
<jsp:setProperty name = “bean instance name” property = “name” value = “vlaue”> |
Description:
1. We can use this standard action in the following combinations.
<jsp:setProperty property="uname" name="b" value="myjavahub0001"/>
2. It retrieves the value of request parameter and assign it to the specified bean property.
<jsp:setProperty property="uname" name="b" param="username" />
3. If the request parameter Name matches with bean property Name then it is not required to use param attribute .
<jsp:setProperty property="uname" name="b" />
4. <jsp:setProperty property="*" name="b"/>
‘*’ specifies all properties of the bean
Note: It iterates through all request parameters and it any parameter name matched with bean property name then assigns request parameter value through the bean property.
Attributes:
1. Name:
It represents the name of the bean object whose property has to set.This is exactly same as id attribute <jsp:useBean>,It is the mandatory attribute.
2.Property:
The name of the java bean property which has to set.It is the mandatory attribute.
3. Value:
It specifies the value which has to set to the java bean property.It is optional attribute and never becomes in combination with param attribute.
4. Param:
It represents the name of the request parameter whose value has to set to the bean property. It is optional attribute and never comes in combination with value attribute.
Example: login.jsp
<body > <form action = "/Scwcd_jsp/test.jsp"> Enter name: <input type = 'text' name = "uname"> <br> Enter mail : <input type = 'text' name = "mail"> <br> Enter age: <input type = 'text' name = "age"><br> <input type = "submit"> </form> </body> |
<jsp:useBean id="b" class="com.myjavahub.SimpleBean"></jsp:useBean> <jsp:setProperty property="mail" name="b" value="chamu001@gmail.com"/> <jsp:setProperty property="*" name="b"/> <table border=2> <tr><td><h2>property</h2></td><td><h2>value</h2></td></tr> <tr><td><h2>User Name</h2></td><td><h2><%= b.getUname()%></h2></td></tr> <tr><td><h2>Age</h2></td><td><h2><%= b.getAge()%></h2></td></tr> <tr><td><h2>Mail Id</h2></td><td><h2><%= b.getMail()%></h2></td></tr> </table> |
SimpleBean.java
package com.myjavahub; public class SimpleBean { private String uname ; private int age; private String mail ; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } } |
Output: http://localhost:8080/Scwcd_jsp/login.jsp
Enter name,mail,age and click on submit button :
Note: All the required type conversions will take care by automatically by the <jsp:setProperty> :(string to int).
Note: It is always a good programming practice to maintain form parameter name are exactly same as bean property names.
No comments:
Post a Comment