How do I convert a JSP variable to a Struts2 variable?

故事扮演 提交于 2020-01-15 05:26:49

问题


How do I convert a JSP variable to a Struts2 variable?

I've tried the following:

<%=scoredDocument%>
<s:push value="scoredDocument"/>
<s:push value="#scoredDocument"/>
<s:push value="%{scoredDocument}"/>
<s:push value="${scoredDocument}"/>
<s:push value="#page.scoredDocument"/>
<s:push value="%{#page.scoredDocument}"/>

<display:column title="Study Code" sortable="true">
    <s:property value="id"/>

The most frequent error is

Caused by: tag 'push', field 'value': You must specify a value to push on the stack. Example: person - [unknown location]


回答1:


<s:push> must enclose the <s:property> tag. Also

<s:push value="#attr.scoredDocument">
    <display:column title="Study Code" sortable="true">
        <s:property value="id"/>
    </display:column>
</s:push>

#attr? WTF Struts? It's not even documented! https://struts.apache.org/release/2.0.x/docs/jsp.html




回答2:


The push tag requires the value attribute and it has to be initialized, otherwise it shows the JSP error. Unlike other tags where the value could be preinitialized with empty string, the JSP doesn't complain.

The OGNL expression is evaluated in this attribute, like in many other attributes of Struts tags, and if can't resolve the value it returns null. This value is unacceptable for the push tag and it throws exception. The error mislead to make a different tries to access the scriptlet variable value via the OGNL expression, unfortunately none of that methods worked.

Scriptlet expressions has a one pitfall when used with the Struts tags that you can use only in the body of the tag and it's converted to string. Like in this example

<s:set var="scoredDocument"><%=scoredDocument%></s:set>

you can't do the same with the push tag. After that you can use the stringified version of #scoredDocument in OGNL expressions.



来源:https://stackoverflow.com/questions/20232665/how-do-i-convert-a-jsp-variable-to-a-struts2-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!