JSP用JAVABEAN传整型值时,如果数值为一位,接收到的值为0,两位以上的值正常。

现象如题,环境为MYECLIPSE和TOMCAT6,代码如下:
提交数据的文件department_add.jsp:
<%@ page contentType="text/html" pageEncoding = "GBK"%>
<%@ page import="com.lb.teashop.factory.*,com.lb.teashop.vo.*"%>


连锁店信息维护

<% request.setCharacterEncoding("GBK"); //解决中文乱码问题 %>


添加连锁店


<%
String id = request.getParameter("id");
if(!(null == id || "".equals(id))){ //指定code时,显示对应信息
try{
DepartmentBean dpt=DAOFactory.getDepartmentDAOInstance().findDepartmentById(Integer.parseInt(id));
//out.println("dgc_code:" + dpt.getName());
department = dpt;
//out.println("department_id:" + department.getId());
%>











店名:
<%=department.getAddress()%>

<%=department.getErrorMsg("errAddress")%>
电话:


<%
}catch(Exception e){
e.printStackTrace();
}
}else{ //未指定code时,显示空白录入界面。
%>


























连锁店名称:

<%=department.getErrorMsg("errName")%>
拼音码:


<%=department.getErrorMsg("errPy_code")%>
地址:


<%=department.getErrorMsg("errPy_code")%>
电话:


<%=department.getErrorMsg("errPhone")%>
传真:


<%=department.getErrorMsg("errFax")%>


<%
}
%>

JAVABEAN文件DepartmentBean.java:
/**

  • */ package com.lb.teashop.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**

  • @author Administrator
    *
    /
    public class DepartmentBean implements Serializable{
    /
    *

    • */ private static final long serialVersionUID = 1L; private int id; private String name; private String py_code; private String address; private String phone; private String fax; private int apply_page_sn; private int page_sn;

    private Maperrors=null; //声明一个保存全部错误信息的Map集合

    public DepartmentBean(){
    //this.code="";
    this.name="";
    this.py_code="";
    this.errors = new HashMap(); //实例化Map对象,保存错误信息
    }

    public boolean isValidate(){ //数据验证操作
    boolean flag = true;
    // //System.out.println(this.name);
    // //System.out.println(this.comment);
    // if(this.name==null || "".equals(this.name)){
    // flag = false;
    // //System.out.print(this.name);
    // errors.put("errName", "请输入商品类别名称。"); //保存错误信息
    // }
    // if(this.py_code==null || "".equals(this.py_code)){
    // flag = false;
    // errors.put("errComment", "请输入规格描述,长度不超过150个汉字。");
    // }else if(this.py_code.length()>15){
    // flag = false;
    // //System.out.println(this.specification.length());
    // errors.put("errComment", "规格描述是不超过150个字的中文。");
    // }
    return flag;
    }
    public String getErrorMsg(String key){
    String value = this.errors.get(key);
    return value == null?"":value; //返回value对应的内容
    }

    /**

    • @return the id */ public int getId() { return id; }

    /**

    • @param id the id to set */ public void setId(int id) { this.id = id; }

    /**

    • @return the name */ public String getName() { return name; }

    /**

    • @param name the name to set */ public void setName(String name) { this.name = name; }

    /**

    • @return the py_code */ public String getPy_code() { return py_code; }

    /**

    • @param py_code the py_code to set */ public void setPy_code(String py_code) { this.py_code = py_code; }

    /**

    • @return the address */ public String getAddress() { return address; }

    /**

    • @param address the address to set */ public void setAddress(String address) { this.address = address; }

    /**

    • @return the phone */ public String getPhone() { return phone; }

    /**

    • @param phone the phone to set */ public void setPhone(String phone) { this.phone = phone; }

    /**

    • @return the fax */ public String getFax() { return fax; }

    /**

    • @param fax the fax to set */ public void setFax(String fax) { this.fax = fax; }

    /**

    • @return the apply_page_sn */ public int getApply_page_sn() { return apply_page_sn; }

    /**

    • @param apply_page_sn the apply_page_sn to set */ public void setApply_page_sn(int apply_page_sn) { this.apply_page_sn = apply_page_sn; }

    /**

    • @return the page_sn */ public int getPage_no() { return page_sn; }

    /**

    • @param page_sn the page_sn to set */ public void setPage_no(int page_sn) { this.page_sn = page_sn; }

}

接收数据的文件department_update_do.jsp:
<%@ page contentType="text/html" pageEncoding = "GBK"%>
<%@ page import="com.lb.teashop.factory.*" %>
/jsp:useBean
/jsp:setProperty

连锁店信息维护
<% request.setCharacterEncoding("GBK"); //解决中文乱码问题 %>
添加连锁店

<%=department.getId() %>

<%=department.getName() %>

<%=department.getAddress() %>

<%=department.getPhone() %>

<%=department.getFax() %>

<%=department.getApply_page_sn() %>

<%
if(department.isValidate()){
try{
if(DAOFactory.getDepartmentDAOInstance().doUpdateDepartment(department)>0 ){
%>

商品信息修改成功!


<%
response.setHeader("refresh","2;URL=department_list.jsp");
}else{
%>

商品信息修改失败!


<%
response.setHeader("refresh","2;URL=department_add.jsp?id="+department.getId());
}
}catch(Exception e){
e.printStackTrace(); //在Tomcat后台打印
}
}else{
%>
/jsp:forward
<%
//response.setHeader("refresh","2;URL=department_add.jsp");
}
%>

全部的整型变量如果小于10,接收文件看到的都为0,大于等于10的都没问题。求大牛指点迷津!非常感谢!

该回答引用ChatGPT

在提交数据的文件department_add.jsp中,使用了JSP表达式<%=department.getAddress()%>来输出DepartmentBean中的address属性,其中DepartmentBean是一个JavaBean对象。在JavaBean对象中,address属性的类型为String,在JSP页面中也以字符串的形式展示。然而,这个问题描述中提到的问题是:当传递的整型值是一位数时,接收到的值为0。这个问题看起来与address属性类型为String没有关系,而与传递整型值有关。


在department_add.jsp中,使用了以下代码将传递的id参数转换为整型值,并将其赋值给DepartmentBean对象中的id属性:

String id = request.getParameter("id");
if(!(null == id || "".equals(id))){
    try{
        DepartmentBean dpt = DAOFactory.getDepartmentDAOInstance().findDepartmentById(Integer.parseInt(id));
        department = dpt;
    }catch(Exception e){
        e.printStackTrace();
    }
}else{
    //...
}

这里将id参数转换为整型值的方式是通过Integer.parseInt(id)来实现的。当传递的id参数是一位数时,Integer.parseInt(id)的返回值为0。因此,当传递的id参数是一位数时,DepartmentBean对象中的id属性会被设置为0。


在department_update_do.jsp中,使用以下代码输出DepartmentBean对象中的id属性:

<%=department.getId() %>

由于department.getId()返回的是整型值,因此如果id属性被设置为0,那么输出的就是0。


解决方案:

为了解决这个问题,可以使用字符串类型来表示id属性。在DepartmentBean中,将id属性的类型从int改为String:

public class DepartmentBean implements Serializable{
    private static final long serialVersionUID = 1L;
    private String id;
    //...
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    //...
}

然后,在department_add.jsp中,将接收到的id参数直接赋值给DepartmentBean对象中的id属性即可:

String id = request.getParameter("id");
if(!(null == id || "".equals(id))){
    try{
        DepartmentBean dpt = DAOFactory.getDepartmentDAOInstance().findDepartmentById(id);
        department = dpt;
    }catch(Exception e){
        e.printStackTrace();
    }
}else{
    //...
}

在department_update_do.jsp中,输出DepartmentBean对象中的id属性时,也需要将其转换为字符串类型:

<%=String.valueOf(department.getId()) %>

这样,无论传递的id参数是几位数,都可以正确地接收并输出了。