Home > Java > javaTutorial > How to use setproperty in jsp?

How to use setproperty in jsp?

藏色散人
Release: 2020-09-19 11:30:20
Original
5431 people have browsed it

"setproperty" in jsp is used to set the properties of the instantiated Bean object. Its syntax is such as "" .

How to use setproperty in jsp?

##jsp tag jsp:setProperty usage


is used to set an instance Properties of the Bean object

The first form:

<jps:setProperty name = "JavaBean实例名"  property = "*"/>
Copy after login

This form is a shortcut for setting Bean properties. The name and type of the properties in the Bean must be the same as Parameter names in the request object match. Since the data types passed in the form are all of String type, the internal mechanism of Jsp will convert these parameters into the corresponding types of Bean attributes.

property = "*" means that all request parameters whose names match the Bean property names will be passed to the corresponding property set method.

Second form:

<jsp:setProperty name = "JavaBean实例名" property = "JavaBean属性名" />
Copy after login

Use a parameter value in the request object to specify an attribute value in the Bean. In this syntax, property specifies the property name of the Bean, and the names of the Bean property and the request parameter should be the same. In other words, if there is a setUserName(String userName) method in the Bean, then the value of the propertyName is "userName". This form is more flexible and can selectively assign values ​​to the properties in the Bean

The third form:

<jsp:setProperty name = "JavaBean实例名" property = "JavaBean属性名" value = "BeanValue"/>
Copy after login

value is used to specify the value of the Bean attribute. String data will be automatically converted into numbers, boolean, Boolean, byte, Byte, char, and Character through the standard valueOf method in the target class. For example, boolean and Boolean type attribute values ​​(such as "true") are converted by Boolean.valueOf, and int and Integer type attribute values ​​(such as "42") are converted by Integer.valueOf.

Fourth form:

<jsp:setProperty name = "JavaBean实例名" 
     property = "propertyName" param = "request对象中的参数名"
/>
Copy after login

param specifies which request parameter is used as the value of the Bean attribute. The names of bean properties and request parameters can be different. If the current request has no parameters, nothing will be done, and the system will not pass null to the set method of the Bean property. Therefore, you can let the bean provide the default property value itself, and only modify the default property value when the request parameter explicitly specifies a new value.

For example, the following code snippet means: if there is a numItems request parameter, set the value of the numberOfItems attribute to the value of the request parameter numItems; otherwise, do nothing.

<jsp:setProperty name="orderBean" property="numberOfItems" param="numItems" />
Copy after login

The following is a simple example:

//sampleBean.java
 
package MyJavaBeanPackage;
 
public class SampleBean2
{
 private String id;
 private String age;
 private String name;
 private String sex;
 private String addr;
public SampleBean2()
 {
 }
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
 public String getSex()
 {
  return sex;
 }
 public void setSex(String sex)
 {
  this.sex = sex;
 }
 
 public void setId(String id)
 {
  this.id = id;
 }
 public void setAge(String age)
 {
  this.age = age;
 }
 public String getId()
 {
  return id;
 }
 public String getAge()
 {
  return age;
 }
 public String getAddr()
 {
  return addr;
 }
 public void setAddr(String addr)
 {
  this.addr = addr;
 }
}
 
//SampleBean.html
<html>
  <head>
    <title>使用<jsp:getProperty>和<jsp:setProperty>标记</title>
  </head>
  
  <body>
   <form name = "form1" action = "Sample2.jsp" method = "post">
    <p align = "center">编号
     <input type = "text" name = "id">
    </p>
    <p align = "center">姓名
     <input type = "text" name = "name">
    </p>
    <p align = "center">性别
     <select name = "TheSex" id = "sex">
      <option value = "男" selected>男</option>
      <option value = "女">女</option>
       </select>
     </p>
     <p align = "center">年龄
      <input type = "text" name = "TheAge">
     </p>
     <p align = "center">
      <input type = "submit" value = "提交">
      <input type = "reset" value = "重置">
     </p>
    </form>
   </body>
 </html>
Copy after login
//SampleBean.jsp
<%@ page
 language="java" 
 contentType = "text/html;charset = GBK"
%>
 
<jsp:useBean id = "sampleBean2" class = "MyJavaBeanPackage.SampleBean2"/> 
<html>
 <head>
  <title>在JavaBean中存放数据</title>
 </head>
 <body>
  <%--方法一
   <jsp:setProperty name = "sampleBean2" property = "*" />
  --%>
  <%-- 方法二 --%>
  <jsp:setProperty name = "sampleBean2" property = "id"/>
  <jsp:setProperty name = "sampleBean2" property = "name"/>
  <%--方法三 --%>
  <jsp:setProperty name = "sampleBean2" property = "addr" value = "洛阳师范学院"/>
  <%-- 方法四 bean属性的名字可以与request参数不同--%>
  <jsp:setProperty name = "sampleBean2" property = "sex" param = "TheSex"/>
  <jsp:setProperty name = "sampleBean2" property = "age" param = "TheAge"/>
  <center>
   编号:<jsp:getProperty name = "sampleBean2" property = "id"/><br>
   姓名:<jsp:getProperty name = "sampleBean2" property = "name"/><br>
   性别:<jsp:getProperty name = "sampleBean2" property = "sex"/><br> 
   年龄:<jsp:getProperty name = "sampleBean2" property = "age"/><br>
   住址:<jsp:getProperty name = "sampleBean2" property = "addr"/><br>
  </center>
 </body>
</html>
Copy after login




##98

The above is the detailed content of How to use setproperty in jsp?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template