Java constant Java static method introduced in struts2 freemarker

Source: Internet
Author: User

1. Configure <constant name = "struts. ognl. allowstaticmethodaccess" value = "true"/> In struts. SML.

Indicates that the static Java method is allowed. Very important.

2. Introduce Java constants in freemarker:

Suppose:

Test. Java

Package test;

Public class test {

Private string usersex = man;

Public static final string man = "male ";

Public static final string woman = "female ";

Public static string M ()
{

Return "test ";
}

Public String getusersex ()
{
Return usersex;
}

Public void setusersex (string usersex)
{
This. usersex = usersex;
}

}

 

Next we will call the static constants and static methods in this class in freemarker.

A. Call constants:

$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample. model. user1 @ Man ")}
</BR>
$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample. model. user1 @ M ()")}

The expected result is displayed.

 

Note: currently, only static

Supplement: currently, the method to be called must have a return value that cannot be the void method. If it is the static void method, this method will be used when running the page, but there is no value, so an error will occur, at this time, it is best to use

$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample2.freemarkerexample2. s @ Haha ('aaa ')")! "No "}

No is displayed when it is determined that it does not exist.

 

 

AttachedSource codeAs follows:

Java

Public class S
{
Public static string STR = "institutes ";

Private string name = "";

Public static String say ()
{
Return "good ";
}

Public static void Haha (string ha)
{
System. Out. println (HA );
}

Public String getname ()
{
Return name;
}

Public void setname (string name)
{
This. Name = Name;
}

Public static s gets ()
{
S = new S ();
S. setname ("China ");
Return S;
}
}

 

FTL:

$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample2.freemarkerexample2. s @ Str ")}
$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample2.freemarkerexample2. s @ say ()")}
$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample2.freemarkerexample2. s @ Haha ('aaa ')")! "No "}

$ {Stack. findvalue ("@ com. Wish. maml. freemarkerexample2.freemarkerexample2. s @ gets (). Name ")! "No "}

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.