java - html5表单提交发送日期与模式yyyy-MM-dd
问题描述
form
date
在提交时提交带有 patten的发送yyyy-MM-dd
到后端。但是有没有办法发送日期dd-MM-yyyy
。我在用html5
如果更改type="text"
比这正常工作type="date"
我正在尝试发送 date dd-MM-yyyy
,前端如下所示。
提交时,因为发送到后端的值是yyyy-MM-dd
并且由于发生以下错误。我的代码有问题吗?
2018-11-16 18:12:20.553 WARN 8925 --- [nio-8080-exec-2] .wsmsDefaultHandlerExceptionResolver:无法绑定请求元素:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:无法转换键入“java.lang.String”到所需类型“java.time.LocalDate”;嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] 的值'2018-11-09';嵌套异常是 java.lang.IllegalArgumentException:值的解析尝试失败 [2018-11-09]
下面是代码:
jsp:
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css"
href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
<!--
<spring:url value="/css/main.css" var="springCss" />
<link href="${springCss}" rel="stylesheet" />
-->
<c:url value="/css/main.css" var="jstlCss" />
<link href="${jstlCss}" rel="stylesheet" />
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Spring Boot</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>Spring Boot Web JSP Example</h1>
<h2>Message: ${message}</h2>
<form action="/get" method="post">
<input type="date" name="date" pattern="\d{1,2}/\d{1,2}/\d{4}"><!-- -->
<input type="submit" value="submit">
</form>
</div>
</div>
<!-- /.container -->
<script type="text/javascript"
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
控制器类:
@Controller
public class MyController {
// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";
@RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
@RequestMapping(value="/get",method={RequestMethod.GET,RequestMethod.POST})
public String get(Map<String, Object> model, @RequestParam(value="date") @DateTimeFormat(pattern ="dd-MM-yyyy") LocalDate date) {
System.out.println("get" + date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
return "welcome";
}
//this is for testing
@RequestMapping(value="/get1",method={RequestMethod.GET,RequestMethod.POST})
public String get1(Map<String, Object> model, @RequestParam(value="date") String date) {
System.out.println("get1 "+ date);
return "welcome";
}
}
解决方案
value
输入的总是采用格式(在支持它的浏览器上date
),无论它如何显示给用户。yyyy-MM-dd
您最好的选择是更新后端以了解这种相对明确的格式,而不是首先使用月份或日期的更模糊的格式(dd/MM/yyyy
如在欧洲大部分地区MM/dd/yyyy
使用,或在北美大部分地区使用)。我相信这将意味着对您的控制器类进行这种更改(但 Spring MVC 不是我的职责):
@RequestMapping(value="/get",method={RequestMethod.GET,RequestMethod.POST})
public String get(Map<String, Object> model, @RequestParam(value="date") @DateTimeFormat(pattern ="dd-MM-yyyy") LocalDate date) {
System.out.println("get" + date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
return "welcome";
}
您可以在客户端预处理表单数据以更改格式,但我不建议这样做。
推荐阅读
- vba - VBA MailItem.Add 只触发一次?
- html - div 溢出未正确对齐
- javascript - HTML CHECKBOX:从 div 写入/删除值
- redmine - 无法从 From Gmail 在 Red-Mine 中打开票证
- java - java中Enum如何取main方法?
- windows - 有什么方法可以知道 Microsoft Windows 序列号/产品密钥详细信息
- c# - 将excel导入数据库c#asp.net
- scala - 在不使用反向方法的情况下获取列表的最后两个元素?[斯卡拉]
- powershell - 将删除的文件记录到文本文件
- dialogflow-es - Google Dialogflow:获取卡片的 ID