java 带有 Spring MVC 的 jQuery datepicker - 将日期从视图传递到控制器发送空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37304922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:23:47  来源:igfitidea点击:

jQuery datepicker with Spring MVC - Passing date from view to controller sends null

javajqueryjspspring-mvc

提问by Full Stack Developer - Java

After submitting the form , I see all the values being passed properly to the controller except date which is passed as null(The date selection in the form enters a proper date in the input field) I tried adding but it did not help. I also tried removing "path" and replacing it with "name" in the "" but it throws error saying "path" attribute is missing. I tried following but no luck-
1. In spring web mvc application date value is received as NULL in controller after reading from date-picker on jsp page
2. http://blog.teamextension.com/date-binding-in-spring-mvc-1321

提交表单后,我看到所有值都正确传递给控制器​​,除了作为 null 传递的日期(表单中的日期选择在输入字段中输入正确的日期)我尝试添加,但没有帮助。我还尝试删除“路径”并将其替换为“”中的“名称”,但它会引发错误,指出缺少“路径”属性。我尝试以下,但没有luck-
1.在弹簧Web MVC应用程序的日期值被接收作为NULL在控制器从日期选择器读jsp页面上后
2. http://blog.teamextension.com/date-binding-in-spring -mvc-1321

jQuery: $(document).ready(function() { $("#datepicker").datepicker({dateFormat:"yy-mm-dd"}); });

jQuery: $(document).ready(function() { $("#datepicker").datepicker({dateFormat:"yy-mm-dd"}); });

JSP:

JSP:

<div class="plLabelSearch" id="hiddenField">Due Date:</div> 
<div class="plinput"><form:input type="text" id="datepicker" path="dueDate" placeholder="yyyy-mm-dd"/></div>

Model:

模型:

@Column(name = "DUE_DATE")  
@DateTimeFormat(pattern = "yyyy/mm/dd")  
private Date dueDate;

Controller:

控制器:

@RequestMapping(value="/lock", method = RequestMethod.POST)  
 public @ResponseBody Status lockDevice(@ModelAttribute("adminTransaction") @Validated AdminTransaction adminTransaction, BindingResult result, Model model, Locale locale,Map<String, Object> map, HttpServletRequest request) {
     try {
         /*.......CODE.........*/
         adminTransactionDO.setDueDate(adminTransaction.getDueDate());
        /*.......CODE.........*/
         }
         return new Status("success", "Transaction inserted Successfully !");  
          } catch (Exception e) {  
           return new Status("error", e.toString());  
          }
}

采纳答案by Kuldeep S Chauhan

It looks like your :

它看起来像你的:

@Column(name = "DUE_DATE")  
@DateTimeFormat(pattern = "yyyy/mm/dd")  
private Date dueDate;

Is the problem. Spring is unable to convert it into Date.

是问题。Spring 无法将其转换为 Date。

Instead try

而是尝试

 private String dueDate;

回答by Full Stack Developer - Java

I resolved the issue by passing the date in RequestParam to controller:

我通过将 RequestParam 中的日期传递给控制器​​解决了这个问题:

public @ResponseBody Status lockDevice(@ModelAttribute("adminTransaction") @Validated AdminTransaction adminTransaction, BindingResult result, Model model, Locale locale,Map<String, Object> map, HttpServletRequest request, @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date dueDate) {
     try {
         /*.......CODE.........*/
         SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
         Date date = dt1.parse(dt1.format(dueDate));
         adminTransactionDO.setDueDate(date);
         /*.......CODE.........*/
         }
         return new Status("success", "Transaction inserted Successfully !");  
          } catch (Exception e) {  
           return new Status("error", e.toString());  
          }
}

回答by geobudex

You could define a transient entity fields for holding the date in String format(Transient are fields that do not participate in persistence and their values are never stored in the database). Then map this field to the path attribute of the input fields of the form in the view. Using the SimpleDateFormat utility within the controller you could convert the date string value into actual date format to be stored into the database.

您可以定义一个临时实体字段来保存字符串格式的日期(临时字段是不参与持久性的字段,它们的值永远不会存储在数据库中)。然后将此字段映射到视图中表单的输入字段的路径属性。使用控制器中的 SimpleDateFormat 实用程序,您可以将日期字符串值转换为要存储到数据库中的实际日期格式。

MODAL

莫代尔

@Column(name = "DUE_DATE")  
@DateTimeFormat(pattern = "yyyy/mm/dd")  
private Date dueDate;

@Transient
private String dueDateString;

//Your getters, setters and the rest

JSP

JSP

<div class="plinput"><form:input type="text" id="datepicker" 
path="dueDateString" placeholder="yyyy/mm/dd"/></div> 

CONTROLLER

控制器

@RequestMapping(value="/lock", method = RequestMethod.POST)  
 public @ResponseBody Status lockDevice(@ModelAttribute("adminTransaction") @Validated AdminTransaction adminTransaction, BindingResult result, Model model, Locale locale,Map<String, Object> map, HttpServletRequest request) {



SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");


        try {
                Date dueDateVal;
        dueDateVal = dateFormat.parse(adminTransaction.getDueDateString());
        adminTransaction.setDueDate(dueDateVal);

        } catch (ParseException e) {


                e.printStackTrace();
        }



try {
         /*.......CODE.........*/
         adminTransactionDO.setDueDate(adminTransaction.getDueDate());
        /*.......CODE.........*/
         }
         return new Status("success", "Transaction inserted Successfully !");  
          } catch (Exception e) {  
           return new Status("error", e.toString());  
          }

}

回答by Arun

You can try this

你可以试试这个

<div class="plLabelSearch" id="hiddenField">Due Date:</div> 
<div class="plinput"><form:input type="date" id="datepicker"/></div>   

In your javascript use

在你的 javascript 中使用

var date = document.getElementbyId("datepicker").value;

if you want the date to be sent to servlet do as follows...

如果您希望将日期发送到 servlet,请执行以下操作...

<form name="dateform" action="ServletName" method="post">
<input type="date" name="InputDate" id="datepicker"/>
<input type="submit" value="Submit"></form>

in your servlet post method....

在您的 servlet post 方法中....

String date = request.getParameter("InputDate");