Springmvc-password

提供:Dev Guides
移動先:案内検索

Spring MVC-パスワードの例

次の例では、Spring Web MVCフレームワークを使用してフォームでパスワードを使用する方法を説明します。 まず、動作するEclipse IDEを用意し、次の手順に従って、Spring Web Frameworkを使用して動的フォームベースのWebアプリケーションを開発します。

Step Description
1 Create a project with a name HelloWeb under a package com.finddevguides as explained in the Spring MVC - Hello World chapter.
2 Create Java classes User, UserController under the com.finddevguidespackage.
3 Create view files user.jsp, users.jsp under jsp sub-folder.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

User.java

package com.finddevguides;

public class User {

   private String username;
   private String password;

   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
}

UserController.java

package com.finddevguides;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class UserController {

   @RequestMapping(value = "/user", method = RequestMethod.GET)
   public ModelAndView user() {
      return new ModelAndView("user", "command", new User());
   }

   @RequestMapping(value = "/addUser", method = RequestMethod.POST)
   public String addUser(@ModelAttribute("SpringWeb")User user,
      ModelMap model) {
      model.addAttribute("username", user.getUsername());
      model.addAttribute("password", user.getPassword());

      return "users";
   }
}

ここでは、最初のサービスメソッド* user()*で、ModelAndViewオブジェクトに「command」という名前の空のUserオブジェクトを渡しました。これは、<form:formを使用している場合、Springフレームワークが「command」 > JSPファイルのタグ。 そのため、user()メソッドが呼び出されると、user.jspビューが返されます。

2番目のサービスメソッド* addUser()*は、HelloWeb/addUser URLのPOSTメソッドに対して呼び出されます。 送信された情報に基づいて、モデルオブジェクトを準備します。 最後に、「users」ビューがサービスメソッドから返され、users.jspがレンダリングされます。

user.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring MVC Form Handling</title>
   </head>
   <body>

      <h2>User Information</h2>
      <form:form method = "POST" action = "/HelloWeb/addUser">
         <table>
            <tr>
               <td><form:label path = "username">User Name</form:label></td>
               <td><form:input path = "username"/></td>
            </tr>
            <tr>
               <td><form:label path = "password">Age</form:label></td>
               <td><form:password path = "password"/></td>
            </tr>
            <tr>
               <td colspan = "2">
                  <input type = "submit" value = "Submit"/>
               </td>
            </tr>
         </table>
      </form:form>
   </body>
</html>

ここでは、<form:password/>タグを使用してHTMLパスワードボックスをレンダリングしています。 たとえば-

<form:password path = "password"/>

次のHTMLコンテンツをレンダリングします。

<input id = "password" name = "password" type = "password" value = ""/>

users.jsp

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring MVC Form Handling</title>
   </head>
   <body>
      <h2>Submitted User Information</h2>
      <table>
         <tr>
            <td>Username</td>
            <td>${username}</td>
         </tr>
         <tr>
            <td>Password</td>
            <td>${password}</td>
         </tr>
      </table>
   </body>
</html>

ソースファイルと構成ファイルの作成が完了したら、アプリケーションをエクスポートします。 アプリケーションを右クリックし、[エクスポート]→[WARファイル]オプションを使用して、HelloWeb.warファイルをTomcatのwebappsフォルダーに保存します。

次に、Tomcatサーバーを起動し、標準ブラウザーを使用してwebappsフォルダーから他のWebページにアクセスできることを確認します。 URL –http://localhost:8080/HelloWeb/userを試してください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。

Spring Password Form

必要な情報を送信したら、送信ボタンをクリックしてフォームを送信します。 Spring Webアプリケーションで問題がなければ、次の画面が表示されます。

Spring Password Form Result