java - html5 表单提交发送日期,格式为 yyyy-MM-dd

标签 java html date spring-mvc jsp

form 提交,在提交时将带有模式 yyyy-MM-dddate 发送到后端。但是有没有办法以dd-MM-yyyy发送日期。我正在使用 html5

如果将 type="text" 更改为 type="date",则可以正常工作

我正在尝试发送日期dd-MM-yyyy,前端如下所示。

enter image description here

提交时,由于发送到后端的值是 yyyy-MM-dd ,因此发生以下错误。 我的代码有问题吗?

2018-11-16 18:12:20.553 WARN 8925 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value '2018-11-09'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [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 类:

@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 日期 输入 is always in yyyy-MM-dd format (在 browsers that support it 上),无论它如何向用户显示。

您的最佳选择是更新后端以理解这种相对明确的格式,而不是更模糊的格式(首先是月份或日期)(欧洲大多数地区使用的dd/MM/yyyy,或北美大部分地区使用的MM/dd/yyyy)。我相信这意味着对您的 Controller 类的更改(但 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";
}

您可以在客户端预处理表单数据来更改格式,但我不推荐这样做。

关于java - html5 表单提交发送日期,格式为 yyyy-MM-dd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53338442/

相关文章:

java - Android 在 super View 上拖动多个 View

html - CSS3 - 使div在延迟后消失

javascript - 使用 momentjs 将 C# 日期时间格式化为 dd/MM/yyyy

javascript 日期加上一些天的问题

Java - 将指定数量的日期添加到日期

java - 如何将 SQL 查询转换为 Lambda 或 Stream (Java 8)?

java - 如何正确调用抽象类中的方法

java - 使用破折号将 Post 参数映射到 Spring Controller 中的模型

html - 搜索栏未显示在页面上

php - Bootstrap 导航栏上的帐户信息