In jsp, the <jsp: setProperty> action command is used to set the JavaBean attribute. There are four formats.
<Jsp: setProperty name = "instantiated Object name" property = "*"/>
In this method, "*" indicates that the JavaBean attribute is set based on all parameters passed in the form. The passed parameter value must be consistent with the attribute name in the JavaBean.
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<% @ Page import = "java. SQL. *" %>
03
<! DOCTYPE html>
04
<Html>
05
<Head>
06
<Title> User form </title>
07
</Head>
08
<Body>
09
<Form action = "SetPropertyDemo. jsp" method = "post">
10
<Table>
11
<Tr> <td colspan = "2"> User forms </td> </tr>
12
<Tr> <td> User name: </td> <input type = "text" name = "username"/> </td> </tr>
13
<Tr> <td> User password: </td> <input type = "password" name = "password"/> </td> </tr>
14
<Tr> <td colspan = "2"> <input type = "submit"> <input type = "reset"/> </td> </tr>
15
<Tr> </tr>
16
</Table>
17
</Form>
18
</Body>
19
</Html>
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> set JavaBean attributes </title>
06
</Head>
07
<Body>
08
<% -- Call JavaBean through useBean Action Command -- %>
09
<Jsp: useBean id = "user" scope = "page" class = "com. javaweb. ch07.UserBean"> </jsp: useBean>
10
<% -- Set attributes in JavaBean based on all parameters -- %>
11
<Jsp: setProperty name = "user" property = "*"/>
12
<%
13
// Print the username of the output user
14
Out. println ("username:" + user. getUsername () + "<br/> ");
15
// Print the output user password
16
Out. println ("user Password:" + user. getPassword () + "<br/> ");
17
%>
18
</Body>
19
</Html>
<Jsp: setProperty name = "instantiated Object name" property = "property name"/>
The form submission page is the same as above.
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> set JavaBean attributes </title>
06
</Head>
07
<Body>
08
<% -- Call JavaBean through useBean Action Command -- %>
09
<Jsp: useBean id = "user" scope = "page" class = "com. javaweb. ch07.UserBean"> </jsp: useBean>
10
<% -- Set attributes in JavaBean based on all parameters -- %>
11
<Jsp: setProperty name = "user" property = "username"/>
12
<%
13
// Print the username of the output user
14
Out. println ("username:" + user. getUsername () + "<br/> ");
15
// Print the output user password
16
Out. println ("user Password:" + user. getPassword () + "<br/> ");
17
%>
18
</Body>
19
</Html>
<Jsp: setProperty name = "instantiate Object name" property = "property name" param = "parameter name"/>
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<% @ Page import = "java. SQL. *" %>
03
<! DOCTYPE html>
04
<Html>
05
<Head>
06
<Title> User form </title>
07
</Head>
08
<Body>
09
<Form action = "SetPropertyDemo. jsp" method = "post">
10
<Table>
11
<Tr> <td colspan = "2"> User forms </td> </tr>
12
<Tr> <td> User name: </td> <input type = "text" name = "username"/> </td> </tr>
13
<Tr> <td> User password: </td> <input type = "password" name = "userpassword"/> </td> </tr>
14
<Tr> <td colspan = "2"> <input type = "submit"> <input type = "reset"/> </td> </tr>
15
<Tr> </tr>
16
</Table>
17
</Form>
18
</Body>
19
</Html>
Note the name attribute of the password form.
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> set JavaBean attributes </title>
06
</Head>
07
<Body>
08
<% -- Call JavaBean through useBean Action Command -- %>
09
<Jsp: useBean id = "user" scope = "page" class = "com. javaweb. ch07.UserBean"> </jsp: useBean>
10
<% -- Set attributes in JavaBean based on all parameters -- %>
11
<Jsp: setProperty name = "user" property = "username" param = "username"/>
12
<Span style = "color: # e53333;"> <jsp: setProperty name = "user" property = "<span style =" color: #003399; "> password </span>" param = "<span style =" color: #003399; "> userpassword </span>"/> </span> <%
13
// Print the username of the output user
14
Out. println ("username:" + user. getUsername () + "<br/> ");
15
// Print the output user password
16
Out. println ("user Password:" + user. getPassword () + "<br/> ");
17
%>
18
</Body>
19
</Html>
Pay attention to the blue part in red in the above Code. www.2cto.com is more flexible.
<Jsp: setProperty name = "instantiate Object name" property = "property name" value = "property value"/>
01
<% @ Page language = "java" contentType = "text/html; charset = gb2312" %>
02
<! DOCTYPE html>
03
<Html>
04
<Head>
05
<Title> set JavaBean attributes </title>
06
</Head>
07
<Body>
08
<% -- Call JavaBean through useBean Action Command -- %>
09
<Jsp: useBean id = "user" scope = "page" class = "com. javaweb. ch07.UserBean"> </jsp: useBean>
10
<% -- Set attributes in JavaBean based on all parameters -- %>
11
<Jsp: setProperty name = "user" property = "username" value = "Devidpeng"/>
12
<Jsp: setProperty name = "user" property = "password" value = "Devidpeng"/>
13
<%
14
// Print the username of the output user
15
Out. println ("username:" + user. getUsername () + "<br/> ");
16
// Print the output user password
17
Out. println ("user Password:" + user. getPassword () + "<br/> ");
18
%>
19
</Body>
20
</Html>
From Zhang Dapeng's blog