Springmvc-checkbox

提供: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.finddevguidesas explained in the Spring MVC - Hello World Example chapter.
2 Create Java classes User, UserController under the com.finddevguidespackage.
3 Create a 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;
   private String address;
   private boolean receivePaper;

   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;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public boolean isReceivePaper() {
      return receivePaper;
   }
   public void setReceivePaper(boolean receivePaper) {
      this.receivePaper = receivePaper;
   }
}

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());
      model.addAttribute("address", user.getAddress());
      model.addAttribute("receivePaper", user.isReceivePaper());
      return "users";
   }
}

ここで、最初のサービスメソッドuser()に対して、ModelFormViewオブジェクトに「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><form:label path = "address">Address</form:label></td>
               <td><form:textarea path = "address" rows = "5" cols = "30"/></td>
            </tr>
            <tr>
               <td><form:label path = "receivePaper">Subscribe Newsletter</form:label></td>
               <td><form:checkbox path = "receivePaper"/></td>
            </tr>
            <tr>
               <td colspan = "2">
                  <input type = "submit" value = "Submit"/>
               </td>
            </tr>
         </table>
      </form:form>
   </body>
</html>

ここでは、 <form:checkboxes/> タグを使用して、HTMLチェックボックスをレンダリングしています。

たとえば-

<form:checkbox path="receivePaper"/>

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

<input id="receivePaper1" name = "receivePaper" type = "checkbox" value = "true"/>
<input type = "hidden" name = "_receivePaper" value = "on"/>

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>
         <tr>
            <td>Address</td>
            <td>${address}</td>
         </tr>
         <tr>
            <td>Subscribed to Newsletter</td>
            <td>${receivePaper}</td>
         </tr>
      </table>
   </body>
</html>

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

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

スプリングチェックボックスフォーム

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

Spring Checkbox Form Result