一、设置属性<jsp:setProperty>:
<jsp:serProperty>标签一共有四种使用方法:
1.自动匹配:<jsp:setProterty name="实例化对象的名称(id)“ property="*"/>
2.指定属性:<jsp:setProperty name="实例化对象的名称(id)" property="属性名称"/>
3.指定参数:<jsp:setProperty name="实例化对象的名称(id)“ property="属性名称"/>
4.指定内容:<jsp:setProperty name="实例化对象的名称(id)“ property="属性名称" value="内容"/>
默认情况下,所有的属性会和请求的参数名称进行匹配,匹配成功,则进行设置,这时可以通过param属性指定属性设置时所需要的具体参数。
样例1:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<% request.setCharacterEncoding("GBK"); %>
<jsp:useBean id="simple" scope="page" class="blog.csdn.joywy.SimpleBean"/>
<jsp:setProperty name="simple" property="name"/>
<h3>姓名:<%=simple.getName()%></h3>
<h3>年龄:<%=simple.getAge()%></h3>
</body>
</html>
只给name属性设置参数。
样例2:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<% request.setCharacterEncoding("GBK"); %>
<jsp:useBean id="simple" scope="page" class="blog.csdn.joywy.SimpleBean"/>
<jsp:setProperty name="simple" property="name" param="age"/>
<jsp:setProperty name="simple" property="age" param="name"/>
<h3>姓名:<%=simple.getName()%></h3>
<h3>年龄:<%=simple.getAge()%></h3>
</body>
</html>
样例3:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<% request.setCharacterEncoding("GBK"); %>
<% int age = 23; %>
<jsp:useBean id="simple" scope="page" class="he.xiao.wei.SimpleBean"/>
<jsp:setProperty name="simple" property="name" value="Joywy"/>
<jsp:setProperty name="simple" property="age" value="<%=age%>"/>
<h3>姓名:<%=simple.getName()%></h3>
<h3>年龄:<%=simple.getAge()%></h3>
</body>
</html>
二、取得属性:<jsp:getProperty>
在JavaBean的操作标签中,也提供了专门取得属性的标签:<jsp:getProperty>此标签会自动调用JavaBean中的getter()方法,与设置属性的标签相比,取得属性的标签只有一种语法形式:
<jsp:getProperty name="实例化对象的名称(id)" property="属性名称"/>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎来到望星空</title></head>
<body>
<% request.setCharacterEncoding("GBK"); %>
<jsp:useBean id="simple" scope="page" class="he.xiao.wei.SimpleBean"/>
<h3>姓名:<jsp:getProperty name="simple" property="name"/></h3>
<h3>年龄:<jsp:getProperty name="simple" property="age"/></h3>
</body>
</html>
注:设置和取得属性标签依靠的是反射机制运行。