Spring MVCのテキストエリアの例

Spring MVCテキストエリアの例

Spring MVCでは、<form:textarea />を使用してHTMLテキストエリアフィールドをレンダリングします。 例えば、

    

次のHTMLコードをレンダリングします

    

このチュートリアルでは、Springのフォームタグ「textarea」からrender a HTML textareaを使用して「address」を格納する方法を示します。 さらに、バリデーターを追加して、フォームの送信中にテキストエリアが空にならないようにします。

1. コントローラ

フォーム値を処理するためのSimpleFormController

ファイル:TextAreaController.java

package com.example.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.example.customer.model.Customer;

public class TextAreaController extends SimpleFormController{

    public TextAreaController(){
        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. モデル

textarea値を格納するCustomerオブジェクト。

ファイル:Customer.java

package com.example.customer.model;

public class Customer{

    String address;
    //getter and setter methods for address
}

3. フォームバリデーター

フォームバリデータークラスを作成し、ValidationUtilsクラスを使用して、「アドレス」が空でないことを確認します。空でない場合は、対応するリソースバンドル(プロパティファイル)から「required.address」メッセージを取得します。

ファイル:CustomerValidator.java

package com.example.customer.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.example.customer.model.Customer;

public class CustomerValidator 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, "address",
             "required.address", "Field name is required.");

    }
}

ファイル:message.properties

required.address = Address is required!

4. View

Springのフォームタグ「textarea」を使用してHTMLテキスト領域をレンダリングし、いくつかのCSSスタイルを配置してエラーメッセージを強調表示するJSPページ。

ファイル:CustomerForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>






    

Spring's form textarea example

Address :

フォームが送信された場合、成功したページをレンダリングし、送信されたtextarea値を表示します。

ファイル:CustomerSuccess.jsp



    

Spring's form textarea example

Address : ${customer.address}

5. Spring Beanの構成

それをすべてリンクしてください〜



  

    
        
        

        
        
            
        
    

    
    
        
    

    
        
            /WEB-INF/pages/
        
        
            .jsp
        
    

6. Demo

ページへのアクセス - http://localhost:8080/SpringMVCForm/textarea.htm

SpringMVC-TextArea-Example-1

フォームの送信中にtextarea値が空の場合、エラーメッセージを表示して強調表示します。

SpringMVC-TextArea-Example-2

フォームが正常に送信された場合、送信されたtextarea値を表示するだけです。

SpringMVC-TextArea-Example-3

ソースコードをダウンロード

ダウンロード–SpringMVCForm-TextArea-Example.zip(9KB)