{"id":1875,"date":"2016-12-29T17:28:45","date_gmt":"2016-12-29T09:28:45","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1875"},"modified":"2017-05-05T11:28:32","modified_gmt":"2017-05-05T03:28:32","slug":"ognl%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1875","title":{"rendered":"OGNL\u8868\u8fbe\u5f0f"},"content":{"rendered":"<h2>\u4e00\u3001OGNL\u4ecb\u7ecd<\/h2>\n<p>     OGNL\u5168\u79f0\u662fObject-Graph Navigation Language(\u5bf9\u8c61\u56fe\u5bfc\u822a\u8bed\u8a00)\u662f\u4e00\u4e2a\u7528\u6765\u83b7\u53d6 \u548c\u8bbe\u7f6e java\u5bf9\u8c61\u5c5e\u6027\u7684\u8868\u8fbe\u5f0f\u8bed\u8a00\uff0c\u9700\u5728Struts2\u6807\u7b7e\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>    \u5728OGNL\u7684\u4f7f\u7528\u4e2d\uff0c\u9700\u8bbf\u95ee\u4e00\u7cfb\u5217\u5bf9\u8c61\uff0c\u8fd9\u4e9b\u5bf9\u8c61\u90fd\u5b58\u653e\u5728OGNL\u7684context\uff08\u4e0a\u4e0b\u6587\uff09\u4e2d\uff0ccontext\u662f\u4e00\u4e2aMap\u7ed3\u6784\uff0c<br \/>\n\u5b58\u653e\u4e86\u4e00\u7cfb\u5217\u7684key-value\u5bf9\uff0c\u5b9e\u9645\u4e0a\uff0c\u5b83\u548cActionContext\u7c7b\u662f\u76f8\u5bf9\u5e94\u7684\u3002<\/p>\n<p>    \u5728\u53d1\u9001Http\u8bf7\u6c42\u7ed9Struts2\u6846\u67b6\u540e\uff0cStruts2\u6846\u67b6\u4f1a\u521b\u5efaActionContext\u3001ValueStack\u548c\u5bf9\u5e94\u7684action\u5bf9\u8c61,\u800caction\u5bf9\u8c61\u4f1a\u88ab\u653e\u5165ValueStack\u5bf9\u8c61\u4e2d,ValueStack\u5bf9\u8c61\u5219\u4f1a\u653e\u5165ActionContext\u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>  \u5728jsp\u9875\u9762\u4e2d\u8bbf\u95eecontext\u7684\u6570\u636e\u65f6,\u90fd\u9700\u7528&#8221;#&#8221;,\u4f46ValueStack\u662f\u5176\u6839\u5bf9\u8c61\u5219\u4e0d\u9700\u8981\uff0c\u56e0\u6b64\uff0c\u5728\u8bbf\u95eeaction\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6<br \/>\n\u4e0d\u9700\u8981\u4f7f\u7528&#8221;#&#8221;\u3002<\/p>\n<h2>\u4e8c\u3001\u4e3b\u8981\u529f\u80fd<\/h2>\n<p>1.\u53d6\u6570\u636e,\u5b83\u53ef\u4ee5\u5229\u7528\u4e00\u6bb5\u7b80\u77ed\u7684\u8868\u8fbe\u5f0f\u53d6\u51fa\u5404\u79cd\u5404\u6837\u4e30\u5bcc\u7684\u6570\u636e.<br \/>\n2.\u9644\u5e26\u4e86\u4e00\u4e9b\u4fbf\u6377\u7684\u529f\u80fd,\u5982:\u65b9\u6cd5\u8c03\u7528\u3001\u9759\u6001\u65b9\u6cd5\u548c\u5c5e\u6027\u8c03\u7528\u3001\u6570\u503c\u8fd0\u7b97\u2026\u2026<\/p>\n<h2>\u4e09\u3001\u5982\u4f55\u53d6\u6570\u636e<\/h2>\n<p>\u3000  ognl\u4f1a\u4ece\u4e24\u4e2a\u5730\u65b9\u53d6\u6570\u636e\uff1a\u4e00\u4e2a\u662fAction\u7684\u5b9e\u4f8b\u5c5e\u6027\uff1b\u53e6\u4e00\u4e2a\u662fValueStack\uff08\u4e2d\u6587\u540d\u53eb\u503c\u6808\uff09\u3002<br \/>\n\u00a0\u00a0\u00a0 ognl\u4f1a\u5148\u4ece\u524d\u8005\u91cc\u9762\u53d6\uff0c\u5982\u679c\u6ca1\u53d6\u5230\u518d\u5230ValueStack\u91cc\u53d6\u3002ValueStack\u91cc\u5b58\u50a8\u4e86request\u3001session\u3001application\u3001response\u3001action\u5b9e\u4f8b\u3001parameters\u6570\u7ec4\u2026\u2026<br \/>\n\u00a0 <\/p>\n<h3>1.\u53d6action\u5b9e\u4f8b\u7684\u5c5e\u6027\u6570\u636e<\/h3>\n<p>\u3000\u3000action\u5b9e\u4f8b\u7684\u5c5e\u6027\u6570\u636e\u53ef\u4ee5\u76f4\u63a5\u5728struts2\u7684\u6807\u7b7e\u4e2d\u901a\u8fc7\u5c5e\u6027\u540d\u53d6\u5230\u3002\u5982\uff1a<s:property value=\"name\"\/>\u3001<s:property value=\"user.password\"\/><br \/>\n\u3000\u3000\u6ce8\u610f\uff1a\u4e0d\u8981\u52a0#\u53f7\u3002<\/p>\n<h3>2.\u53d6ValueStack\u4e2d\u7684\u6570\u636e\u3002<\/h3>\n<p>\u3000\u3000struts\uff12\u63d0\u4f9b\u4e09\u79cd\u65b9\u5f0f\u901a\u8fc7ognl\u8868\u8fbe\u5f0f\u6765\u53d6ValueStack\u4e2d\u7684\u6570\u636e\uff1a#\u3001%{}\u3001${}<br \/>\n\u3000\u3000#\u548c%{}\u9700\u8981\u653e\u5230struts2\u63d0\u4f9b\u7684\u6807\u7b7e\u91cc\u624d\u751f\u6548\u3002\u5982\uff1a[java]&lt;s:property value=&quot;#name&quot;\/&gt;\u3001&lt;s:property value=&quot;%{&#8216;hello struts2&#8217;}&quot;\/&gt;[\/java]<br \/>\n\u3000  \u201c%\u201d\u7b26\u53f7\u7684\u7528\u9014\u662f\u5728\u6807\u7b7e\u7684\u5c5e\u6027\u503c\u88ab\u7406\u89e3\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u65f6\uff0c\u544a\u8bc9\u6267\u884c\u73af\u5883%{}\u91cc\u7684\u662fOGNL\u8868\u8fbe\u5f0f\u3002 [java]&lt;s:url value=&quot;%{#myMap[&#8216;key1&#8217;]}&quot;\/&gt;[\/java]<br \/>\n\u3000\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\uff1a#<br \/>\n\u3000[java]\u3000\uff11.#\u80fd\u53d6request\u3001session\u3001application\u91cc\u7684attribute\uff0c\u4f46\u9700\u8981\u52a0\u524d\u7f00\u3002\u5982\uff1a<br \/>\n&lt;s:property value=&quot;#session.name2&quot;\/&gt;\u3001<br \/>\n&lt;s:property value=&quot;#application.name3&quot;\/&gt;\u3002<\/p>\n<p>\u5982\u679c\u662f\u53d6request\u8303\u56f4\u7684attribute\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u52a0request\u524d\u7f00\uff0c\u52a0\u4e0a\u53cd\u800c\u53d6\u4e0d\u5230\u6570\u636e\uff0cognl\u9ed8\u8ba4\u4ecerequest\u91cc\u53d6\uff0c\u5982\u679c\u6ca1\u6709\u53d6\u5230\u5e76\u4e0d\u4f1a\u5230session\u6216application\u91cc\u53d6\u3002<br \/>\n\u5982\uff1a&lt;s:property value=&quot;#name&quot;\/&gt;<\/p>\n<p>\u3000\u3000\uff12.#\u80fd\u53d6request\u91cc\u7684\u8bf7\u6c42\u53c2\u6570\uff0c\u4f46\u5fc5\u987b\u52a0parameters\u524d\u7f00,\u4e14\u53d6\u5230\u7684\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u8981\u5f97\u5230\u53c2\u6570\u7684\u7b2c\u4e00\u9879\u503c\uff0c\u90a3\u4e48\u8fd8\u8981\u52a0\u4e0b\u6807\u3002<br \/>\n\u5982\uff1a&lt;s:property value=&quot;#parameters.name[0]&quot;\/&gt;\u3002\u8fd9\u76f8\u5f53\u4e8e\u8c03\u7528request.getParameterValues(&quot;name&quot;)[0];<\/p>\n<p>\u3000\u30003.#\u52a0attr\u524d\u7f00\u80fd\u6309request &gt; session &gt;application\u987a\u5e8f\u83b7\u53d6attribute\uff0c\u8fd9\u6837\u5f53\u5728request\u4e2d\u53d6\u4e0d\u5230\u65f6\uff0c\u4f1a\u81ea\u52a8\u5411session\u91cc\u53d6\uff0c\u5982\u679csession\u91cc\u4e5f\u53d6\u4e0d\u5230\uff0c\u4f1a\u518d\u5411application\u91cc\u53d6\u3002\u5982\u679c\u53d6\u5230\u5219\u8fd4\u56de\uff0c\u4e0d\u518d\u5411\u4e0a\u6e38\u5386\u3002<br \/>\n\u5982\uff1a&lt;s:property value=&quot;#attr.name&quot;\/&gt;<\/p>\n<p>\u3000\u3000\uff14.#\u80fd\u6784\u9020Map\uff0c<br \/>\n \u5982\uff1a&lt;s:set name=&quot;foobar&quot; value=&quot;#{&#8216;foo1&#8242;:&#8217;bar1&#8217;, &#8216;foo2&#8242;:&#8217;bar2&#8217;}&quot; \/&gt;&lt;s:property value=&quot;#foobar[&#8216;foo1&#8217;]&quot; \/&gt;<br \/>\n    &lt;s:radio list=&quot;#{&#8216;0&#8242;:&#8217;\u7537&#8217;,&#8217;1&#8242;:&#8217;\u5973&#8217;}&quot; name=&quot;sex&quot; label=&quot;\u6027\u522b&quot; value=&quot;%{user.sex}&quot;&gt;&lt;\/s:radio&gt;<br \/>\n\u3000\u3000\uff15.#\u80fd\u7528\u4e8e\u8fc7\u6ee4\u548c\u6295\u5f71\uff08projecting)\u96c6\u5408\uff0c<br \/>\n\u5982\uff1abooks.{?#this.price&lt;100}<\/p>\n<p>   6\u3001\u201c$\u201d\u6709\u4e24\u79cd\u7528\u9014   <\/p>\n<p>(1)\u3001\u5728\u56fd\u9645\u5316\u8d44\u6e90\u6587\u4ef6\u4e2d\uff0c\u5f15\u7528OGNL\u8868\u8fbe\u5f0f\u3002   <\/p>\n<p>(2)\u3001\u5728Struts 2\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u5f15\u7528OGNL\u8868\u8fbe\u5f0f\uff1a    <\/p>\n<p>&lt;action name=&quot;saveUser&quot; class=&quot;userAction&quot; method=&quot;save&quot;&gt;   <\/p>\n<p>&lt;result type=&quot;redirect&quot;&gt;listUser.action?msg=${msg}&lt;\/result&gt;   <\/p>\n<p>&lt;\/action&gt;   <\/p>\n<p>&lt;action name=&quot;saveUser&quot; class=&quot;userAction&quot; method=&quot;save&quot;&gt;       <\/p>\n<p>&lt;result type=&quot;redirect&quot;&gt;listUser.action?msg=${msg}&lt;\/result&gt;   <\/p>\n<p>&lt;\/action&gt;<br \/>\n[\/java]<\/p>\n<h2>\u4e09\u3001\u4ee3\u7801\u5b9e\u4f8b<\/h2>\n<p>\u5982:java:<br \/>\n[java]\/**<br \/>\n *   \u901a\u8fc7ognl\u8868\u8fbe\u5f0f\u4eceognl\u4e0a\u4e0b\u6587(context)\u83b7\u53d6\u503c\u663e\u793a\u5230jsp\u9875\u9762,ognl\u8868\u8fbe\u5f0f\u53ea\u4f7f\u7528\u5728struts2\u6807\u7b7e\u4e2d<br \/>\n *   ognl\u4e0a\u4e0b\u6587(context) &lt;=&gt; ActionContext &lt;- ValueStack &lt;- action\u5bf9\u8c61<br \/>\n *<br \/>\n *   \u503c\u6808 ValueStack \u5305\u542b\u5982\u4e0b\u5bf9\u8c61:<br \/>\n *   action\u5b9e\u4f8b\u3001request\u3001session\u3001application\u3001response\u3001parameters\u6570\u7ec4  attr&#8230;<br \/>\n *<br \/>\n *\/<br \/>\npublic class OgnlAction extends BaseServletAction{<br \/>\n\tprivate String name;<br \/>\n\tprivate User user = new User();<br \/>\n\tprivate List&lt;User&gt; list = new ArrayList&lt;User&gt;();<\/p>\n<p>\t@Override<br \/>\n\tpublic String execute() throws Exception {<br \/>\n\t\tActionContext actionContext = ActionContext.getContext();<br \/>\n\t\tValueStack  valueStack = actionContext.getValueStack();<br \/>\n\t\tvalueStack.push(&quot;ognl\u540d\u79f0&quot;);<\/p>\n<p>\t\tname=&quot;\u5f20\u4e09&quot;;<br \/>\n\t\tuser.setName(&quot;\u674e\u56db&quot;);<br \/>\n\t\tuser.setPassword(&quot;abcd&quot;);<\/p>\n<p>\t\tlist.add(new User(&quot;\u738b\u4e8c&quot;,&quot;456&quot;));<br \/>\n\t\tlist.add(new User(&quot;\u5c0f\u660e&quot;,&quot;123&quot;));<\/p>\n<p>\t\tactionContext.put(&quot;number&quot;, &quot;request=1001&quot;);<\/p>\n<p>\t\tsession.setAttribute(&quot;age&quot;, &quot;session=23&quot;);<\/p>\n<p>\t\treturn SUCCESS;<br \/>\n\t}<\/p>\n<p>\tpublic String getName() {<br \/>\n\t\treturn name;<br \/>\n\t}<br \/>\n\tpublic void setName(String name) {<br \/>\n\t\tthis.name = name;<br \/>\n\t}<br \/>\n\tpublic User getUser() {<br \/>\n\t\treturn user;<br \/>\n\t}<br \/>\n\tpublic void setUser(User user) {<br \/>\n\t\tthis.user = user;<br \/>\n\t}<br \/>\n\tpublic List&lt;User&gt; getList() {<br \/>\n\t\treturn list;<br \/>\n\t}<br \/>\n\tpublic void setList(List&lt;User&gt; list) {<br \/>\n\t\tthis.list = list;<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>jsp:<br \/>\n[java]&lt;body&gt;<br \/>\n\t&lt;!&#8211; \u663e\u793avaluestack\u503c\u6808\u5185\u5bb9 &#8211;&gt;<br \/>\n\t&lt;s:property value=&quot;name&quot;\/&gt;<br \/>\n\t&lt;s:property value=&quot;user.name&quot;\/&gt;<br \/>\n\t&lt;s:property value=&quot;user.password&quot;\/&gt;<\/p>\n<p>\t&lt;hr&gt;<br \/>\n\t&lt;s:iterator value=&quot;list&quot; var=&quot;u&quot;&gt;<br \/>\n\t\t&lt;s:property value=&quot;#u.name&quot;\/&gt;&lt;br&gt;<br \/>\n\t&lt;\/s:iterator&gt;<\/p>\n<p>\t&lt;s:property value=&quot;#number&quot;\/&gt;<\/p>\n<p>\t&lt;s:property value=&quot;#session.age&quot;\/&gt;<\/p>\n<p>&lt;%&#8211; &lt;s:set var=&quot;name&quot;  value=&quot;\u5c0f\u660e&quot; &gt;&lt;\/s:set&gt;  &#8211;%&gt;<br \/>\n\t&lt;%&#8211;ognl \u4eceaction\u5bf9\u8c61\u4e2d\u53d6\u5c5e\u6027\u503c &#8211;%&gt;<br \/>\n\t&lt;s:property value=&quot;name&quot; default=&quot;\u9ed8\u8ba4\u503c&quot; \/&gt;<br \/>\n\t&lt;s:property value=&quot;user.username&quot; default=&quot;\u9ed8\u8ba4\u503c&quot; \/&gt;<\/p>\n<p>\t&lt;br&gt;\u7528\u6237\u540d:<br \/>\n\t&lt;s:property value=&quot;userLists[1].username&quot; default=&quot;\u9ed8\u8ba4\u503c&quot; \/&gt;<br \/>\n\t\u5bc6\u7801:<br \/>\n\t&lt;s:property value=&quot;userLists[1].password&quot; default=&quot;\u9ed8\u8ba4\u503c&quot; \/&gt;<br \/>\n\t&lt;br&gt;<br \/>\n\t&lt;%&#8211;ognl \u4eceServletAPI request session &#8230;. \u5bf9\u8c61\u4e2d\u53d6\u5c5e\u6027\u503c &#8211;%&gt;<br \/>\n\t&lt;s:property value=&quot;#request.age&quot; default=&quot;request\u9ed8\u8ba4\u503c&quot; \/&gt;<br \/>\n\t&lt;s:property value=&quot;#session.sex&quot; default=&quot;session\u9ed8\u8ba4\u503c&quot; \/&gt;<br \/>\n\t&lt;!&#8211; reqeust &gt; session &gt; application &#8211;&gt;<br \/>\n\t&lt;s:property value=&quot;#attr.age&quot; default=&quot;attr\u9ed8\u8ba4\u503c&quot; \/&gt;<\/p>\n<p>\t&lt;%&#8211;ognl \u4ecevalueStack\u4e2d\u53d6\u5c5e\u6027\u503c &#8211;%&gt;<br \/>\n\t&lt;s:property value=&quot;stack2.username&quot; default=&quot;valueStack\u9ed8\u8ba4\u503c&quot; \/&gt;<\/p>\n<p>\t${stack2.password}<br \/>\n\t&lt;br&gt; ${user.username}<\/p>\n<p>\t&lt;br&gt;<br \/>\n\t&lt;s:form action=&quot;ognltag.jsp&quot; method=&quot;post&quot;&gt;<br \/>\n\t\t&lt;s:textfield label=&quot;\u7528\u6237\u540d&quot; name=&quot;user_name&quot;&gt;&lt;\/s:textfield&gt;<\/p>\n<p>\t\t&lt;s:password label=&quot;\u5bc6\u7801&quot; name=&quot;pass_word&quot;&gt;&lt;\/s:password&gt;<\/p>\n<p>\t\t&lt;s:submit value=&quot;\u63d0\u4ea4&quot;&gt;&lt;\/s:submit&gt;<br \/>\n\t&lt;\/s:form&gt;<\/p>\n<p>\t&lt;s:property value=&quot;#parameters.user_name&quot;\/&gt;<br \/>\n\t&lt;s:property value=&quot;#parameters.pass_word&quot;\/&gt;<\/p>\n<p>\t&lt;s:debug&gt;&lt;\/s:debug&gt;<br \/>\n&lt;\/body&gt;[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001OGNL\u4ecb\u7ecd OGNL\u5168\u79f0\u662fObject-Graph Navigation Language(\u5bf9\u8c61\u56fe\u5bfc\u822a\u8bed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[87],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1875"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1875"}],"version-history":[{"count":6,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1875\/revisions"}],"predecessor-version":[{"id":1977,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1875\/revisions\/1977"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}