Spring MVCドロップダウンボックスの例
Spring MVCでは、formタグ –
<form:select/>
、
<form:option/>
または
<form:options/>
を使用してHTMLドロップダウンボックスを描画します。次の例を参照してください。
….//SimpleFormController
protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
Map<String,String> country = new LinkedHashMap<String,String>();
country.put(“US”, “United Stated”);
country.put(“CHINA”, “China”);
country.put(“SG”, “Singapore”);
country.put(“MY”, “Malaysia”);
referenceData.put(“countryList”, country);
}
=== 1. <form:select/> $ \ {countryList}を含むドロップボックスボックスを生成します。
<form:select path=”country” items=”${countryList}”/>
** HTMLコード**
<select id=”country” name=”country”>
<option value=”US”>United Stated</option>
<option value=”CHINA”>China</option>
<option value=”SG”>Singapore</option>
<option value=”MY”>Malaysia</option>
</select>
=== 2. <form:options/> <form:options/>はselectタグで囲まなければなりません。
<form:select path=”country”>
<form:options items=”${countryList}”/>
</form:select>
** HTMLコード**
<select id=”country” name=”country”>
<option value=”US”>United Stated</option>
<option value=”CHINA”>China</option>
<option value=”SG”>Singapore</option>
<option value=”MY”>Malaysia</option>
</select>
=== 3. <form:option/> ** <form:option/> ** はselectタグで囲まなければならず、単一のselectオプションをレンダリングする必要があります。次の組み合わせを参照してください。
<form:select path=”country”>
<form:option value=”NONE” label=”— Select —“/>
<form:options items=”${countryList}”/>
</form:select>
** HTMLコード**
<select id=”country” name=”country”>
<option value=”NONE”>— Select —</option>
<option value=”US”>United Stated</option>
<option value=”CHINA”>China</option>
<option value=”SG”>Singapore</option>
<option value=”MY”>Malaysia</option>
</select>
=== 4.リストボックス リストボックスを表示するには、selectタグに "** multiple = true ** "属性を追加します。
<form:select path=”country” items=”${countryList}” multiple=”true”/>
** HTMLコード** 、国の選択を処理するための隠された値。
<select id=”country” name=”country” multiple=”multiple”>
<option value=”US”>United Stated</option>
<option value=”CHINA”>China</option>
<option value=”SG”>Singapore</option>
<option value=”MY”>Malaysia</option>
</select>
<input type=”hidden” name=”__country” value=”1″/>
** ドロップダウンボックスの値を選択** ** ドロップダウンボックス、リストボックス、または "選択"オプションの場合、 "** パス** "または "** プロパティ** "が "** オプションキー値** を選択する"自動的に選択されます。 === 完全なドロップダウンボックスの例 完全なSpring MVCドロップダウンボックスの例を考えてみましょう: === 1.モデル ドロップダウンボックスの値を格納する顧客モデルクラス。 __File:Customer.java__
package com.mkyong.customer.model;
public class Customer{
String country; String javaSkills;
public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getJavaSkills() { return javaSkills; } public void setJavaSkills(String javaSkills) { this.javaSkills = javaSkills; } }
=== 2.コントローラー フォームのドロップダウンボックス値を扱うための `SimpleFormController '。デフォルトのドロップダウンボックスの選択値として、Javaスキルの「** Spring ** 」を作成します。 __File:DropDownBoxController.java__
package com.mkyong.customer.controller;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.mkyong.customer.model.Customer;
public class DropDownBoxController extends SimpleFormController{
public DropDownBoxController(){ setCommandClass(Customer.class); setCommandName("customerForm"); }
@Override protected Object formBackingObject(HttpServletRequest request) throws Exception {
Customer cust = new Customer();
//make "Spring" as the default java skills selection cust.setJavaSkills("Spring");
return cust;
}
@Override protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
Customer customer = (Customer)command; return new ModelAndView("CustomerSuccess","customer",customer);
}
protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
Map<String,String> country = new LinkedHashMap<String,String>(); country.put("US", "United Stated"); country.put("CHINA", "China"); country.put("SG", "Singapore"); country.put("MY", "Malaysia"); referenceData.put("countryList", country);
Map<String,String> javaSkill = new LinkedHashMap<String,String>(); javaSkill.put("Hibernate", "Hibernate"); javaSkill.put("Spring", "Spring"); javaSkill.put("Apache Wicket", "Apache Wicket"); javaSkill.put("Struts", "Struts"); referenceData.put("javaSkillsList", javaSkill);
return referenceData; } }
=== 3.バリデーター "** country ** "と "** javaSkills ** "ドロップダウンボックスが選択されていることを確認する簡単なフォームバリデータ。 ファイル:ドロップダウンボックスValidation.java
package com.mkyong.customer.validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.mkyong.customer.model.Customer;
public class DropDownBoxValidator implements Validator{
@Override public boolean supports(Class clazz) { //just validate the Customer instances return Customer.class.isAssignableFrom(clazz); }
@Override public void validate(Object target, Errors errors) {
Customer cust = (Customer)target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "javaSkills", "required.javaSkills");
if("NONE".equals(cust.getCountry())){ errors.rejectValue("country", "required.country"); } } }
__ファイル:message.properties__
required.country = Please select a country!
required.javaSkills = Please select a java Skill!
=== 4.ビュー Springのformタグ** <form:select/> ** 、** <form:option/> ** および** <form:options/> ** の使用方法を示すJSPページ。 ファイル:Customer Form.jsp
<%@ taglib prefix=”form” uri=”http://www.springframework.org/tags/form”%>
<html>
<head>
<style>
.error {
color: #ff0000;
}
color: #000; background-color: #ffEEEE; border: 3px solid #ff0000; padding: 8px; margin: 16px; } </style> </head>
<body>
<h2>Spring’s form select, option, options example</h2>
<form:form method="POST" commandName="customerForm"> <form:errors path="** " cssClass="errorblock" element="div"/> <table>
<tr> <td>Country :</td> <td><form:select path="country"> <form:option value="NONE" label="--- Select ---"/> <form:options items="${countryList}"/> </form:select> </td> <td><form:errors path="country" cssClass="error"/></td> </tr> <tr> <td>Java Skills :</td> <td><form:select path="javaSkills" items="${javaSkillsList}" multiple="true"/></td> <td><form:errors path="javaSkills" cssClass="error"/></td> </tr>
<tr> <td colspan="3"><input type="submit"/></td> </tr> </table> </form:form>
</body>
</html>
提示された値を表示するにはJSTLを使用します。 ファイル:Customer Success.jsp
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>
<html>
<body>
<h2>Spring’s form select, option, options example</h2>
Country : ${customer.country} <br/> Java Skills : ${customer.javaSkills} <br/>
</body>
</html>
=== 5. Spring Beanの設定 すべてのリンク〜
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”>
;
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="com.mkyong.customer.controller.DropDownBoxController"> <property name="formView" value="CustomerForm"/> <property name="successView" value="CustomerSuccess"/>
<!-- Map a validator --> <property name="validator"> <bean class="com.mkyong.customer.validator.DropDownBoxValidator"/> </property> </bean>
<!-- Register the Customer.properties --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="message"/> </bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
=== 6.デモ ページにアクセスする - ** http://localhost:8080/SpringMVCForm/dropdownbox.htm ** [SpringMVC-DropDownBox-Example-1、width = 640、height = 1、title = "SpringMVC-DropDownBox-Example-1" 300] ユーザーがフォームの送信中にドロップダウンボックスの値を選択しなかった場合は、エラーメッセージを表示して強調表示します。 [SpringMVC-DropDownBox-Example-2]、width = 639、height = 364] フォームが正常に送信された場合は、送信されたドロップダウンボックスの値を表示するだけです。 [SpringMVC-DropDownBox-Example-3]、width = 639、height = 3、title = "SpringMVC-DropDownBox-Example-3" 244] === ソースコードをダウンロードする ダウンロードする - link://wp-content/uploads/2010/08/SpringMVCForm-DropDownBox-Example.zip[SpringMVCForm-DropDownBox-Example.zip](10KB) link://tag/dropdown/[dropdown]link://タグ/spring-mvc/[spring mvc]