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 "}