An example of the methods defined and used in jsp
- 2020-06-19 11:31:48
- OfStack
In jsp, if you need to process complex data, it is a very effective solution to define an java method at the top, placing the responsibility for processing the data on the method, and then leaving the main flow of the jsp page unaffected. Of course, you can use the js definition method, and js seems to be better at defining this method to handle foreground data, but sometimes there will be some scrambling between jsp and js or some error of not finding an object, so the fewer values they pass to each other, the better.
Look at the jsp method for defining 1 string processing:
The function of this method is to pass in 1 string and length, and return the intercepted string. The length will be counted according to two Chinese characters and one alphanumeric character. If the last character is a Chinese character, then the character will be abandoned.
To define methods in jsp, the following points need to be noted:
1. Need to use < %! % > This is the tag that defines the variable or method in jsp.
2. If you need to use the built-in object out in a method, 1 must be passed in the method's argument list, and the method must run an IO exception.
3, do not define the scope of the method, but the internal use, just use the default scope.
Look at the jsp method for defining 1 string processing:
<%!
String splitString(String str, int a) {
if (str != null && str.trim().length() > 0 && a > 0) {
// Gets the byte length of the string
int length = str.getBytes().length;
// They do not include Chinese characters
if (str.length() == length) {
// If the interception length is within the string length , it substring, Otherwise, take the string
if (a < str.length()) {
return str.substring(0, a);
} else {
return str;
}
} // Contains Chinese characters
else {
StringBuffer sb = new StringBuffer();
// Intercept algorithm Traversal string , And monitoring a value
for (int i = 0; i < str.length() && a > 0; i++) {
// If it's Chinese characters 2 A length.
if (str.charAt(i) >= '\u4e00' && str.charAt(i) <= '\u9fa5') {
// If it's a character and it's not the last 1 Characters, just add, otherwise not add
if (a > 1) {
sb.append(str.charAt(i));
a -= 2;
}
// Not just Chinese characters 1 A length of
} else {
sb.append(str.charAt(i));
a--;
}
}
return sb.toString();
}
}
return " Input is wrong ";
}
%>
The function of this method is to pass in 1 string and length, and return the intercepted string. The length will be counted according to two Chinese characters and one alphanumeric character. If the last character is a Chinese character, then the character will be abandoned.
To define methods in jsp, the following points need to be noted:
1. Need to use < %! % > This is the tag that defines the variable or method in jsp.
2. If you need to use the built-in object out in a method, 1 must be passed in the method's argument list, and the method must run an IO exception.
3, do not define the scope of the method, but the internal use, just use the default scope.