如何在Struts2中填充Calendar对象

时间:2017-04-27 14:36:31

标签: struts2 struts2-json-plugin

我想用json数据填充Struts2中的java.util.Calendar字段。 尝试这个时,我得到以下struts错误:

<html>
<body>
    <h2>Struts Problem Report</h2>
    <p>
    Struts has detected an unhandled exception:
    </p>


<div id="exception-info">
<table>
    <tr>
        <td><strong>Messages</strong>:</td>
        <td>
                    <li>Incompatible types for property setValue</li>
        </td>
    </tr>
    <tr>
        <td><strong>File</strong>:</td>
        <td>org/apache/struts2/json/JSONPopulator.java</td>
    </tr>
    <tr>
        <td><strong>Line number</strong>:</td>
        <td>146</td>
    </tr>

</table>
</div>


<div id="stacktraces">
<hr />
<h3>Stacktraces</h3>
<div class="stacktrace" style="padding-left: 0em">
    <strong>org.apache.struts2.json.JSONException: Incompatible types for property setValue</strong>
    <div>
    <pre>
    org.apache.struts2.json.JSONPopulator.convert(JSONPopulator.java:146)
    org.apache.struts2.json.JSONPopulator.populateObject(JSONPopulator.java:109)
    org.apache.struts2.json.JSONPopulator.convert(JSONPopulator.java:139)
    org.apache.struts2.json.JSONPopulator.populateObject(JSONPopulator.java:109)
    org.apache.struts2.json.JSONPopulator.convert(JSONPopulator.java:139)
    org.apache.struts2.json.JSONPopulator.populateObject(JSONPopulator.java:109)
    org.apache.struts2.json.JSONPopulator.convert(JSONPopulator.java:139)
    org.apache.struts2.json.JSONPopulator.populateObject(JSONPopulator.java:109)
    org.apache.struts2.json.JSONInterceptor.intercept(JSONInterceptor.java:134)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
    </pre>
    </div>
</div>
</div>
</div>
</body>
</html>

我有一个包含日历字段的Bean ...

public class DateTimeField {

    private Calendar value;
    private DateFormatCode formatCode;

    public Calendar getValue() {
        return value;
    }

    public void setValue(final Calendar value) {
        this.value = value;
    }

    public DateFormatCode getFormatCode() {
        return formatCode;
    }

    public void setFormatCode(final DateFormatCode formatCode) {
        this.formatCode = formatCode;
    }

}

...以及应填充该字段的Struts Action:

@Action(interceptorRefs = { @InterceptorRef(value = "json") })
public class FillDateTimeFieldAction extends BasicAuthorizedAction {

    private static final Logger logger = LoggerFactory.getLogger(FillDateTimeFieldAction.class);

    private DateTimeField dateTimeField;

    @Override
    public String execute(User user) {
        return SUCCESS;
    }

    public DateTimeField getDateTimeField() {
        return dateTimeField;
    }

    public void setDateTimeField(final DateTimeField dateTimeField) {
        this.dateTimeField= dateTimeField;
    }
}

JSON Payload如下所示:

{"dateTimeField":{"formatCode":"STANDARD_DATE","value":"2017-03-29T00:00:00"}}

我尝试通过将以下注释应用于bean或动作设置器来使用自定义类型转换器。

@TypeConversion(converter="com.test.CalendarTypeConverter")

在发生错误之前,不会调用操作和TypeConverter

编辑1:添加了CalendarTypeConverter

public class CalendarTypeConverter extends StrutsTypeConverter {

    private static final Logger logger = LoggerFactory.getLogger(CalendarTypeConverter.class);
    private static final DateFormat FORMAT = new SimpleDateFormat("ddMMyyyy");

    @Override
    public Object convertFromString(Map arg0, String[] values, Class arg2) {
        logger.info("convertFromString called");
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(FORMAT.parse(values[0]));
            return calendar;
        } catch (final ParseException e) {
            throw new TypeConversionException("Cannot parse string \"" + values[0], e);
        } catch (final IllegalArgumentException e) {
            throw new TypeConversionException("Cannot parse string \"" + values[0], e);
        }
    }

    @Override
    public String convertToString(Map arg0, Object value) {
        try {
            logger.info("convertToString called");
            return DateFormat.getDateInstance(DateFormat.MEDIUM).format(((Calendar)value).getTime());
        } catch (Exception e) {
            throw new TypeConversionException(e.getMessage());
        }
    }

}

0 个答案:

没有答案