JSP动作-jsp:setProperty和jsp:getProperty

1 jsp:setProperty动作标签语法

jsp:setProperty和jsp:getProperty动作标签用于通过Java Bean开发Web应用程序。

jsp:setProperty动作标签使用setter方法在Bean中设置一个或多个属性值。

2 jsp:getProperty动作标签语法

<jsp:setProperty name="instanceOfBean" property= "*"   |   
property="propertyName" param="parameterName"  |   
property="propertyName" value="{ string | <%= expression %>}"   
/>  

2.1 jsp:getProperty动作标签示例

1)通配所有属性名称

<jsp:setProperty name="bean" property="*" />  

2)传入指定属性名

<jsp:setProperty name="bean" property="username" />   

3)传入指定属性名和属性值

<jsp:setProperty name="bean" property="username" value="yiidian" />  

3 jsp:getProperty动作标签语法

jsp:getProperty动作标签用于返回属性的值。语法如下:

<jsp:getProperty name="instanceOfBean" property="propertyName" /> 

4 jsp:getProperty动作标签简单示例

<jsp:getProperty name="obj" property="name" />  

5 jsp:setProperty和jsp:getProperty综合示例

5.1 编写index.jsp

<%@ page language="java" contentType="text/html;charset=utf-8" %>
<html>
<html>
<head>
    <meta charset="utf-8">
    <title>一点教程网-jsp:setProperty和jsp:getProperty动作标签</title>
</head>
<body>

<form action="process.jsp" method="post">
    用户名:<input type="text" name="name"><br>
    密码:<input type="password" name="password"><br>
    邮箱:<input type="text" name="email"><br>
    <input type="submit" value="注册">
</form>

</body>
</html>

5.2 编写User实体类

package com.yiidian;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class User {
    private String name;
    private String password;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

5.3 编写process.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>一点教程网-jsp:setProperty和jsp:getProperty动作标签</title>
</head>
<body>

<jsp:useBean id="u" class="com.yiidian.User"></jsp:useBean>
<jsp:setProperty property="*" name="u"/>

结果是:<br>
<jsp:getProperty property="name" name="u"/><br>
<jsp:getProperty property="password" name="u"/><br>
<jsp:getProperty property="email" name="u" /><br>

</body>
</html>

5.4 运行测试


6 在多个JSP页面共用Bean对象

下面的示例中,在两个JSP页面中打印Bean对象的数据。

6.1 编写index.jsp

和上面的案例一样

6.2 编写User实体类

和上面的案例一样

6.3 编写process.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>一点教程网-jsp:setProperty和jsp:getProperty动作标签</title>
</head>
<body>

<jsp:useBean id="u" class="com.yiidian.User" scope="session"></jsp:useBean>
<jsp:setProperty property="*" name="u"/>

结果是:<br>
<jsp:getProperty property="name" name="u"/><br>
<jsp:getProperty property="password" name="u"/><br>
<jsp:getProperty property="email" name="u" /><br>

<a href="second.jsp">访问second.jsp页面</a>

</body>
</html>

6.4 编写second.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>一点教程网-jsp:setProperty和jsp:getProperty动作标签</title>
</head>
<body>

<jsp:useBean id="u" class="com.yiidian.User" scope="session"></jsp:useBean>
<jsp:setProperty property="*" name="u"/>

结果是:<br>
<jsp:getProperty property="name" name="u"/><br>
<jsp:getProperty property="password" name="u"/><br>
<jsp:getProperty property="email" name="u" /><br>


</body>
</html>

6.5 运行测试


7 在jsp:setProperty中使用变量

在某些情况下,您可能会从数据库中获得一些数据,要设置到Bean对象的属性中,在这种情况下,您需要使用JSP表达式。如下:

<jsp:useBean id="u" class="com.yiidian.User"></jsp:useBean>  
<%  
String name="eric";  
%>  
<jsp:setProperty property="name" name="u" value="<%=name %>"/>  
  
结果是:<br>  
<jsp:getProperty property="name" name="u"/><br>  

 

推荐好课