日期:2014-05-19  浏览次数:20707 次

jsp遇到问题,搞了一个早上。求解?
java Bean里面的代码   问题  谢谢大家啊   为什么jsp里面用param ="discount" 就没错  用value="<%= request.getParameter("discount")%>"   就会报异常rg.apache.jasper.JasperException: An exception occurred processing JSP page /bean/testproperty.jsp  搞了一个早上发现用param才行  

package Bean;

public class SaleEntry {
private double discount = 1.0;
private String productname = "unknown";
private int number = 0;
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
if(productname!=null){
this.productname = productname;
} else {
this.productname = "unknown";
}

}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double productcost(){
double cost;
if(productname.equals("coffee")){
cost = 13.14*this.getDiscount();
} else{
cost = 0;
}
return roundmoney(cost);
}
public double roundmoney(double cost){
return Math.floor((cost*100)/100.0);//将小数点后面的内容给去掉
}
public double gettotalcost(){
return (this.productcost()*this.getNumber());
}
}
jsp代码   


<html>
<head>
<title>jsp set property</title>
</head>
<body>
<jsp:useBean id="se" class="Bean.SaleEntry"/>
<jsp:setProperty 
name="se"
property="productname"
value="<%= request.getParameter("productname")%>"/>
<jsp:setProperty 
name="se"
property="discount"
param ="discount" />
    <!--value="<%= request.getParameter("discount")%>" />-->


<table align="center" border="2">

<tr >
<th>product name <th> number <th> discount <th> totalcost
</tr>
<td><jsp:getProperty name="se" property="productname" />
</tr>
</table>
</body>
</html>

------解决方案--------------------
value="<%= request.getParameter("discount")%>"
使用这个的话那这个参数是从哪个页面跳转过来的呢?

------解决方案--------------------
jsp:useBean 标签中用param的话不需要自己做类型转换,用value的话貌似需要自己做类型转换,
即将request.getParameter("discount"))返回的字符串类型转换成double类型即可:
value="<%= request.getParameter("discount")%>"改写成
value="<%=Double.parseDouble(request.getParameter(\"discount\"))%>" ,我这边实验通过了,希望对您有帮助!