这个页面我用了两个usebean动作,product1使用setproperty设置成员,并把product1赋给product
但是在下面获取时,使用
<jsp:getProperty property="productId" name="product" />
获取不到product中的内容,但是用
product.getProductId()
可以获取到product的内容。
将
<jsp:getProperty property="productId" name="product" />
name改为product1 可以获取product1的内容。
还是说只有用setproperty 赋值的元素 才能用 getproperty获取到?
后面贴代码
刚才又测试了一下,用普通的set方法将product1的name 属性赋值,在后面使用
<jsp:getProperty property="productName" name="product1" />
依然能获取,说明并不是只有setproperty的才能getproperty
下面是修改之前的。
<%@page import="com.xiajun.Product.Product"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="product" class="com.xiajun.Product.Product"/>
<%
String action = request.getParameter("action");
if(action!=null&&action.equals("add")){
%>
<jsp:useBean id="product1" class="com.xiajun.Product.Product" />
<jsp:setProperty property="productId" name="product1" value="<%=request.getParameter(\"productId\") %>"/>
<% product1.setProductName(request.getParameter("productName")); %>
<%--
<jsp:setProperty property="productName" name="product1" value="<%=request.getParameter(\"productName\") %>"/>
--%>
<jsp:setProperty property="productPrice" name="product1" value="<%=request.getParameter(\"productPrice\") %>"/>
<jsp:setProperty property="productStock" name="product1" value="<%=request.getParameter(\"productStock\") %>"/>
<jsp:setProperty property="productInDate" name="product1" value="<%=new Date().toLocaleString() %>"/>
<%
request.setAttribute("phone", product1);
product = (Product) request.getAttribute("phone");
}
if(action!=null&&action.equals("del")){
request.removeAttribute("phone");
product = new Product();
}
%>
<html>
<head>
<style>
.listing td,.listing th{
cellspacing:0;
border:red 2px solid;
text-align:center;
}
</style>
</head>
<body>
<table class="listing">
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>当前库存</th>
<th>入库时间</th>
<th>操作</th>
</tr>
<tr>
<td><jsp:getProperty property="productId" name="product" /><%=product.getProductId() %></td>
<td><jsp:getProperty property="productName" name="product" /></td>
<td><jsp:getProperty property="productPrice" name="product" /></td>
<td><jsp:getProperty property="productStock" name="product"/></td>
<td><jsp:getProperty property="productInDate" name="product"/></td>
<td><a href="del.jsp?action=del&id=<jsp:getProperty property="productId" name="product" />">删除</a></td>
</tr>
</table>
</body>
</html>