私は、jsp、javascriptなどを使って自分のFIRST webappで作業しています。最初のページは "login.jsp"です。そのページの中には、サインアップページへのリンクがあります。ユーザーがサーブレットを介して登録フォームを送信すると、彼はログインページに戻ります。しかし、私は、ページが読み込まれると同時にログインページ内に成功の警告が表示されることを願っています。divを表示するにはどうすればいいですか?次のページにリダイレクトしますか?
これを可能にする正確な方法はわかりません。私は、サーブレットの "notifier"のようなリクエスト属性を設定することを考えました。そのため、ログインページは、ユーザがサインアップから来たものかどうかを知っています。しかし、私はその属性を「読む」方法がわかりません。私はすでにjavascript、jspなどを使ってみました。何も私のために働いていません。 さらに、RequestDispatcher.forward()を使ってリダイレクトして、NotifierをSession属性として設定する必要があるかどうかはわかりません。 (ここで私は別の問題があり、ログインページのスタイルシートがロードされません。)
これは、サインアップページの一部です:
//....
<form action="/servlet/UpdateUser" method="POST" accept-charset="utf-8" class="form" role="form">
//....
<button class="btn btn-lg btn-block signup-btn" type="submit" name="signup">Create my account</button>
</form>
//....
サーブレットのいくつか:
//servlet
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
if (req.getParameter("signup") != null) {
User user = new User();
//user setters and register
//..
//Should i use (?):
//req.setAttribute("SignUp", true);
} //..
//rest of the servlet
resp.sendRedirect("../login.jsp");
}
一部ログインページ:
//Rest of Login Page
//Here is something i've tried.
<script type="text/javascript">
window.onload = function() {
var b = <%request.getAttribute("SignUp");%>;
if (b) document.getElementById('success').style.display = 'block';
else document.getElementById('success').style.display = 'none';
}
</script>
//Rest of Login Page
<body>
//..
<div class="alert alert-success" id="success">
<strong>Success!</strong> You can now login.
</div>
//..
</body>
//Rest of Login Page
PD:これは私の最初の質問です。
'req.setAttribute(" signup "、true)'と 'req.getRequestDispathcer(" ../ login.jsp ").forward(req、resp)'を使用しましたが、問題はlogin.jspの属性は、最初に加えて、属性は存在しません。 ' – BrunoLedesma
とにかくdivが表示されています。 – BrunoLedesma
文字列を属性 'req.setAttribute(" signup "、" true ")'として渡し、 'if(su ===" true ")'のように比較してみるべきです。 オブジェクト全体を属性としても送信でき、 '$ {objectName.fieldName} 'を使ってオブジェクトのフィールドにアクセスできることに注意してください。 – solstinger