Getting started with MVC(8)

Parameter conversion

Spring MVC providesConversionServiceto convert data to bean target type from from data.

In MVC, we can use customParamConverterto convert form data to the target type.

Parameter conversion

As an example, add adueDatetoTaskentity. We need to convert the form field value(it is aString) toLocalDatetype.

  1. Create a customParamConverterProviderforLocalDate.

    @Provider
    public class CustomConverterProvider implements ParamConverterProvider {
    
        final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_DATE;
    
        @Override
        public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
    
            if (rawType.getName().equals(LocalDate.class.getName())) {
    
                return new ParamConverter<T>() {
                    @Override
                    public T fromString(String value) {
                        return value != null ? (T) LocalDate.parse(value, DATE_FORMAT) : null;
                    }
    
                    @Override
                    public String toString(T value) {
                        return value != null ? ((LocalDate) value).format(DATE_FORMAT) : "";
                    }
                };
            } 
            ....
    }
  2. Add a new field dueDate toTaskForm, which type isLocalDate.

    @NotNull
    @FormParam("duedate")
    private LocalDate dueDate;
  3. In the view, add a new form field namedduedate.

    <input type="text" id="duedate" name="duedate" class="form-control" placeholder="Due date"/>

When the form is submitted, theduedatewill be converted toLocalDatetype and bind todueDateproperty ofTaskForm.

Format

Spring provides Formatting service in the display view. eg.@DateFormatand custom Formatters.

Unfortunately, MVC does not support such features. And JSTLfmt:formatDatedoes not accept Java 8 DateTime APIs.

We can define a custom CDI beans for date formatting, and call it via expression language.

  1. CreateRequestScopedbeanFormatters.

    @RequestScoped
    @Named("formatters")
    public class Formatters {
    
        static DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_DATE;
    
        public String formatDate(LocalDate data) {
            return DATE_FORMATTER.format(data);
        }
    
    }
  2. Apply it on thedueDatein the details.jspx page.

    <dt>Due date:</dt>
    <dd>${formatters.formatDate(details.dueDate)}</dd>

The result looks like:

date formatting result

Source Codes

  1. Clone the codes from my github.com account.

    https://github.com/hantsy/ee8-sandbox/

  2. Open the mvc project in NetBeans IDE.

  3. Run it on Glassfish.
  4. After it is deployed and runging on Glassfish application server, navigate http://localhost:8080/ee8-mvc/mvc/tasks in browser.

转载于:https://my.oschina.net/hantsy/blog/661902

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值