使用JSTL在JSP中获取值时获取NumberFormatException [英] Getting NumberFormatException while getting the value in JSP using JSTL

查看:78
本文介绍了使用JSTL在JSP中获取值时获取NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Spring/Hibernate示例Web应用程序.实际上,我正在尝试从数据库中加载员工.在这种情况下,在从员工和地址表的数据库中获取数据时,我遇到了NumberFormat异常.

I am working on Spring/Hibernate sample web application. Actually, I am trying to load the employees from database. In this case, while getting the data from database for both employee and address tables i am getting the NumberFormat exception.

以下是我正在处理的代码,

Following is the code i am working on,

JSP代码:

      <c:if  test="${!empty employeeList}">
        <table class="data">
<c:forEach items="${employeeList}" var="emp">
    <tr>
        <td><c:out value="${emp.firstname}" /></td>
        <td><c:out value="${emp.lastname}" /></td>
        <td><c:out value="${emp.email}" /></td>
        <td><a href="edit/${emp.id}">Edit</a></td>
        <td><a href="delete/${emp.id}">Delete</a></td>
    </tr>
</c:forEach>

控制器代码:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String listEmployees(ModelMap map) 
{
    map.addAttribute("employeeList", employeeManager.getAllEmployees());
    return "editEmployeeList";
}

服务层代码:

 @Override
@Transactional
public List<EmployeeEnitity> getAllEmployees() {
    return employeeDAO.getAllEmployees();
}


public List<EmployeeEntity> getAllEmployees() {
    return this.sessionFactory.getCurrentSession().createQuery("select ee.firstname,ee.lastname,addr.email from " +
            "com.howtodoinjava.entity.EmployeeEntity ee, com.howtodoinjava.entity.AddressEntity addr where ee.id=addr.id").list();

}

请帮助我解决此异常

推荐答案

The return type of session.createQuery(String).list() in your service method getAllEmployees is List<Object[]>, it is not List<Employee>

在控制器中,您正在以下行中将此List<Object[]>添加到模型中:

In controller you are adding this List<Object[]> to your model at this line:

map.addAttribute("employeeList",employeeList);

现在在JSP中,您正尝试在JSTL forEach循环中访问模型对象:

Now in JSP you are trying to access the model object in JSTL forEach loop:

<c:forEach items="${employeeList}" var="emp">

由于employeeList表示List<Object[]>,变量emp表示Object[],所以不是Employee.现在在变量emp上使用dot (.)运算符意味着您正在尝试访问特定索引位置的元素.例如:

As employeeList represents List<Object[]>, the variable emp represents Object[], it is not Employee. Now using dot (.) operator on variable emp means you are trying to access an element at a particular index position. For example:

emp.0 --> is same as emp[0]
emp.1 --> is same as emp[1]
emp.indexPosition --> is same as emp[indexPosition]

因此,当您说emp.firstName时,firstName会转换为整数,因为firstName不是整数,您会得到NumberFormatException

So when you say emp.firstName, then the firstName is converted to integer, as firstName is not an integer you are getting NumberFormatException

这篇关于使用JSTL在JSP中获取值时获取NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆