JSTL Function Tags
2011年09月01日
Now, let’s have a look at the JSTL Function tag. Create a new JSP. Let’s name it Jstl_Functions_Tags.jsp. Then create a new link to direct to this new JSP. JSTL Functions Tags String Manipulations Do not forget to include JSTL taglib directive for prefix fn in our JSP. Here is how to import the fn JSTL taglib directive. The JSTL functions tag will mainly deal with string manipulation. Let’s try some of them. Above codes are used to set a fixed string to variable “sentence” and print it out for display. Next, please pay attention on how we are going to combine JSTL core tag with JSTL function tag. Above codes will print out the length of variable “sentence”. It will produce 35 since all characters are counted. Above codes are used to convert our “sentence” to UPPER or LOWER case. Above codes are used to trim the “sentence” and count the length of trimmed “sentence”. Trimming the sentence means that we are removing the extra spaces (if any) in the string either in front or in back. You need to know that the spaces within the string will NOT be trimmed. It should produce 33 since the spaces at the beginning and at the end will be discarded. While the sample above of fn:replace will replace all occurrences of spaces with underscores.
The fn:substring will take 3 parameters. 1stparameter is our “sentence” variable itself. 2ndparameter is for the starting point and 3rdparameter is where it must stop to return result of substring. More advanced substring functions are fn:substringAfter and fn:substringBefore. See at the screenshot to see how they work.
And the fn:indexOf will return the position of defined character in our “sentence”.
fn:contains will return boolean value true if correct and false otherwise. However, it is case sensitive and will sensitively check for the lower case and upper case of the string. fn:containsIgnoreCase work in the same way as fn:contains but it is not case sensitive which means that it will not care whether it is in lower case or in upper case. fn:startsWith and fn:endsWith will also return boolean to check whether the “sentence” starts or ends with corresponding given value. Again, clean build the project and run the project. The index.jsp will appear as usual and click the “Jstl_Functions_Tags” to see the result.
6. Conclusion Now we have known what JSTL is and how to use it. Use JSTL anytime when possible. JSTL could empower our web app. From what we learnt, we could think of when or in what conditions JSTL could be used for. Some examples of use of JSTL are display format and internationalization
print out values from servlets response-request or even database values as in work with POJO orjava bean
validating users’ input on UI
parsing users’ input value
URL handling
You could think of other several examples andextendyour knowledge on JSTL. And apply it on your project.
2011年09月01日
Now, let’s have a look at the JSTL Function tag. Create a new JSP. Let’s name it Jstl_Functions_Tags.jsp. Then create a new link to direct to this new JSP. JSTL Functions Tags String Manipulations Do not forget to include JSTL taglib directive for prefix fn in our JSP. Here is how to import the fn JSTL taglib directive. The JSTL functions tag will mainly deal with string manipulation. Let’s try some of them. Above codes are used to set a fixed string to variable “sentence” and print it out for display. Next, please pay attention on how we are going to combine JSTL core tag with JSTL function tag. Above codes will print out the length of variable “sentence”. It will produce 35 since all characters are counted. Above codes are used to convert our “sentence” to UPPER or LOWER case. Above codes are used to trim the “sentence” and count the length of trimmed “sentence”. Trimming the sentence means that we are removing the extra spaces (if any) in the string either in front or in back. You need to know that the spaces within the string will NOT be trimmed. It should produce 33 since the spaces at the beginning and at the end will be discarded. While the sample above of fn:replace will replace all occurrences of spaces with underscores.
The fn:substring will take 3 parameters. 1stparameter is our “sentence” variable itself. 2ndparameter is for the starting point and 3rdparameter is where it must stop to return result of substring. More advanced substring functions are fn:substringAfter and fn:substringBefore. See at the screenshot to see how they work.
And the fn:indexOf will return the position of defined character in our “sentence”.
fn:contains will return boolean value true if correct and false otherwise. However, it is case sensitive and will sensitively check for the lower case and upper case of the string. fn:containsIgnoreCase work in the same way as fn:contains but it is not case sensitive which means that it will not care whether it is in lower case or in upper case. fn:startsWith and fn:endsWith will also return boolean to check whether the “sentence” starts or ends with corresponding given value. Again, clean build the project and run the project. The index.jsp will appear as usual and click the “Jstl_Functions_Tags” to see the result.
6. Conclusion Now we have known what JSTL is and how to use it. Use JSTL anytime when possible. JSTL could empower our web app. From what we learnt, we could think of when or in what conditions JSTL could be used for. Some examples of use of JSTL are display format and internationalization
print out values from servlets response-request or even database values as in work with POJO orjava bean
validating users’ input on UI
parsing users’ input value
URL handling
You could think of other several examples andextendyour knowledge on JSTL. And apply it on your project.
本文详细介绍了JSTL函数标签的基本用法及字符串操作功能,包括字符串转换、截取等,并通过实例展示了如何结合JSTL核心标签使用。
184

被折叠的 条评论
为什么被折叠?



