Struts2 s: if tag and how to use #, % {}, % {#}

Source: Internet
Author: User

<S: if> judge the string: 1. Judge a single character: <s: if test = "# session. user. username = 'C' "> in this way, the username value is retrieved from the session and determined whether it is c. However, this is incorrect, it cannot be determined at all. It should be changed to the following: <s: if test = "# session. user. username = 'C '. toString () "> in this way, we can make a correct judgment. I don't know the reason. on the Internet, we can see that struts2 may judge the char type. 2. Judge the string: <s: if test = "# session. user. username = 'milo' "> in this case, it is written to judge whether username is milo or String. This does not need to be added with toString. 3. Value: <s: if test = "# session. user. username = 0"> in this case, the value is int. B: NULL: <s: if test = "# session. user. username = null "> In struts2, it seems that the null value can only be written in this way to judge whether it is not null. You can write it like this: <s: if test =" # session. user. username! = Null "> ps: If the property value is obtained from the action, '#' before the property value in test is not required. The property in action is interconnected with the property in jsp. String N must be enclosed by "" Double quotation marks. From test, it is enclosed by single quotation marks ''. If it is the opposite, the attribute cannot be correctly determined to be equal to the string. Correct: <s: if test = 'activitybean. searchForce = "N" '> error: <s: if test = "activityBean. searchForce = 'n' "> Because java is a strong type language, single quotation marks indicate the char type. Only one character can be assigned to the char type, while double quotation marks indicate the String type, so my activityBean. if the searchForce attribute is of the String type, N must use double quotation marks. For example, this statement must be used to determine systemSettingModel In the struts stack. whether settingValue is equal to "A" written as <s: if test = "systemSettingModel. settingValue = 'A' ">... </s: if> is incorrect. In this way, A is considered A character, while systemSettingModel. settingValue is a single character string. if it does not match, it should be written as <s: if test = 'systemsettingmodel. settingValue = "A" '>... </S: if> or <s: if test = "systemSettingModel. settingValue = \ "A \" ">... </s: if> In addition, systemSettingModel. the value of settingValue can be $ {systemSettingModel. settingValue} <s: property value = "systemSettingModel. settingValue "/> <s: property value =" # request. systemSettingModel. settingValue "/> # indicates that it is not in the struts stack. No # indicates that the tag" # "," % {} "in the Struts2 stack is obtained from the struts stack {}", "% {#}" s: When should the href in a use "#" and "% {}" be used and "% {#}" be used? S: When should "#" Be Used in the list in the select statement, "% {}", "% {#}", and "" s "be used: when should I use "#" for test in if? When should I use "% {}"? When should I use "% {#}"? When should I use "s: when should "#" be used for the value IN iterator, "% {}", "% {#}", and "" s "be used: when should "#" Be used for the list in the checkboxlist, "% {}", "% {#}", and "" Struts2 "have the concept of value stack and stack context?, you can see from <s: debug/>. in the S tag, # is used to retrieve the objects stored in the stack context. you can use % {} to retrieve the Action object in the value stack and call its method directly. for example, if your Action inherits ActionSupport. in the page tag, use % {getText ('Key. % {#}. This is the syntax in JSP2.1's latest specification, it is written as % {#} in the domain object for Struts2 to solve the compatibility problem. For example, % {# session. user. userName} will get the userName attribute value of the user object % {}. It is emphasized that % {} is taken from the root point you set for the calculation expression. For example, % {10 + 20}, the result will be output for 30.% {"a" + "B"} results will be output "AB" # normally, the emphasis is given from the context as # parameters. name [0] is equivalent to request. getParameter ("name") for example # session. name is equivalent to session. getAttribute ("name") such as # request. name is equivalent to request. getAttribute ("name") % {#} is still used to calculate the expression, but the operation element can be a variable. For example, if you first define a variable <s: set n Ame = "age" value = "% {25}"/> % {# age} Will output 25, or you can omit "#", write as % {age}. If you add age to 10, you can write as follows: % {# age + 10} and output 30. "#" cannot be omitted "#". That is to say, when a variable is used for calculation, the #, %, and $ #, %, and $ symbols in the "#" ognl cannot be omitted, these three symbols are also a part that developers cannot easily grasp and understand. Here, I will briefly introduce their respective uses. 1. # There are generally three types of symbols. 1) access non-root object attributes. For example, the # session. msg expression in the example. Because the Struts 2 mid-value stack is regarded as the root object, the # prefix is required when accessing other non-root objects. In fact, the expression # is equivalent to ActionContext. getContext (); # session. msg is equivalent to ActionContext. getContext (). getSession (). getAttribute ("msg "). 2) used for filtering and projecting sets, such as persons .{? # This. age> 20 }. 3) used to construct a Map. For example, # {'foo1': 'bar1', 'foo2': 'bar2'} in the example ′}. 2.% The purpose of the symbol % is to calculate the value of the OGNL expression when the flag attribute is of the string type. The following code constructs a Map <s: set name = "foobar" value = "# {'foo1': 'bar1', 'foo2 ′: 'bar2'} "/> <p> The value of key" foo1 "is <s: property value = "# foobar ['foo1']"/> </p> <p> do not use %: <s: url value = "# foobar ['foo1']"/> </p> <p> usage %: <s: url value = "% {# foobar ['foo1']}"/> </p> 3. the $ symbol has two main purposes. REFERENCE The OGNL expression in the international resource file. For example, in the international resource file code: reg. agerange = International Resource Information: The age must be between min and {max. REFERENCE The OGNL expression in the configuration file of the Struts 2 framework, as shown in the following code snippet: <validators> <field name = "intb"> <field-validator type = "int"> <param name = "min"> 10 </param> <param name = "max "> 100 </param> <message> BAction-test Verification: the number must be between min and {max! </Message> </field-validator> </field> </validators: if test = "deptType = 1"> subordinate </s: if> the field type of your database is integer, you can use it like this. <s: if test = "deptType = '1'"> subordinate </s: if> if the field type of your database is char, you can use it like this. <s: if test = "deptType = 'A'"> subordinate </s: if> if the field type of your database is varchar, and the strings in the database are redundant strings, which can be used like this; <s: if test = "deptType = \" 1 \ ""> subordinates </s: if> <s: else> direct </s: else> (the test is successful. This is the most correct method.) How to Solve the style selection problem: <li> <a href = "/pro! ProductList. action? ProId =$ {productType. id} "<s: if test =" # productType. id = # parameters. productType [0] "> class =" currt "</s: if >>$ {productType. typeName} </a> </li>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.