EL表达式详情,遍历对象,集合和map例子

来源:互联网 发布:淘宝助理报告错误 编辑:程序博客网 时间:2024/05/16 17:09
问题:为什么会有el表达式?目的就是为了代替,jsp表达式<%= %>。向浏览器输出域对象中的值。jsp的核心语法: jsp表达式 <%=%>和 jsp脚本<%  %>。        以后开发jsp的原则: 尽量在jsp页面中少写甚至el不写java代码。        使用EL表达式替换掉jsp表达式        EL表达式作用: 向浏览器输出域对象中的变量值或表达式计算的结果!!!        El表达式的语法:${域对象中的变量名称}问题1: el表达式的语法和原理?语法1:El表达式 ${ 域对象中的变量名 }原理:${ 域对象中的变量名 } 会被翻译成  pageContext.findAttribute(域对象变量名); 语法2:指定域取出数据。数据保存在哪个域:  <%pageContext.setAttribute("name","kkkkkkk",pageContext.REQUEST_SCOPE);%>     就从哪个域中取数据:    El表达式:  ${requestScope.name}等价于    <%=pageContext.getAttribute("name",pageContext.REQUEST_SCOPE) %>问题2:el调用对象的get方法输出属性值? <%        //获取对象        student stud = new student("kk",23,"id777");        //将对象保存到page域当中        pageContext.setAttribute("student",stud);  %>问题3:el如何输出对象的属性值?例1:el代码: ${student} 等价于Java脚本代码: <%= pageContext.getAttribute("student",pageContext.PAGE_SCOPE) %>  例2:el代码:${student.name}<br/>${student.age}<br/>${student.id}<br/>等价于Java脚本代码:<% student studs=(student)pageContext.getAttribute("student",pageContext.PAGE_SCOPE);studs.getName();studs.getAge();studs.getId();%>  问题:student.name为什么可以输出对象的属性值?Student.name,中的点不能调用student类私有属性,而是调用getName()方法。为什么说调用的是getXX方法而不是直接调用的属性呢?例如:将student类中属性name改为username。而getName()方法名称没有改成getUsername()。El还是${student.name},这里的name是getName()方法名称。而不是属性名称。例子1:获取对象的属性值?//获取对象        student stud = new student("kk",23,"id777");        //将对象保存到page域当中        pageContext.setAttribute("student",stud);   <!-- 获取对象,并且输出 -->     ${student.name}     ${student.age}     ${student.id}    等价于下面:    <%         student studs = (student)pageContext.getAttribute("student",pageContext.PAGE_SCOPE);        out.write(studs.getName());        out.write(""+studs.getAge());        out.write(studs.getId());     %> 例子2:获取集合?//集合        List<student> list = new ArrayList();        list.add(new student("jia",24,"id333"));        list.add(new student("cui",24,"id444"));        list.add(new student("kun",24,"id555"));        //将集合保存到page域当中        pageContext.setAttribute("list", list,pageContext.REQUEST_SCOPE); <!-- 获取集合,并且输出 -->  ${list[0].name}      ${list[1].name}      ${list[2].name}等价于List<student> lis =(List)pageContext.getAttribute("list",pageContext.REQUEST_SCOPE);         student stu = lis.get(0);         student stu01 = lis.get(1);         out.write(stu.getName());         out.write(stu01.getName());代码的详情:list 等价于List<student> lis = (List)pageContext.getAttribute("list",pageContext.REQUEST_SCOPE);list[0] 等价于List<student> lis = (List)pageContext.getAttribute("list",pageContext.REQUEST_SCOPE);student stu = lis.get(0); list[0].name等价于List<student> lis = (List)pageContext.getAttribute("list",pageContext.REQUEST_SCOPE);student stu = lis.get(0);stu.getName()例子3:获取map?    //map        Map<String,student> map = new HashMap<String,student>();        map.put("101",new student("cui",10,"id999"));        map.put("102",new student("jia",23,"id888"));        map.put("103",new student("wang",24,"id777"));        //将map保存到page域当中        pageContext.setAttribute("map",map); <!-- map对象中获取数据 -->       ${map['101'].name}       ${map['102'].name}       ${map['103'].name}      等价于:      <%        Map<String,student> maps = (Map)pageContext.getAttribute("map");        student studd = (student)maps.get("101");        out.write(studd.getName());       %>例子4:算数运算和比较运算,逻辑运算,判断null或空字符串?    <%         String name = "ss";        pageContext.setAttribute("name", name);     %>    ${10+20} <br/>    ${2*8} <br/>    ${3<4} <br/>    ${true && false} <br/>${true || false} <br/>Empty name 就能判断变量name是否是null和空字符串。    ${name==null || name==""}    ${empty name}
0 0
原创粉丝点击