Java 将表单值从thymeleaf传递到控件类,并将集合对象呈现到另一个页面
我最近发现了Thymeleaf,但我很难弄清楚如何使用它。我真的需要把这件事做完 基本上我有这个方法Java 将表单值从thymeleaf传递到控件类,并将集合对象呈现到另一个页面,java,spring,forms,model-view-controller,thymeleaf,Java,Spring,Forms,Model View Controller,Thymeleaf,我最近发现了Thymeleaf,但我很难弄清楚如何使用它。我真的需要把这件事做完 基本上我有这个方法 public Collection renderHost(String Hostgroup, String startDate, String endDate){ HostDao ho= new HostDao(); ho.getAllHosts(Hostgroup); ho.generateObjects(startDate, endDate); return
public Collection renderHost(String Hostgroup, String startDate, String endDate){
HostDao ho= new HostDao();
ho.getAllHosts(Hostgroup);
ho.generateObjects(startDate, endDate);
return ho.getListaOgg().values();
}
因此它返回一个宿主对象的集合
public class Host {
private String deviceName;
private String deviceIP;
private double connectionLoss;
private double responseTime;
private double packetLoss;
private String upTime;
private double cpuUtil;
private double Temp;
private double memory;
}
我希望提交此表格:
<form th:action="/gethosts" method="post">
<label>Hostgroup name:</label>
<input type="text" name="Hostgroup" id="Hostgroup">
<label>Date start</label>
<input type="date" name="startDate" id="startDate" >
<label>date end</label>
<input type="date" name="endDate" id="endDate" r>
<input type="submit" value="submit">
</form>
首先,您将表单作为post发送,并且在控制器中有get方法。第二件事是控制器方法:
@RequestMapping(value="/gethosts", method=RequestMethod.GET)
public **String** renderHost(@RequestParam("Hostgroup") String Hostgroup,
@RequestParam("dataInizio") String dateInizio,
@RequestParam("dataFine" ) String dateFine , Model model) {
model.addAttribute("hostGroup", Hostgroup);
model.addAttribute("dateStart", dateInizio);
model.addAttribute("dateEnd", dateFine);
return "put your html page name";
}
或:
在控制器中,您只需返回模型,而不需要任何关于您使用的模板的信息。
如果希望这样做,请将表单方法更改为“GET”。
我建议您在html表单中有要使用的字段时创建一个表单类。然后您只需发送此POST方法,并在控制器方法中从表单中检索信息
更多信息请点击此处:
如果您需要将此用作POST请求,请选择此项 创建Host.java类
public class Host {
private String hostGroup;
private String dateStart;
private String dateEnd;
// add getters and setters as well
}
编写一个方法来呈现页面
@RequestMapping(value = "/renderPage", method = RequestMethod.GET)
public String renderPage( Model model,HttpServletRequest request, HttpServletResponse response )
{
model.addAttribute("host", new Host() );
return "page";
}
处理POST请求的方法
@RequestMapping(value = "/gethosts", method = RequestMethod.POST)
public String renderHost( @ModelAttribute("Host") Host host )
{
}
并相应地更改html
<form th:action="/gethosts" method="post" th:object="${host}">
<label>Hostgroup name:</label>
<input type="text" name="host.hostgroup" id="Hostgroup">
<label>Date start</label>
<input type="date" name="host.startDate" id="startDate" >
<label>date end</label>
<input type="date" name="host.endDate" id="endDate" r>
<input type="submit" value="submit">
</form>
主机组名称:
开始日期
日期结束
控制器代码在哪里?你有没有试着先做一个简单的表单,就像这里的教程一样?你到底想要什么?你想知道什么,因为我不理解你的帖子。我已经更新了帖子,添加了控制器方法。我不知道如何配置表单以将值传递给我的控制器,以及如何使用控制器方法检索这3个值并将其传递给我的renderHost方法。我尝试了几次教程,但都没有达到我想要的效果。我很难弄明白怎么做。我要试一试!非常感谢你。它让事情变得有点清楚,让我知道你什么时候取得了一些进展
@RequestMapping(value = "/renderPage", method = RequestMethod.GET)
public String renderPage( Model model,HttpServletRequest request, HttpServletResponse response )
{
model.addAttribute("host", new Host() );
return "page";
}
@RequestMapping(value = "/gethosts", method = RequestMethod.POST)
public String renderHost( @ModelAttribute("Host") Host host )
{
}
<form th:action="/gethosts" method="post" th:object="${host}">
<label>Hostgroup name:</label>
<input type="text" name="host.hostgroup" id="Hostgroup">
<label>Date start</label>
<input type="date" name="host.startDate" id="startDate" >
<label>date end</label>
<input type="date" name="host.endDate" id="endDate" r>
<input type="submit" value="submit">
</form>