package javahello; import javax.servlet.http.*; import org.apache.struts.action.*; /** * アクションフォームBeanクラス * ActionFormを継承します */ public final class SendMessageForm extends ActionForm { private String name; private String email; private String message; public void setName(String name) { this.name = toSJIS(name); } public String getName() { return name; } public void setEmail(String email) { this.email = toSJIS(email); } public String getEmail() { return email; } public void setMessage(String message) { this.message = toSJIS(message); } public String getMessage() { return message; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // エラーを格納するActionErrorsオブジェクトを作成 ActionErrors errors = new ActionErrors(); // 入力チェック if (name == null || name.equals("")) { // ActionErrorオブジェクトを作成して格納 // ActionErrorオブジェクトのコンストラクタの第1引数は // リソースファイルに記述してあるキーを渡す errors.add("name" , new ActionError("error.name.required")); } if (email == null || email.equals("")) { errors.add("email", new ActionError("error.email.required")); } if (message == null || message.equals("")) { errors.add("message" , new ActionError("error.message.required")); } return errors; } /** * iso-8859-1からShift_JISへの文字コード変換メソッド */ private String toSJIS(String str) { try { str = new String(str.getBytes("iso-8859-1"), "Shift_JIS"); } catch (Exception e) { e.printStackTrace(); } return str; } }