⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 struts 2中的ognl - max on java - blogjava.htm

📁 struts2标签的详细解释说明教程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0058)http://www.blogjava.net/max/archive/2007/04/28/114417.html -->
<HTML><HEAD id=Head><TITLE>Struts 2中的OGNL - Max On Java - BlogJava</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META id=metaKeywords content="Struts 2中的OGNL" name=keywords><LINK id=MainCss 
href="Struts 2中的OGNL - Max On Java - BlogJava.files/style.css" type=text/css 
rel=stylesheet><LINK id=RSSLink title=RSS 
href="http://www.blogjava.net/max/rss.aspx" type=application/rss+xml 
rel=alternate>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY>
<FORM id=Form1 name=Form1 onsubmit="javascript:return WebForm_OnSubmit();" 
action=114417.html method=post>
<DIV><INPUT id=__EVENTTARGET type=hidden name=__EVENTTARGET> <INPUT 
id=__EVENTARGUMENT type=hidden name=__EVENTARGUMENT> <INPUT 
id="&#13;&#10;__VIEWSTATE" type=hidden name=__VIEWSTATE> </DIV>
<SCRIPT type=text/javascript>
<!--
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</SCRIPT>

<SCRIPT src="Struts 2中的OGNL - Max On Java - BlogJava.files/WebResource.axd" 
type=text/javascript></SCRIPT>

<SCRIPT language=JavaScript>
									function ctlent(evt,id)
											{
												if(evt.ctrlKey && evt.keyCode == 13)
												{	
													try
													{
														TempSave(id);
													}
													catch(ex)
													{
													}
													finally
													{
													    __doPostBack('AjaxHolder$PostComment$btnSubmit','')
													}
												}
		
												}</SCRIPT>

<SCRIPT language=JavaScript>function SetReplyAuhor(author){document.getElementById('AjaxHolder_PostComment_tbComment').value+="@"+author+"\n";document.getElementById('AjaxHolder_PostComment_tbComment').focus();return false}</SCRIPT>

<SCRIPT src="Struts 2中的OGNL - Max On Java - BlogJava.files/ScriptResource.axd" 
type=text/javascript></SCRIPT>

<SCRIPT 
src="D:\C_TOOLS\tools\勉強の工具\sql2000 sp4\struts-2.0.9-all\Struts 2中的OGNL - Max On Java - BlogJava.files\ScriptResource(1).axd" 
type=text/javascript></SCRIPT>

<SCRIPT 
src="D:\C_TOOLS\tools\勉強の工具\sql2000 sp4\struts-2.0.9-all\Struts 2中的OGNL - Max On Java - BlogJava.files\ScriptResource(2).axd" 
type=text/javascript></SCRIPT>

<SCRIPT type=text/javascript>
<!--
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
// -->
</SCRIPT>
<!--done-->
<DIV id=header><!--done-->
<DIV class=header>
<DIV class=headerText><A class=headermaintitle id=Header1_HeaderTitle 
href="http://www.blogjava.net/max/">Max On Java</A><BR>The value of hands-on 
experience as compared to book learning is smaller in software development than 
in many other fields. </DIV></DIV></DIV>
<DIV id=mytopmenu>
<DIV id=mylinks><!--done--><A class=menu id=MyLinks1_HomeLink 
href="http://www.blogjava.net/">BlogJava</A>&nbsp;&nbsp; <A class=menu 
id=MyLinks1_MyHomeLink href="http://www.blogjava.net/max/">首页</A>&nbsp;&nbsp; <A 
class=menu id=MyLinks1_NewPostLink 
href="http://www.blogjava.net/max/admin/EditPosts.aspx?opt=1">新随笔</A> <A 
class=menu id=MyLinks1_ContactLink 
href="http://www.blogjava.net/max/contact.aspx?id=1">联系</A>&nbsp;&nbsp; <A 
class=menu id=MyLinks1_Syndication 
href="http://www.blogjava.net/max/rss">聚合</A><A id=MyLinks1_XMLLink 
href="http://www.blogjava.net/max/rss"><IMG 
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" 
src="Struts 2中的OGNL - Max On Java - BlogJava.files/xml.gif"></A>&nbsp;&nbsp; <A 
class=menu id=MyLinks1_Admin 
href="http://www.blogjava.net/max/admin/EditPosts.aspx">管理</A> </DIV>
<DIV id=mystats><!--done-->随笔-18&nbsp; 评论-753&nbsp; 文章-0&nbsp; trackbacks-0 
</DIV></DIV>
<DIV id=centercontent><!--done-->
<DIV class=post>
<DIV class=postTitle><A class=postTitle2 id=viewpost1_TitleUrl 
href="http://www.blogjava.net/max/archive/2007/04/28/114417.html">Struts 
2中的OGNL</A> </DIV>
<P>本人是一个EL(Expression Language,以下译为表达式语言)的支持者。因为我对&lt;% 
%&gt;写法极为反感,忘记了在那本书上看到的一句话——“使用标志(Tag)的一个目的就是避免在JSP页面中出现过多的&lt;%%&gt;的语句,使页面与后台代码分离。”</P>
<P>表达式语言主要有以下几大好处:</P>
<OL>
  <LI>避免<SPAN style="BACKGROUND-COLOR: lightgrey">(MyType) 
  request.getAttribute()</SPAN>和<SPAN 
  style="BACKGROUND-COLOR: lightgrey">myBean.getMyProperty()</SPAN>之类的语句,使页面更简洁; 

  <LI>支持运算符(如+-*/),比普通的标志具有更高的自由度和更强的功能; 
  <LI>简单明了地表达代码逻辑,使用代码更可读与便于维护。 </LI></OL>
<H2>Struts 2中的表达式语言</H2>
<P>Struts 2支持以下几种表达式语言:</P>
<OL>
  <LI>OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言; 
  <LI>JSTL(JSP Standard Tag Library),JSP 2.0集成的标准的表达式语言; 
  <LI>Groovy,基于Java平台的动态语言,它具有时下比较流行的动态语言(如Python、Ruby和Smarttalk等)的一些起特性; 
  <LI>Velocity,严格来说不是表达式语言,它是一种基于Java的模板匹配引擎,具说其性能要比JSP好。 </LI></OL>
<P>Struts 2默认的表达式语言是OGNL,原因是它相对其它表达式语言具有下面几大优势:</P>
<OL>
  <LI>支持对象方法调用,如<SPAN 
  style="BACKGROUND-COLOR: lightgrey">xxx.doSomeSpecial()</SPAN>; 
  <LI>支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名 | &nbsp;值名],例如:<SPAN 
  style="BACKGROUND-COLOR: lightgrey">@java.lang.String@format('foo %s', 
  'bar')</SPAN>或<SPAN 
  style="BACKGROUND-COLOR: lightgrey">@tutorial.MyConstant@APP_NAME</SPAN>; 
  <LI>支持赋值操作和表达式串联,如<SPAN style="BACKGROUND-COLOR: lightgrey">price=100, 
  discount=0.8, calculatePrice()</SPAN>,这个表达式会返回80; 
  <LI>访问OGNL上下文(OGNL context)和ActionContext; 
  <LI>操作集合对象。 </LI></OL>
<H2>OGNL的用法</H2>
<P>OGNL是通常要结合Struts 2的标志一起使用,如<SPAN 
style="BACKGROUND-COLOR: lightgrey">&lt;s:property value="xx" 
/&gt;</SPAN>等。大家经常遇到的问题是#、%和$这三个符号的使用。下面我想通过例子讲述这个问题:</P>
<P>首先新建名为Struts2_OGNL的Web工程,配置开发环境。之前很多朋友在使用Struts 
2的过程中都遇到乱码问题。当然乱码问题由来已久,而且涉及多方面的知识,所以并非三言两语可以说明白,而且互联网上也已经有很多这方便的文章,大家可以Google一下。不过,如果你在开发的过程,多注意一下,避免乱码问题也不难。乱码多数是由于编码与解码所使用的方式不同造成的,所以我建议大家将编码方式都设为“utf-8”,如<SPAN 
style="BACKGROUND-COLOR: lightgrey">&lt;%@&nbsp; page language="java" 
contentType="text/html; charset=utf-8" pageEncoding="utf-8" 
%&gt;</SPAN>。另外,在配置web.xml时使用ActionContextCleanUp过滤器(Filter),如下面代码所示:</P>
<DIV 
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN 
style="COLOR: #0000ff">&lt;?</SPAN><SPAN style="COLOR: #ff00ff">xml 
version="1.0" encoding="UTF-8"</SPAN><SPAN 
style="COLOR: #0000ff">?&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">web-app 
</SPAN><SPAN style="COLOR: #ff0000">id</SPAN><SPAN 
style="COLOR: #0000ff">="WebApp_9"</SPAN><SPAN style="COLOR: #ff0000"> 
version</SPAN><SPAN style="COLOR: #0000ff">="2.4"</SPAN><SPAN 
style="COLOR: #ff0000"><BR>&nbsp; &nbsp; xmlns</SPAN><SPAN 
style="COLOR: #0000ff">="http://java.sun.com/xml/ns/j2ee"</SPAN><SPAN 
style="COLOR: #ff0000"><BR>&nbsp; &nbsp; xmlns:xsi</SPAN><SPAN 
style="COLOR: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</SPAN><SPAN 
style="COLOR: #ff0000"><BR>&nbsp; &nbsp; xsi:schemaLocation</SPAN><SPAN 
style="COLOR: #0000ff">="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR>&nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">display-name</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000">Struts 2 
OGNL</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">display-name</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp;&nbsp;<BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">filter</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; 
&nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">filter-name</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN 
style="COLOR: #000000">struts-cleanup</SPAN><SPAN 
style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">filter-name</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; 
&nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">filter-class</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; 
org.apache.struts2.dispatcher.ActionContextCleanUp<BR>&nbsp; &nbsp; &nbsp; 
&nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">filter-class</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; 
</SPAN><SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN 
style="COLOR: #800000">filter</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; 
&nbsp;&nbsp;<BR>&nbsp; &nbsp; </SPAN><SPAN 
style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">filter-mapping</SPAN><SPAN 
style="COLOR: #0000ff">&gt;</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp; &nbsp; 
&nbsp; &nbsp; </SPAN><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN 
style="COLOR: #800000">filter-name</SPAN><SPAN 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -