EL 与 ognl 的区别

JavaHelloWorld_1 2013-09-29 02:33:53
介绍功能背景:SSH框架 ,在JSP中取出session中的List<Map<String,String>> ,然后遍历。并用checkbox控件标记每一行,提交时,把一行中的courseId 提交到Action。

ognl代码:
<s:iterator value="#session['LoadingCourseList']" id="mapList">
<tr>
<td><input name="checkBox" id="checkBox" value="#mapList.get('courseId')" type="checkbox"> </td>

<td><s:property value="#mapList.get('courseId')"/></td>
<td><s:property value="#mapList.get('courseName')"/></td>
<td><s:property value="#mapList.get('teacherName')"/></td>
<td><s:property value="#mapList.get('courseAddress')"/></td>
<td><s:property value="#mapList.get('courseTime')"/></td>
</tr>
</s:iterator>

这样提交的话,生成的_jsp.java文件如下
out.write("\r\n");
out.write("\t\t \t<tr>\r\n");
out.write("\t\t \t\t<td><input name=\"checkBox\" id=\"checkBox\" value=\"#mapList.get('courseId')\" type=\"checkbox\"> </td>\r\n");
out.write("\t\t \t\t\r\n");
out.write("\t\t \t\t<td>");

如此可加,Action中得到的就是 "#mapList.get('courseId')"这个字符串,没有解析

而换成EL则可以解析,并且提交到Action中
jap中代码如下:
<s:iterator value="#session['LoadingCourseList']" id="mapList" var="mapList">
<tr>
<td><input name="checkBox" id="checkBox" value=${mapList.courseId} type="checkbox"> </td>
<td><s:property value="#mapList.get('courseId')"/></td>
<td><s:property value="#mapList.get('courseName')"/></td>
<td><s:property value="#mapList.get('teacherName')"/></td>
<td><s:property value="#mapList.get('courseAddress')"/></td>
<td><s:property value="#mapList.get('courseTime')"/></td>
</tr>
</s:iterator>

生成的_jap.java文件如下
out.write("\r\n");
out.write("\t\t \t<tr>\r\n");
out.write("\t\t \t\t<td><input name=\"checkBox\" id=\"checkBox\" value=");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${mapList.courseId}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));
out.write(" type=\"checkbox\"> </td>\r\n");
out.write("\t\t \t\t\r\n");
out.write("\t\t \t\t<td>");

我想问的是~为什么会出现这种区别,使用ognl这样为什么不能解析呢???求大神解答
...全文
277 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zswiori 2013-09-29
  • 打赏
  • 举报
回复


上面是页面内容显示,下面是源文件
zswiori 2013-09-29
  • 打赏
  • 举报
回复

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>TEST PAGE</title>
	</head>
	<body>
		<%
			List<Map<String,String>> loadingCourseList = new ArrayList<Map<String,String>>();
			Map<String,String> map1 = new HashMap<String,String>();
			map1.put("a", "map1_1");
			map1.put("b", "map1_2");
			map1.put("c", "map1_3");
			Map<String,String> map2 = new HashMap<String,String>();
			map2.put("a", "map2_1");
			map2.put("b", "map2_2");
			map2.put("c", "map2_3");
			Map<String,String> map3 = new HashMap<String,String>();
			map3.put("a", "map3_1");
			map3.put("b", "map3_2");
			map3.put("c", "map3_3");
			
			loadingCourseList.add(map1);
			loadingCourseList.add(map2);
			loadingCourseList.add(map3);
			
			session.setAttribute("loadingCourseList", loadingCourseList);
		%>
		<table>
			<s:iterator value="#session.loadingCourseList" id="mapList">
	    		<tr>
					<td><input name="checkBox" id="checkBox" value="<s:property value="#mapList.get(\"a\")"/>" type="checkbox"> </td>
					<td><s:property value="#mapList.get(\"a\")"/></td>
					<td><s:property value="#mapList.get(\"b\")"/></td>
					<td><s:property value="#mapList.get(\"c\")"/></td>
			     </tr>
			</s:iterator>
		</table>
	</body>
</html>
给你一个极其完成的,输出如下:
zswiori 2013-09-29
  • 打赏
  • 举报
回复

<table>
	<s:iterator value="#session.loadingCourseList" id="mapList">
   		<tr>
			<td><input name="checkBox" id="checkBox" value="<s:property value="#mapList.get(\"courseId\")"/>" type="checkbox"> </td>
			<td><s:property value="#mapList.get(\"courseId\")"/></td>
			<td><s:property value="#mapList.get(\"courseName\")"/></td>
			<td><s:property value="#mapList.get(\"teacherName\")"/></td>
	     </tr>
	</s:iterator>
</table>
非要我写那么完成。。。。
JavaHelloWorld_1 2013-09-29
  • 打赏
  • 举报
回复
引用 9 楼 zswiori 的回复:
<s:property value="#mapList.get(\"courseId\")"/> 看清楚,是双引号,不是单引号!
双引也试过~~结果得到#mapList.get(\"
zswiori 2013-09-29
  • 打赏
  • 举报
回复
<s:property value="#mapList.get(\"courseId\")"/> 看清楚,是双引号,不是单引号!
JavaHelloWorld_1 2013-09-29
  • 打赏
  • 举报
回复
引用 5 楼 zswiori 的回复:
<s:property value="#mapList.get(\"courseId\")"/>
引用 6 楼 breaking236 的回复:
value的值#mapList.get('courseId'),解析到单引号时解析就结束了,所以解析会出问题,转义试试#mapList.get(\'courseId\'),ognl并不是很好用,一般用el和jstl
引用 7 楼 u011972917 的回复:
引用 5 楼 zswiori 的回复:
<s:property value="#mapList.get(\"courseId\")"/>
应该是这样的
楼上几位都说改成如下 <input name="checkBox" id="checkBox" value="#mapList.get(\'courseId\')" type="checkbox"> 但是我试了试还是不可 编译后的_jsp.java代码如下 out.write("\r\n"); out.write("\t\t \t<tr>\r\n"); out.write("\t\t \t\t<td><input name=\"checkBox\" id=\"checkBox\" value=\"#mapList.get(\\'courseId\\')\" type=\"checkbox\"> </td>\r\n"); out.write("\t\t \t\t\r\n"); out.write("\t\t \t\t<td>"); 我在想ognl是不是在<input >标签中有限制~~
江城DiorsMan 2013-09-29
  • 打赏
  • 举报
回复
引用 5 楼 zswiori 的回复:
<s:property value="#mapList.get(\"courseId\")"/>
应该是这样的
breaking236 2013-09-29
  • 打赏
  • 举报
回复
value的值#mapList.get('courseId'),解析到单引号时解析就结束了,所以解析会出问题,转义试试#mapList.get(\'courseId\'),ognl并不是很好用,一般用el和jstl
zswiori 2013-09-29
  • 打赏
  • 举报
回复
<s:property value="#mapList.get(\"courseId\")"/>
zswiori 2013-09-29
  • 打赏
  • 举报
回复
哦,不好意思看错了,value=\"#mapList.get('courseId')\"这段是编译过的 不是你写的,你写的改成
开发者_android 2013-09-29
  • 打赏
  • 举报
回复
同楼上所说的,改成value=\"%{mapList.get('courseId')}\"
JavaHelloWorld_1 2013-09-29
  • 打赏
  • 举报
回复
引用 1 楼 zswiori 的回复:
value=\"#mapList.get('courseId')\" 改成value=\"%{mapList.get('courseId')}\"
不行啊~~还是得到%{mapList.get('courseId')}。。。。。
zswiori 2013-09-29
  • 打赏
  • 举报
回复
value=\"#mapList.get('courseId')\" 改成value=\"%{mapList.get('courseId')}\"

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧