Digression: The biggest headache for Web developers who have worked on the front-end of a large website is the layout of the front-end homepage. The biggest headache is the interception of the title characters, although FN: substring, however, the performance rate and support for Chinese characters are not perfect. In fact, the customer's requirements are too high, but we should be rigorous in doing things, especially in our line .. With the guidance of our boss, we learned how to customize El functions. Haha, we have implemented it today.
Development steps:
1. Add static methods
/** <Br/> * @ Author X. ING's blog <br/> */<br/> public class stringutils {</P> <p>/** <br/> * @ Param STR: <br/> * Source string <br/> * @ Param width: <br/> * string's byte width <br/> * @ Param ellipsis: <br/> * a string added to abbreviate string bottom <br/> * @ return String object <br/> * @ Author X. ING's blog <br/> * @ See <a href = "http://blog.csdn.net/xxd851116/" mce_href = "http://blog.csdn.net/xxd8 51116/"> X. ING's blog </a> <br/> */<br/> Public static string abbreviate (string STR, int width, string ellipsis) {<br/> If (STR = NULL | "". equals (STR) {<br/> return ""; <br/>}</P> <p> int D = 0; // byte length <br/> int n = 0; // char length <br/> for (; n <Str. length (); N ++) {<br/> d = (INT) Str. charat (n)> 256? D + 2: D + 1; <br/> If (D> width) {<br/> break; <br/>}</P> <p> If (D> width) {<br/> N = N-ellipsis. length ()/2; <br/> return Str. substring (0, N> 0? N: 0) + ellipsis; <br/>}</P> <p> return STR = Str. substring (0, n); <br/>}< br/>
2. Add the tag library descriptor TLD file (TAG library descriptor) and register the static method
<? XML version = "1.0" encoding = "UTF-8"?> </P> <p> <taglib xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" <br/> Version = "2.0"> </P> <p> <description> stringutils </description> <br/> <tlib-version> 1.0 </tlib-version> <br/> <short-Name> stringutils </short-Name> <br/> <URI> http://blog.csdn.net/xxd851116 </uri> </P> <p> <function> <br/> <description> stringutils </description> <br/> <Name> abbreviate </Name> <br/> <function-class> COM. xxd. utils. stringutils </function-class> <br/> <function-signature> JAVA. lang. string abbreviate (Java. lang. string, Int, Java. lang. string) </function-signature> <br/> </function> <br/> </taglib>
3. Import the tag library on the JSP page (you do not need to describe the location of the TLD in Web. xml. The servlet container can automatically search for the TLD file, so it can be packaged as a jar file)
<% @ Taglib uri = "http://blog.csdn.net/xxd851116" prefix = "fnx" %>
Note:
(1) The URI here must be consistent with the URI in the TLD file. Do not describe this? Haha.
(2) prefix is the prefix used by the function.
4. Page use, super simple!
$ {Fnx: abbreviate ('source string to intercept', 'byte length', 'append characters ')}
5. Test Results
[Code]
<Body> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*0 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*1 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*2 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*3 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*4 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*5 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*6 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*7 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*8 ,'')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people in China D F', 2*9 ,'')} <br/> $ {fnx: abbreviate ('a I am B, C, C, E, F ', 2*0 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*1 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*2 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*3 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*4 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*5 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*6 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*7 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is C, C, E, F, 2*8 ,'... ')} <br/> <br/>$ {fnx: abbreviate ('a I B is c, e people from China D F', 2*9 ,'... ')} <br/> </body>
Result]
A
A I B
A I B Yes
A I B is C
A I B is C,
A I B is C, medium d
A I B is C, China d
A I B is C, China d e
A: I am B, C, E, F
...
...
A I...
A I B...
A I B is...
A I B is c...
A I B is C, medium...
A I B is C, medium d...
A I B is C, China d...
A: I am B, C, E, F
[Source code download]: http://download.csdn.net/source/1618386
Article original, reproduced please indicate the source: http://blog.csdn.net/xxd851116