简体   繁体   English

无法将类型'java.lang.String'的属性值转换为必需类型'java.util.Date'

[英]Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'

I am trying to implement the full calendar plugin in my webapp using Spring MVC, Java and MySql. 我正在尝试使用Spring MVC,Java和MySql在我的webapp中实现完整的日历插件。 I have been getting this error when I try to add a date using "input type = date" in my jsp: 当我尝试在我的jsp中使用“input type = date”添加日期时,我收到此错误:

Field error in object 'event' on field 'endDate': rejected value [2018-03-13]; 
codes [typeMismatch.event.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [event.endDate,endDate]; arguments [];
default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' 
to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: 
Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat
java.util.Date for value '2018-03-13'; 
nested exception is java.lang.IllegalArgumentException: Unable to parse '2018-03-13']

In my controller class I am using SimpleDateFormat to format my date: 在我的控制器类中,我使用SimpleDateFormat来格式化我的日期:

@RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@ModelAttribute("event") Event event,
            HttpServletRequest request,ServletRequestDataBinder binder,
            ModelMap modelMap){
        try{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd/MM/yyyy");
            event.setStartDate(simpleDateFormat.parse(request.getParameter("startDate")));
            event.setEndDate(simpleDateFormat.parse(request.getParameter("endDate")));

            eventService.create(event);
            return "redirect:../event.html";
        }catch (Exception e){
            modelMap.put("event", event);
            return "event/index";
        }   
    }

And finally in my Jsp: 最后在我的Jsp中:

<fieldset>
        <legend>Event Information</legend>
    <s:form method ="post" commandName = "event"
        action="${pageContext.request.contextPath }/event/add.html">
        <table>
            <tr>
                <td>Name</td>
                <td><s:input path = "name"/></td>
            </tr>
            <tr>
                <td valign = "top">Description</td>
                <td><s:textarea path = "description" cols = "20" rows = "5" /></td>
            </tr>
            <tr>
                <td>Start Date</td>
                <td><input type = "date" name = "startDate" /></td>
            </tr>
            <tr>
                <td>End Date</td>
                <td><input type = "date" name = "endDate" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type = "submit" value = "Save" /></td>
            </tr>
        </table>
    </s:form>
    </fieldset>

Here is the DAO Implementation: 这是DAO实现:

@Repository("eventDAO")
public class EventDAOImpl implements EventDAO{

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Override
    public List<EventEntity> findAll() {
        List <EventEntity> list = null;
        Session session = null;
        Transaction transaction = null;
        try{
            session = sessionFactory.openSession();
            transaction = session .beginTransaction();
            list = session.createQuery("select e.id as id, "
                    + "e.name as title, "
                    + "DATE_FORMAT(e.startDate, '%Y-%m-%d') as start, "
                    + "DATE_FORMAT(e.endDate, '%Y-%m-%d') as end "
                    + "from Event e")
                    .setResultTransformer(
                            Transformers.aliasToBean(EventEntity.class))
                    .list();
            transaction.commit();
        }catch(Exception e){
            list = null;
            if(transaction != null){
                transaction.rollback();
            }
        }finally{
            session.close();
        }
        return list;
    }

In my Entity class I have variables saved as a date so it is 在我的Entity类中,我将变量保存为日期,所以它是

private Date endDate;

I think the issue is with parsing of the Date but I am not sure! 我认为问题在于解析Date但我不确定! Any explanation of the issue would be appreciated. 对此问题的任何解释将不胜感激。

@ModelAttribute("event") Event event will make Spring try to bind the request value 2018-03-13 to the private Date endDate field inside Event type. @ModelAttribute("event") Event event将使Spring尝试将请求值2018-03-13绑定到Event类型中的private Date endDate字段。 Your conversion code won't be invoked because the error happens before the add method is invoked. 您的转换代码将不会被调用,因为错误发生在调用add方法之前。

You need to either define a global conversion logic using PropertyEditor or Converter as described here or use org.springframework.format.annotation.DateTimeFormat to specify the format for each date field: 您需要使用此处所述的PropertyEditorConverter定义全局转换逻辑或使用org.springframework.format.annotation.DateTimeFormat指定每个日期字段的格式:

@DateTimeFormat(pattern = "yyyy-MM-dd")
// or use @DateTimeFormat(pattern = DateTimeFormat.ISO.DATE)
private Date endDate;

request.getParameter("endDate") returns String "2018-03-13" . request.getParameter("endDate")返回String "2018-03-13" You need to give matching format in SimpleDateFormat 您需要在SimpleDateFormat提供匹配的格式

simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
event.setEndDate(simpleDateFormat.parse(request.getParameter("endDate")));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 java.lang.String 类型的属性值转换为所需类型 java.util.Date - Failed to convert property value of type java.lang.String to required type java.util.Date 错误:无法将类型java.lang.String的属性值转换为属性dob的必需类型java.util.Date? - Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob? 无法将属性“ dateOfBirth”的类型“ java.lang.String”的属性值转换为所需的类型“ java.util.Date”; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; 无法将属性fromDate的java.lang.String类型的属性值转换为所需的java.util.Date类型; - Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate; 无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String 类型的值转换为所需的 java.util.Date 类型 - Failed to convert value of type java.lang.String to required type java.util.Date 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”) 无法将类型“java.lang.String”的属性值转换为属性“日期”所需的类型“java.util.Date”:它不完全是 10 个字符长 - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date': it is not exactly10characters long 如何将“java.lang.String”类型的值转换为所需的“java.util.Date”类型? - How to convert value of type 'java.lang.String' to required type 'java.util.Date'? 发送 GET 请求时无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' when sending GET request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM