TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
先看下面的jsf页面:
<HTML>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<link href="styles.css" rel="stylesheet" type="text/css" />
<head>
<f:loadBundle basename="com.corejsf.messages" var="msgs"/>
<title>
<h:outputText value="#{msgs.windowTitle}"/>
</title>
</head>
<body>
<h:form id="registerForm">
<table>
<tr>
<td>
<h:outputText value="#{msgs.namePrompt}" styleClass="emphasis" />
</td>
<td>
<h:inputText/>
</td>
</tr>
<tr>
<td>
<h:outputText value="#{msgs.passwordPrompt}"/>
</td>
<td>
<h:inputSecret id="password"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="#{msgs.confirmPasswordPrompt}"/>
</td>
<td>
<h:inputSecret id="passwordConfirm"/>
</td>
</tr>
</table>
<h:commandButton type="button" value="Submit Form" onclick="checkPassword(this.form)"/>
</h:form>
</body>
<script type="text/javascript">
<!--
function checkPassword(form) {
var password = form["registerForm:password"].value;
var passwordConfirm = form["registerForm:passwordConfirm"].value; if(password == passwordConfirm)
form.submit();
else
alert("Password and password confirm fields don"t match");
}
-->
</script>
</f:view>
</html>
这个页面使用JavaScript来确认密码字段是否匹配密码确认字段,表单名为registerForm,两个密码输入 组件名为password和passwordConfirm,当单击按钮时,将调用JavaScript函数checkPassword(form),如果密码不匹配,会显示JavaScript对话框,如果匹配,则会提交表单。
要注意的是:不能在JavaScript函数中使用如下类似语法来访问表单组件:
documents.forms.registerForm.password
而应使用:
documents.forms.registerForm["registerForm:password"].value
这是因为JSF解释上面<h:form id="registerForm">...</h:form>一段时会生成如下代码:
<form id="registerForm" method="post" action="/javascript/index.faces;jsessionid=7A029269C9ECF42CF553C9B52C824E2E"
enctype="application/x-www-form-urlencoded"> <table>
<tr>
<td>
<span class="emphasis">Name:</span>
</td>
<td>
<input type="text" name="registerForm:_id2" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input id="registerForm:password" type="password" name="registerForm:password" value="" />
</td>
</tr>
<tr>
<td>
Confirm Password:
</td>
<td>
<input id="registerForm:passwordConfirm" type="password" name="registerForm:passwordConfirm" value="" />
</td>
</tr>
</table>
<input type="button" name="registerForm:_id5" value="Submit Form" onclick="checkPassword(this.form)" />
<input type="hidden" name="registerForm" value="registerForm" /></form>
JSF产生的所有表单控件都有符合formName:componentName格式的名称,这里的formName表示控件的表单的
名称,而componentName表示控件名称。如果没有指定id属性,则JSF框架会自动创建标识符,就象上面的HTML
片段中的按钮一样。因此,要访问上面的密码字段,必须使用下列方法:
documents.forms.registerForm["registerForm:password"].value
源码下载:http://file.javaxxz.com/2014/10/9/234807375.zip |
|