java 如何从spring boot thyme Leaf获取输入值到java类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40899494/
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
How to get input values from spring boot thyme leaf to java class?
提问by Jesse
I am trying to get a value from thymeleaf input
into my java class.
我正在尝试从 thymeleaf 中获取一个值input
到我的 java 类中。
Simple script from thymeleaf:
来自 thymeleaf 的简单脚本:
<input type="text" id="datePlanted" name="datePlanted" th:value="*{datePlanted}"/>
How would I be able to retrieve datPlanted
into my java class?
我如何才能检索datPlanted
到我的 Java 类?
Attempted the following servlet tutorial:
尝试了以下 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);
}
}
I tried to follow a tutorial but I am not sure that I should/should not be using servlets. My Application is created with Spring Boot, Java, and Thymeleaf. What am I doing wrong? I am open to other options, I am trying to understand and learn how to solve this problem.
我尝试按照教程进行操作,但不确定是否应该/不应该使用 servlet。我的应用程序是使用 Spring Boot、Java 和 Thymeleaf 创建的。我究竟做错了什么?我对其他选择持开放态度,我试图理解并学习如何解决这个问题。
采纳答案by vphilipnyc
There's nothing inherently wrong with using servlets directly but both Spring MVC and Boot offer many tools to make your life easier and make your code more concise. I'll provide you with some areas to delve into, but do take a look at more examples on GitHub for further reading. When you're looking through the docs, take a close look at @ModelAttribute
, th:object
, and @RequestParam
.
直接使用 servlet 并没有本质上的错误,但是 Spring MVC 和 Boot 都提供了许多工具来使您的生活更轻松并使您的代码更简洁。我将为您提供一些需要深入研究的领域,但请查看 GitHub 上的更多示例以进一步阅读。当你翻翻文档,需要仔细看@ModelAttribute
,th:object
和@RequestParam
。
Let's you have foo.html:
让我们拥有 foo.html:
<form th:action="@{/foo}" th:object="${someBean}" method="post">
<input type="text" id="datePlanted" name="datePlanted" />
<button type="submit">Add</button>
</form>
The form uses Thymeleaf's th:object
notation which we can refer to using Spring's ModelAttribute
method parameter.
该表单使用 Thymeleaf 的th:object
表示法,我们可以使用 Spring 的ModelAttribute
方法参数来引用它。
Then your Controller could have:
那么你的控制器可能有:
@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";
}
}
Note that in the above controller, we didn't need to extend any other class. Just annotate and you're set.
请注意,在上面的控制器中,我们不需要扩展任何其他类。只需注释即可。
If you're looking to print the value of a URL parameter named myParam
in your Java code, Spring let's you do this easily in your controller with @RequestParam
. It can even convert it to things like Integer
types without any extra work on your side:
如果您想打印myParam
在 Java 代码中命名的 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";
}
I'm also not including the steps to handle dates appropriately since that is out of scope for this question, but you can look at adding something like this to your controller if you come across issues:
我也不包括适当处理日期的步骤,因为这超出了这个问题的范围,但是如果遇到问题,您可以考虑将类似的内容添加到您的控制器中:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, true));
}
Edit: SomeBean
is a POJO:
编辑:SomeBean
是一个POJO:
public class SomeBean {
private LocalDate datePlanted;
//include getter and setter
}