检查Struts2标签中的请求参数值

发布于 2024-10-09 12:59:05 字数 284 浏览 3 评论 0原文

我的 jsp: 中有以下代码:

<s:property value="#parameters['test']"/>
<br/><s:property value="'1'.equals(#parameters['test'])"/>

现在如果我像这样访问此操作: test.action?test=1

我得到以下信息:

1
false

想知道为什么 1 不等于 1?

I have the following code in my jsp:

<s:property value="#parameters['test']"/>
<br/><s:property value="'1'.equals(#parameters['test'])"/>

now if I access this action like this:
test.action?test=1

I get the following:

1
false

Wondering, why 1 is not equal to 1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-10-16 12:59:06

编辑
OGNL 中的 ""(双引号)和 ''(单引号)之间存在差异。

双引号将单个字符评估为字符串,而单引号将单个字符评估为字符类型。您可以将多个字符放在单引号中,它将被视为字符串。

进一步的请求范围不是

Map<String, String> 

,但

Map<String, String[]> 

可以在 jsp 的最后 5 行中看到。

JSP

<%@taglib prefix="s" uri="/struts-tags"%>
<!-- values from action where letters = "abcd" -->
<s:property value="letters"/><br/> <!-- Displays: abcd -->
<s:property value="letters.equals('abcd')"/><br/> <!-- Displays: true -->
<s:property value="'abcd'.compareTo('abcd')"/><br/> <!-- Displays: 0 -->
<s:property value="'abcd'.compareTo('abcd') == 0"/><br/> <!-- Displays: true -->
<s:property value="'abcd'.equals('abcd')"/><br/> <!-- Displays: true -->
<s:property value="'abcd'.equals(letters)"/><br/> <!-- Displays: true -->
<br/>
<!-- RUN with ?test=a&test2=abc appended to the url -->
<!-- Time for the numbers from action where number = 1-->
<s:property value="number"/><br/><!-- Displays: 1 -->
<s:property value="number.toString()"/><br/><!-- Displays: 1 -->
<!-- OGNL strings in certain cases must be double quoted -->
<s:property value='"1".equals(number.toString())'/><br/><!-- Displays: true -->
<!-- As we can see single quotes does automatic type conversion to Character which is then evaluates false-->
<s:property value="'1'.equals(number.toString())"/><br/><!-- Displays: false -->
<!-- here a string is compared to an integer which is false-->
<s:property value='"1".equals(number)'/><br/><!-- Displays: false -->
<br/><!-- Request Variables -->
<s:property value="#parameters['test']"/><br/><!-- Displays: a -->
<!-- a is single quoted so automatic type conversion probably converted it to a Character, which is not equal to string "a" -->
<s:property value="'a'.equals(#parameters['test'])"/><br/><!-- Displays: false -->
<!-- both are strings so equality works as expected -->
<s:property value='#parameters["test"]'/><br/><!-- Displays: a -->
<s:property value='"a".equals(#parameters["test"])'/><br/><!-- Displays: false because #parameters["test"] is a string[] and calling toString on string[] does not work -->
<!-- #parameters['test2'] now is 'abc' automatic type conversion of OGNL swill convert 'abc' to a string and so both are equal -->
<s:property value='#parameters["test2"]'/><br/>
<!-- Double quotes must be a string -->
<s:property value='"abc".compareTo(#parameters["test2"]) == 0'/><br/><!-- Displays: true -->
<!-- Single quote so automatic type conversion... string of chars is converted to String -->
<s:property value="'abc'.compareTo(#parameters['test2']) == 0"/><br/><!-- Displays: true -->
<!-- Out of curiosity I'd like to know if '1' is an Integer or a Byte -->
<s:property value="'1'.toString()"/><br/><!-- Answer: Neither it prints "class java.lang.Character" -->
<!-- 'a' is a Character however equals calls toString() -->
<!-- But the request object (and session too) is not <string, string> but <string, string[]> -->
1: <s:property value='"1".class'/><br/> <!-- class java.lang.String -->
2: <s:property value='#parameters["test"].class'/><br/> <!-- Array of string: class [Ljava.lang.String; -->
3: <s:property value='#parameters["test"][0].class'/><br/> <!-- This is what we need: class java.lang.String -->
<s:property value='#parameters["test"][0].equals("a")'/><br/> <!-- Now this works -->
<s:property value="#parameters['test'][0].equals('a'.toString())"/><br/> <!-- this is another way, leaving off the .toString results in false -->

Test.jsp(Action)

封装struts2;
导入 com.opensymphony.xwork2.ActionSupport;

public class test extends ActionSupport{
    public String letters = "abcd";
    public int number = 1;
}

Edit:
There is a difference between "" (double quotes) and '' (single quotes) in OGNL.

Double quotes evaluate single characters to strings while single quotes evaluate single characters to Character types. You may put multiple characters in single quotes and it will be evaluated as a string.

Further request scope is not

Map<String, String> 

but

Map<String, String[]> 

as can be seen below in last 5 lines of the jsp.

JSP

<%@taglib prefix="s" uri="/struts-tags"%>
<!-- values from action where letters = "abcd" -->
<s:property value="letters"/><br/> <!-- Displays: abcd -->
<s:property value="letters.equals('abcd')"/><br/> <!-- Displays: true -->
<s:property value="'abcd'.compareTo('abcd')"/><br/> <!-- Displays: 0 -->
<s:property value="'abcd'.compareTo('abcd') == 0"/><br/> <!-- Displays: true -->
<s:property value="'abcd'.equals('abcd')"/><br/> <!-- Displays: true -->
<s:property value="'abcd'.equals(letters)"/><br/> <!-- Displays: true -->
<br/>
<!-- RUN with ?test=a&test2=abc appended to the url -->
<!-- Time for the numbers from action where number = 1-->
<s:property value="number"/><br/><!-- Displays: 1 -->
<s:property value="number.toString()"/><br/><!-- Displays: 1 -->
<!-- OGNL strings in certain cases must be double quoted -->
<s:property value='"1".equals(number.toString())'/><br/><!-- Displays: true -->
<!-- As we can see single quotes does automatic type conversion to Character which is then evaluates false-->
<s:property value="'1'.equals(number.toString())"/><br/><!-- Displays: false -->
<!-- here a string is compared to an integer which is false-->
<s:property value='"1".equals(number)'/><br/><!-- Displays: false -->
<br/><!-- Request Variables -->
<s:property value="#parameters['test']"/><br/><!-- Displays: a -->
<!-- a is single quoted so automatic type conversion probably converted it to a Character, which is not equal to string "a" -->
<s:property value="'a'.equals(#parameters['test'])"/><br/><!-- Displays: false -->
<!-- both are strings so equality works as expected -->
<s:property value='#parameters["test"]'/><br/><!-- Displays: a -->
<s:property value='"a".equals(#parameters["test"])'/><br/><!-- Displays: false because #parameters["test"] is a string[] and calling toString on string[] does not work -->
<!-- #parameters['test2'] now is 'abc' automatic type conversion of OGNL swill convert 'abc' to a string and so both are equal -->
<s:property value='#parameters["test2"]'/><br/>
<!-- Double quotes must be a string -->
<s:property value='"abc".compareTo(#parameters["test2"]) == 0'/><br/><!-- Displays: true -->
<!-- Single quote so automatic type conversion... string of chars is converted to String -->
<s:property value="'abc'.compareTo(#parameters['test2']) == 0"/><br/><!-- Displays: true -->
<!-- Out of curiosity I'd like to know if '1' is an Integer or a Byte -->
<s:property value="'1'.toString()"/><br/><!-- Answer: Neither it prints "class java.lang.Character" -->
<!-- 'a' is a Character however equals calls toString() -->
<!-- But the request object (and session too) is not <string, string> but <string, string[]> -->
1: <s:property value='"1".class'/><br/> <!-- class java.lang.String -->
2: <s:property value='#parameters["test"].class'/><br/> <!-- Array of string: class [Ljava.lang.String; -->
3: <s:property value='#parameters["test"][0].class'/><br/> <!-- This is what we need: class java.lang.String -->
<s:property value='#parameters["test"][0].equals("a")'/><br/> <!-- Now this works -->
<s:property value="#parameters['test'][0].equals('a'.toString())"/><br/> <!-- this is another way, leaving off the .toString results in false -->

Test.jsp (Action)

package struts2;
import com.opensymphony.xwork2.ActionSupport;

public class test extends ActionSupport{
    public String letters = "abcd";
    public int number = 1;
}
孤独难免 2024-10-16 12:59:06

我刚刚发现 #parameters['test'] 返回一个字符串数组。因此,当您说

<s:property value="'1'.equals(#parameters['test'])"/>

您实际上是将字符串 '1' 与数组 {'1'} 进行比较时,它会返回 false。也许这是因为有两个参数具有相同的名称,

正确的做法是

<s:property value="'1'.equals(#parameters['test'][0])"/>

I just found that #parameters['test'] returns an array of String. So when you say

<s:property value="'1'.equals(#parameters['test'])"/>

you are actually comparing a string '1' with an array {'1'} hence it returns false. Maybe this is because there are two parameters with same name

correct thing to do would be

<s:property value="'1'.equals(#parameters['test'][0])"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文