Spring MVCパスワードの例
Spring MVCでは、
<form:password/>
タグを使用してHTMLパスワードフィールドをレンダリングできます。例えば、
<form:password path="password"/>
次のHTMLコードをレンダリングします
<input id="password" name="password" type="password" value=""/>
このチュートリアルでは、Springのフォームタグ ”
password
“を使用して、2つのHTMLパスワード** フィールド( “password”と “confirmPassword”)をレンダリングする方法を示します。
さらに、両方のパスワードフィールドにバリデーターチェックを追加します。空白にしないでください。また、「パスワード」フィールドは「confirmPasswod」フィールドと一致する必要があります。
1.コントローラー
フォーム値を扱うための
SimpleFormController
。
File:PasswordController.java
package com.mkyong.customer.controller;
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 PasswordController extends SimpleFormController{
public PasswordController(){
setCommandClass(Customer.class);
setCommandName("customerForm");
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
Customer customer = (Customer)command;
return new ModelAndView("CustomerSuccess","customer",customer);
}
}
2.モデル
パスワード値を格納するCustomerオブジェクト。
File:Customer.java
package com.mkyong.customer.model;
public class Customer{
String password;
String confirmPassword;
//getter and setter methods for password and confirmPassword
}
3.フォームバリデーター
パスワードバリデータークラスを作成して、両方のパスワードフィールドを確認します。
空白であってはいけません。 “password”と “confirmPassword”は一致する必要があります。
それ以外の場合は、リソース・バンドル(プロパティー・ファイル)から対応するメッセージを取得します。
File:PasswordValidator.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 PasswordValidator 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) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
"required.password", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
"required.confirmPassword", "Field name is required.");
Customer cust = (Customer)target;
if(!(cust.getPassword().equals(cust.getConfirmPassword()))){
errors.rejectValue("password", "notmatch.password");
}
}
}
ファイル:message.properties
required.password = Password is required! required.passwordConfirm = Confirm password is required! notmatch.password = Password and Conform password is not match!
4.ビュー
Springのフォームタグ ”
password
“を使用して2つのHTMLパスワードフィールドをレンダリングし、エラーメッセージを強調するCSSスタイルを入れるJSPページ。
File:CustomerForm.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<style>
.error {
color: #ff0000;
}
.errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
</head>
<body>
<h2>Spring's form password example</h2>
<form:form method="POST" commandName="customerForm">
<form:errors path="** " cssClass="errorblock" element="div"/>
<table>
<tr>
<td>Password :</td>
<td><form:password path="password"/>
</td>
<td><form:errors path="password" cssClass="error"/>
</td>
</tr>
<tr>
<td>Confirm Password :</td>
<td><form:password path="confirmPassword"/>
</td>
<td><form:errors path="confirmPassword" cssClass="error"/>
</td>
</tr>
<tr>
<td colspan="3"><input type="submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
フォームが送信された場合は、成功したページをレンダリングし、送信されたパスワード値を表示します。
File:CustomerSuccess.jsp
<html>
<body>
<h2>Spring's form password example</h2>
Password : ${customer.password}
<br/> Confirm Password : ${customer.confirmPassword}
</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.PasswordController">
<property name="formView" value="CustomerForm"/>
<property name="successView" value="CustomerSuccess"/>
<!-- Map a validator -->
<property name="validator">
<bean class="com.mkyong.customer.validator.PasswordValidator"/>
</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/password.htm

フォームを送信しているときに「パスワード」と「確認パスワード」が一致しない場合は、エラーメッセージを表示して強調表示します。

フォームが正常に送信された場合は、提示されたパスワード値を表示するだけです。
