jsp页面显示对象的属性三种方法

2025-10-31 16:43:54

1、假定项目已在运行状态,创建名称为Product的类,该类是封装商品对象的JavaBean,在Product类中创建商品属性,并提供Get方法.(创建单个实体类)

public class Produce {   

         private String name="钢琴";  

         private double price=40000.56; 

         private int count=100; 

         private String factoryAdd="江苏省";  

       public String getName() {    

             return name;  

          } 

      public double getPrice() {  

             return price;   

        } 

      public int getCount() {      

             return count; 

        } 

       public String getFactoryAdd() {       

        return factoryAdd; 

    } 

 }

2、在JSP页面中获取商品JavaBean(单个实体类)中的属性信息,该操作通过JSP动作标签进行获取。(注意jsp标签要引入实体类的位置)

<jsp:useBean id="Produce" class="com.cyt.bean.Produce"></jsp:useBean>

<div>

<ul>

<li>商品名称:<jsp:getProperty property="name" name="produce"/</li><li> 价格:<jsp:getProperty property="price" name="produce"/></li>

<li> 数量:<jsp:getProperty property="count" name="produce"/></li>

<li>厂址:<jsp:getProperty property="factoryAdd" name="produce"/></li> 

</ul>

</div>

3、jsp页面显示List中对象的属性三种方法:注意不要忘了jsp标签的引用:

<-- 方法一 for循环迭代-->

<html>

<head>

<title>My JSP</title>

</head>

<body>

<table width="80%" border="1" align="center">

      <tr>

        <th>id</th>

        <th>name</th>

        <th>password</th>

        <th>email</th> 

        <th>hiredate</th>

        <th>salary</th>

        <th>grade</th>

        <th>department</th>

</tr>

      <%  

                       List list=(List)request.getAttribute("empList");

                       for(int i=0;i<list.size();i++){

                       Employee e=(Employee)list.get(i);

      %>

            <tr>

              <th><%=e.getId() %></th>

              <th><%=e.getName() %></th>

              <th><%=e.getPassword() %></th>

              <th><%=e.getEmail() %></th>

              <th><%=e.getHiredate() %></th>

              <th><%=e.getSalary() %></th>

              <th><%=e.getGrade() %></th>

              <th><%=e.getDep() %></th>

              </tr>            

     <%              

           }

      %>

</table>

</body>

</html>

4、<!--方法2:S标签  -->

<html>

<head>

<title>My JSP</title>

</head>

<body>

<table width="80%" border="1" align="center">

      <tr>

        <th>id</th>

        <th>name</th>

        <th>password</th>

        <th>email</th> 

        <th>hiredate</th>

        <th>salary</th>

        <th>grade</th>

        <th>department</th><th>删除用户</th>

</tr>

<s:iterator value="#request.empList" id="e">

          <tr>

              <th><s:property value="#e.id"/></th>

              <th><s:property value="#e.name"/></th>

              <th><s:property value="#e.password"/></th>

              <th><s:property value="#e.email"/></th>

              <th><s:property value="#e.hiredate"/></th>

              <th><s:property value="#e.salary"/></th>

              <th><s:property value="#e.grade"/></th>

              <th><s:property value="#e.dep.name"/></th>

               <th><a href="#">删除用户</a></th>

              </tr> 

 </s:iterator> 

</table>

</body>

</html>

5、<!--方法3:c标签  -->

<html>

<head>

<title>My JSP</title>

</head>

<body>

<table width="80%" border="1" align="center">

      <tr>

        <th>id</th>

        <th>name</th>

        <th>password</th>

        <th>email</th> 

        <th>hiredate</th>

        <th>salary</th>

        <th>grade</th>

        <th>department</th><th>删除用户</th>

</tr>

 <c:forEach items="${empList}" var="e">

      <tr>

              <th>${e.id}</th>

              <th>${e.name}</th>

              <th>${e.password}</th>

              <th>${e.email}</th>

              <th>${e.hiredate}</th>

              <th>${e.salary}</th>

              <th>${e.grade}</th>

              <th>${e.dep.name}</th>

               <th><a href="#">删除用户</a></th>

       </tr>   

     </c:forEach>

</table>

</body>

</html>

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢