【问题标题】:How to get input values from spring boot thyme leaf to java class?如何从 Spring Boot thymeleaf 获取输入值到 java 类?
【发布时间】:2017-04-15 10:23:47
【问题描述】:

我正在尝试从 thymeleaf input 获取一个值到我的 java 类中。

来自 thymeleaf 的简单脚本:

<input type="text" id="datePlanted" name="datePlanted" th:value="*{datePlanted}"/>

如何将datPlanted 检索到我的 java 类中?

尝试了以下 servlet 教程:

@WebServlet("/")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        // read form fields
        String username = request.getParameter("datePlanted");

        System.out.println("date: " + datePlanted);

        // do some processing here...
        // get response writer
        PrintWriter writer = response.getWriter();
        // return response
        writer.println(htmlRespone);
    }
}

我尝试按照教程进行操作,但我不确定我应该/不应该使用 servlet。我的应用程序是使用 Spring Boot、Java 和 Thymeleaf 创建的。我究竟做错了什么?我对其他选择持开放态度,我正在尝试了解并学习如何解决这个问题。

【问题讨论】:

    标签: java spring spring-boot thymeleaf


    【解决方案1】:

    直接使用 servlet 本身并没有错,但是 Spring MVC 和 Boot 都提供了许多工具来让您的生活更轻松,并使您的代码更简洁。我将为您提供一些需要深入研究的领域,但请查看 GitHub 上的更多示例以进一步阅读。在查看文档时,请仔细查看 @ModelAttributeth:object@RequestParam

    让你有 foo.html:

    <form th:action="@{/foo}" th:object="${someBean}" method="post">
       <input type="text" id="datePlanted" name="datePlanted" />
       <button type="submit">Add</button>
    </form>
    

    表单使用 Thymeleaf 的 th:object 表示法,我们可以使用 Spring 的 ModelAttribute 方法参数来引用。

    那么你的控制器可能有:

    @Controller
    public class MyController {
    
        @GetMapping("/foo")
        public String showPage(Model model) {
            model.addAttribute("someBean", new SomeBean()); //assume SomeBean has a property called datePlanted
            return "foo";
        }
    
        @PostMapping("/foo")
        public String showPage(@ModelAttribute("someBean") SomeBean bean) {
    
            System.out.println("Date planted: " + bean.getDatePlanted()); //in reality, you'd use a logger instead :)
            return "redirect:someOtherPage";
        }    
    }
    

    请注意,在上面的控制器中,我们不需要扩展任何其他类。只需注释即可。

    如果您希望在 Java 代码中打印名为 myParam 的 URL 参数的值,Spring 让您在控制器中使用 @RequestParam 轻松完成此操作。它甚至可以将其转换为 Integer 类型,而无需您做任何额外的工作:

        @PostMapping("/foo")
        public String showPage(@ModelAttribute("someBean") SomeBean bean, @RequestParam("myParam") Integer myIntegerParam) {
    
            System.out.println("My param: " + myIntegerParam);
            return "redirect:someOtherPage";
        }    
    

    我也没有包括适当处理日期的步骤,因为这超出了这个问题的范围,但是如果遇到问题,您可以考虑在控制器中添加类似的内容:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, true));
    }
    

    编辑:SomeBean 是 POJO:

    public class SomeBean {
    
       private LocalDate datePlanted;
    
       //include getter and setter
    
    }
    

    【讨论】:

    • 感谢您如此详细的解释,我目前正在使用Spring Boot,我还没有创建或遇到任何bean。我可以做些什么来创建一个 bean?遵循您的方法“someBean”。我是否必须为此创建一个 xml 配置文件?
    • 我遵循了您的逻辑,但收到了一个模棱两可的处理程序错误。是因为我用了ModelAndView吗?
    • 嗯 - 你会想看看 SO 并酌情发布一个新问题(但我敢冒险有人已经看到你的问题,所以请在发布前好好看看) .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2018-02-20
    • 2021-08-01
    • 2020-04-20
    • 2019-04-07
    相关资源
    最近更新 更多