定制列表+ JSP + java.lang.NumberFormatException [英] Custom list + JSP + java.lang.NumberFormatException

查看:49
本文介绍了定制列表+ JSP + java.lang.NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现自定义JSP列表标记,但是在访问自定义列表对象的属性时遇到问题.在下面的示例中,访问test.jsp页上的List2name属性给出错误org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "name".如何解决呢?

I want to implement custom JSP list tag, but have problem with accessing properties of custom list object. With example like below accessing a name property of List2 on test.jsp page give an error org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "name". How to solve this ?

public class List2 extends ArrayList<String> {

    public String getName() {
        return "name";
    }
}

test.jsp

<%-- java.lang.NumberFormatException --%>
${list.name}

<%-- this works ok --%>
<c:forEach  items="${list}" var="item">
   ${item}  
</c:forEach>

编辑

整个test.jsp工作

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach  items="${list}" var="item">
   ${item}
</c:forEach>

整个test.jsp 正常工作

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
${list.name} 

TestController.java:

TestController.java:

@Controller
public class TestController {

    @ModelAttribute("list")
    public List2 testList() {
        List2 l = new List2();
        l.add("foo");
        l.add("bar");
        return l;
    }

    /* test.jsp */
    @RequestMapping("/test")
    public String test() {
        return "test";
    }
}

推荐答案

我认为这是由于JSP EL允许使用.[]访问对象属性.但是对于List实例,两者都有特殊含义:这意味着可以访问第n个元素.因此,您可以编写${list[2]}${list.2}.由于EL检测到您的对象是集合的实例,因此它将尝试将名称转换为数字,然后您会收到此异常.

I think it's due to the fact that the JSP EL allows using . or [] to access object properties. But both have a special meaning for List instances: it means access to the nth element. You may thus write ${list[2]} or ${list.2}. Since EL detects that your object is an instance of a collection, it tries to transform name into a number, and you get this exception.

请注意,这仅是对您得到的异常的解释.我还没有检查规格,以查看它是否是Tomcat的错误或预期的行为.

Note that this is only an explanation of the exception you get. I haven't checked the specification to see if it's a bug of Tomcat or if it's expected behavior.

您应该很少扩展ArrayList.在大多数情况下,最好使用委托,然后将列表包装在另一个对象中.您不能只提供以下内容吗?

You should very very rarely extend ArrayList. Most of the time, it's better to use delegation, and thus wrap the list inside another object. Couldn't you just have something like the following?

public class List2 {

    private List list;

    public String getName() {
        return "name";
    }

    public List getList() {
        return list;
    }
}

这篇关于定制列表+ JSP + java.lang.NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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