springboot form表单中 直接传递 Date类型

2步操作
1 创建 utils — StringToDateConverter

public class StringToDateConverter implements Converter<String,Date> {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String shortDateFormat = "yyyy-MM-dd";
    private static final String dateFormat2 = "yyyy/MM/dd HH:mm:ss";
    private static final String shortDateFormat2 = "yyyy/MM/dd";
    @Override
    public Date convert(String source) {
        if (StringUtils.isEmpty(source)) {
            return null;
        }
        source = source.trim();
        try {
            SimpleDateFormat formatter;
            if (source.contains("-")) {
                if (source.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat);
                }
                Date dtDate = formatter.parse(source);
                return dtDate;
            } else if (source.contains("/")) {
                if (source.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat2);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat2);
                }
                Date dtDate = formatter.parse(source);
                return dtDate;
            }
        } catch (Exception e) {
            throw new RuntimeException(String.format("parser %s to Date fail", source));
        }

        throw new RuntimeException(String.format("parser %s to Date fail", source));

    }
}

2 创建 config – webConfigBeans

@Configuration
public class WebConfigBeans {

    @Autowired
    RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @PostConstruct
    public void initEditableValidation(){
       ConfigurableWebBindingInitializer configurableWebBindingInitializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter.getWebBindingInitializer();
        if(configurableWebBindingInitializer.getConversionService()!=null){
            GenericConversionService service = (GenericConversionService) configurableWebBindingInitializer.getConversionService();
            service.addConverter(new StringToDateConverter());
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Boot可以很方便地处理表单提交。以下是一些基本的步骤: 1. 在Controller定义一个处理表单提交的方法,使用@PostMapping注解。 2. 在方法使用@ModelAttribute注解来绑定表单数据到一个对象。 3. 在方法处理表单数据,可以使用@RequestParam注解来获取单个表单字段的值,或者使用@RequestBody注解来获取整个表单数据的JSON格式。 4. 在方法返回一个视图或者重定向到另一个页面。 5. 在HTML页面使用<form>标签来定义表单,使用<input>标签来定义表单字段。 6. 在HTML页面使用Thymeleaf等模板引擎来渲染表单和处理表单提交后的结果。 以上是Spring Boot处理表单提交的基本步骤,具体实现可以根据具体需求进行调整和扩展。 ### 回答2: Spring Boot是Spring框架的顶层封装,它对于项目的构建和部署有着非常大的作用。在实际的开发,我们经常需要处理form表单提交的请求,本文将介绍如何使用Spring Boot来进行form表单提交的处理。 1. 添加依赖 在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <!--如果使用thymeleaf--> <!--如果使用其他模板引擎,则换成对应的依赖即可--> </dependency> ``` 2. 编写Controller 在Spring Boot,可以通过@Controller注解来定义Controller类,其的各种方法可以用来处理不同的请求。对于form表单提交,我们可以编写类似如下的代码: ```java @Controller public class FormController { @GetMapping("/form") public String showForm(Model model) { model.addAttribute("form", new Form()); return "form"; } @PostMapping("/submitForm") public String submitForm(@ModelAttribute("form") Form form) { //处理form表单提交后的逻辑 return "result"; } } ``` 在上面的代码,showForm方法用来处理GET请求,并将一个空的Form对象添加到模型。submitForm方法用来处理POST请求,它将form表单提交后的数据封装到Form对象,并返回result视图。 3. 编写视图 在Spring Boot,可以通过模板引擎来渲染视图。这里以Thymeleaf为例,编写一个form.html视图: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Form</title> </head> <body> <form th:object="${form}" th:action="@{/submitForm}" method="post"> <label> Name: <input type="text" th:field="*{name}"/> <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</span> </label> <label> Age: <input type="number" th:field="*{age}"/> <span th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</span> </label> <button type="submit">Submit</button> </form> </body> </html> ``` 在视图,我们使用了Thymeleaf的语法来渲染一个form表单,并使用th:field属性将表单项绑定到Form对象的属性上。在input标签,我们使用了*{name}和*{age}的写法来引用Form对象的属性,这样一来,表单项的值就会自动绑定到Form对象的属性上。同时,我们还使用了Thymeleaf的表单验证功能,当表单项的值不符合要求时,会自动提示错误信息。 4. 运行项目 完成以上步骤后,我们可以启动Spring Boot项目,并访问/form路径,即可看到一个form表单。在表单填写数据并点击提交按钮后,就会触发submitForm方法,并跳转到result视图。 Spring Boot的form表单提交处理,需要注意以下几个方面: - POST请求需要使用@PostMapping注解来处理; - 表单项需要使用th:field属性来绑定到Form对象的属性上; - 表单验证可以使用Thymeleaf的语法来实现。 ### 回答3: Spring Boot是一款集成了许多方便特性的JavaWeb框架,其包括了处理表单提交的功能。Spring Boot表单提交采用了注解的方式,使得表单提交非常方便,而且可以快速实现表单周期。 Spring Boot表单的核心是使用@Controller注解来标记处理请求的方法。在这些方法,可以使用@RequestParam注解来接收表单的参数。例如: ```java @Controller public class MyController { @RequestMapping("/submitForm") public String handleSubmitForm(@RequestParam("name") String name, @RequestParam("email") String email) { // 处理表单数据 return "resultPage"; } } ``` 在上述代码,我们使用@RequestMapping注解将方法映射到处理表单提交的URL。它包括了两个@RequestParam注解,表示需要获取表单的'name'和'email'参数。 接收表单提交后,我们可以使用这些参数来执行表单操作。最后,我们返回一个视图,显示处理的结果。 除了@RequestParam注解外,Spring Boot还提供了其他注解来处理表单提交,如@ModelAttribute注解,该注解可以将表单数据映射到一个Java对象。例如: ```java @Controller public class MyController { @RequestMapping("/submitForm") public String handleSubmitForm(@ModelAttribute("user") User user) { // 处理表单数据 return "resultPage"; } } ``` 在上述代码,我们使用@ModelAttribute注解来将表单数据映射到User对象。这就意味着,我们不需要再手动获取表单所有参数,而是可以直接使用User对象来访问所有参数。 除了以上的方式,Spring Boot还可以使用@Valid注解来验证表单数据的有效性,防止表单数据不完整或格式错误。我们只需要在User对象上加上@Valid注解,然后在处理表单数据的方法加上BindingResult参数即可。 总的来说,Spring Boot提供了非常便捷易用的方式来处理表单提交,使得我们可以快速实现Web应用的表单操作。同时,通过注解的方式,我们也可以避免繁琐的表单参数处理操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值