📄 javascriptvalidatortag.java
字号:
String message =
Resources.getMessage(messages, locale, va, field);
message = (message != null) ? message : "";
jscriptVar = this.getNextVar(jscriptVar);
results.append(
" this."
+ jscriptVar
+ " = new Array(\""
+ getFormClientId()
+ ":"
+ field.getKey()
+ "\", \""
+ message
+ "\", ");
results.append("new Function (\"varName\", \"");
Map vars = field.getVars();
// Loop through the field's variables.
Iterator varsIterator = vars.keySet().iterator();
while (varsIterator.hasNext()) {
String varName = (String) varsIterator.next();
Var var = (Var) vars.get(varName);
String varValue = var.getValue();
String jsType = var.getJsType();
// skip requiredif variables field, fieldIndexed,
// fieldTest, fieldValue
if (varName.startsWith("field")) {
continue;
}
if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
results.append(
"this."
+ varName
+ "="
+ ValidatorUtils.replace(
varValue,
"\\",
"\\\\")
+ "; ");
} else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(
jsType)) {
results.append(
"this."
+ varName
+ "=/"
+ ValidatorUtils.replace(
varValue,
"\\",
"\\\\")
+ "/; ");
} else if (Var.JSTYPE_STRING.equalsIgnoreCase(
jsType)) {
results.append(
"this."
+ varName
+ "='"
+ ValidatorUtils.replace(
varValue,
"\\",
"\\\\")
+ "'; ");
// So everyone using the latest format doesn't
// need to change their xml files immediately.
} else if ("mask".equalsIgnoreCase(varName)) {
results.append(
"this."
+ varName
+ "=/"
+ ValidatorUtils.replace(
varValue,
"\\",
"\\\\")
+ "/; ");
} else {
results.append(
"this."
+ varName
+ "='"
+ ValidatorUtils.replace(
varValue,
"\\",
"\\\\")
+ "'; ");
}
}
results.append(" return this[varName];\"));\n");
}
results.append(" } \n\n");
}
} else if ("true".equalsIgnoreCase(staticJavascript)) {
results.append(this.getStartElement());
if ("true".equalsIgnoreCase(htmlComment)) {
results.append(htmlBeginComment);
}
}
}
if ("true".equalsIgnoreCase(staticJavascript)) {
results.append(getJavascriptStaticMethods(resources));
}
if (form != null
&& ("true".equalsIgnoreCase(dynamicJavascript)
|| "true".equalsIgnoreCase(staticJavascript))) {
results.append(getJavascriptEnd());
}
JspWriter writer = pageContext.getOut();
try {
writer.print(results.toString());
} catch (IOException e) {
throw new JspException(e.getMessage());
}
return (EVAL_BODY_TAG);
}
/**
* Release any acquired resources.
*/
public void release() {
super.release();
bundle = Globals.MESSAGES_KEY;
formName = null;
page = 0;
methodName = null;
staticJavascript = "true";
dynamicJavascript = "true";
htmlComment = "true";
cdata = "true";
src = null;
formClientId = null;
}
/**
* Returns the opening script element and some initial javascript.
*/
protected String getJavascriptBegin(String methods) {
StringBuffer sb = new StringBuffer();
String name =
formName.substring(0, 1).toUpperCase()
+ formName.substring(1, formName.length());
sb.append(this.getStartElement());
if (this.isXhtml() && "true".equalsIgnoreCase(this.cdata)) {
sb.append("<![CDATA[\r\n");
}
if (!this.isXhtml() && "true".equals(htmlComment)) {
sb.append(htmlBeginComment);
}
sb.append("\n var bCancel = false; \n\n");
if (methodName == null || methodName.length() == 0) {
sb.append(
" function validate"
+ name
+ "(form) { "
+ " \n");
} else {
sb.append(
" function "
+ methodName
+ "(form) { "
+ " \n");
}
sb.append(" if (bCancel) \n");
sb.append(" return true; \n");
sb.append(" else \n");
// Always return true if there aren't any Javascript validation methods
if (methods == null || methods.length() == 0) {
sb.append(" return true; \n");
} else {
sb.append(" return " + methods + "; \n");
}
sb.append(" } \n\n");
return sb.toString();
}
protected String getJavascriptStaticMethods(ValidatorResources resources) {
StringBuffer sb = new StringBuffer();
sb.append("\n\n");
Iterator actions = resources.getValidatorActions().values().iterator();
while (actions.hasNext()) {
ValidatorAction va = (ValidatorAction) actions.next();
if (va != null) {
String javascript = va.getJavascript();
if (javascript != null && javascript.length() > 0) {
sb.append(javascript + "\n");
}
}
}
return sb.toString();
}
/**
* Returns the closing script element.
*/
protected String getJavascriptEnd() {
StringBuffer sb = new StringBuffer();
sb.append("\n");
if (!this.isXhtml() && "true".equals(htmlComment)){
sb.append(htmlEndComment);
}
if (this.isXhtml() && "true".equalsIgnoreCase(this.cdata)) {
sb.append("]]>\r\n");
}
sb.append("</script>\n\n");
return sb.toString();
}
/**
* The value <code>null</code> will be returned at the end of the sequence.
* ex: "zz" will return <code>null</code>
*/
private String getNextVar(String input) {
if (input == null) {
return "aa";
}
input = input.toLowerCase();
for (int i = input.length(); i > 0; i--) {
int pos = i - 1;
char c = input.charAt(pos);
c++;
if (c <= 'z') {
if (i == 0) {
return c + input.substring(pos, input.length());
} else if (i == input.length()) {
return input.substring(0, pos) + c;
} else {
return input.substring(0, pos) + c + input.substring(pos,
input.length() - 1);
}
} else {
input = replaceChar(input, pos, 'a');
}
}
return null;
}
/**
* Replaces a single character in a <code>String</code>
*/
private String replaceChar(String input, int pos, char c) {
if (pos == 0) {
return c + input.substring(pos, input.length());
} else if (pos == input.length()) {
return input.substring(0, pos) + c;
} else {
return input.substring(0, pos) + c + input.substring(pos,
input.length() - 1);
}
}
/**
* Constructs the beginning <script> element depending on
* xhtml status.
*/
private String getStartElement() {
StringBuffer start =
new StringBuffer("<script type=\"text/javascript\"");
// there is no language attribute in xhtml
if (!this.isXhtml()) {
start.append(" language=\"Javascript1.1\"");
}
if (this.src != null) {
start.append(" src=\"" + src + "\"");
}
start.append("> \n");
return start.toString();
}
/**
* Returns true if this is an xhtml page.
*/
private boolean isXhtml() {
return TagUtils.getInstance().isXhtml(this.pageContext);
}
/**
* Returns the cdata setting "true" or "false".
* @return String - "true" if JavaScript will be hidden in a CDATA section
*/
public String getCdata() {
return cdata;
}
/**
* Sets the cdata status.
* @param cdata The cdata to set
*/
public void setCdata(String cdata) {
this.cdata = cdata;
}
private String formClientId = null;
/**
* <p>Return the <code>clientId</code> of the form component for which
* we are rendering validation Javascript.</p>
*
* @exception IllegalStateException if we are not nested inside a
* UIComponentTag with a child FormComponent matching our form name
*/
private String getFormClientId(){
// Return any cached value
if (formClientId != null) {
return (formClientId);
}
// Locate our parent tag that is a component tag
Tag parent = getParent();
while (parent != null) {
if (parent instanceof UIComponentTag) {
break;
}
parent = parent.getParent();
}
if (parent == null) {
throw new IllegalArgumentException
("Not nested inside a UIComponentTag");
}
// Are we nested inside our corresponding form tag?
UIComponent parentComponent =
((UIComponentTag) parent).getComponentInstance();
if (parentComponent instanceof FormComponent) {
if (formName.equals(
parentComponent.getAttributes().get("beanName"))) {
formClientId = parentComponent.getClientId
(FacesContext.getCurrentInstance());
return (formClientId);
}
}
// Scan the children of this tag's component
Iterator kids = ((UIComponentTag) parent).
getComponentInstance().getChildren().iterator();
while (kids.hasNext()) {
UIComponent kid = (UIComponent) kids.next();
if (!(kid instanceof FormComponent)) {
continue;
}
if (formName.equals(kid.getAttributes().get("beanName"))) {
formClientId =
kid.getClientId(FacesContext.getCurrentInstance());
return (formClientId);
}
}
throw new IllegalArgumentException
("Cannot find child FormComponent for form '" + formName + "'");
}
// ---------------------------------------------------------- Static Methods
private static boolean struts11;
static {
try {
JavascriptValidatorTag.class.getClassLoader().loadClass
("org.apache.struts.taglib.TagUtils");
struts11 = false;
} catch (Exception e) {
struts11 = true;
}
}
/**
* <p>Return <code>true</code> if we are running on top of Struts 1.1</p>
*/
private static boolean isStruts11() {
return struts11;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -