Tomcat7でJSF2.0アプリケーションを実行しています。フォームの検証にJavaScriptを使用したいと思います。これが私の問題の単純化されたバージョンです:
2つのjsfファイルindex.xhtmlとresult.xhtmlと、javascriptコードを含む1つのファイルがあります。両方のxhtmlファイルはアプリのルートフォルダーにあります。
index.xhtmlの場合:
<h:head>
<h:outputScript name="script.js" library="js" />
</h:head>
<h:body>
<h:form>
<h:inputSecret id="password" />
<h:inputSecret id="confirm" />
<h:commandButton
type="button"
value="go"
action="result"
onclick="checkPassword(this.form)" />
</h:form>
</h:body>
script.js:
function checkPassword(form){
var password = form[form.id+":password"].value;
var confirm = form[form.id+":confirm"].value;
alert(password + " " + confirm);
if(password == confirm){
form.submit();
}else{
alert("Password doesn't match");
}
}
result.xhtmlの内容は重要ではありません。このindex.xhtmlを実行すると、期待どおりの結果が得られます。テキストボックスに同じ値を入力してボタンをクリックすると、checkPassword関数がトリガーされますが、ブラウザーはindex.xhtmlのままであり、result.xhtmlに転送します。私は何が間違っているのですか?ウォークアラウンドだけでなく、それが機能しない理由にも興味があります。手伝って頂けますか?