2010-12-30 3 views

ответ

3

Edit: Существует разница между "" (двойные кавычки) и '' (одинарные кавычки) в OGNL.

Двойные кавычки оценивают одиночные символы для строк, в то время как одинарные кавычки оценивают отдельные символы в типах символов. Вы можете поставить несколько символов в одинарных кавычках и будет оцениваться как строка.

Дополнительный объем запрос не

Map<String, String> 

но

Map<String, String[]> 

как можно видеть ниже в последние 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; 
} 
+0

1 это просто пример. любая строка ведет себя одинаково: не работает – eumust

+0

Ну нет, потому что == для встроенных типов. Строки нельзя сравнивать таким образом. Если #parameters ['test'] оценивается как 'hello' будет работать. Если #parameters ['test'] оценивает целое число, будет работать. – Quaternion

+0

только что протестировано: производит false на test.action? Test = hello – eumust

0

Я только что нашел, что #parameters ['test'] возвращает массив String. Так что, когда вы говорите

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

вы на самом деле, сравнивая строку «1» с массивом { «1»}, следовательно, он возвращает ложь. Может быть, это потому, что есть два параметра с таким же именем

правильно, что нужно сделать было бы

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