我最近发现了Thymeleaf,但我很难弄清楚如何使用它。我真的需要完成这项工作。
基本上我有这个方法
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>
它将 3 个变量传递给我的控制器类,将其放入方法和渲染的对象集合中,以传递到另一个 thymeleaf 页面,在那里我可以使用所有对象使用以下方法创建表内容:
<th:foreach th:each="...">
...block to be repeated...
</th>
像这样的东西。对不起,如果我的指示不是那么好。但是我很难弄清楚从哪里开始,搜索网络只会让我更加困惑。 任何帮助都是预先提供的。
编辑:添加了我设置控制器的尝试
@RequestMapping(value="/gethosts", method=RequestMethod.GET)
public Model 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 model;
}
首先,您将表单作为帖子发送,并在控制器中获取方法。第二件事是您的控制器方法:
@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";
}
或:
@RequestMapping(value="/gethosts", method=RequestMethod.GET)
public ModelAndView renderHost(@RequestParam("Hostgroup") String Hostgroup,
@RequestParam("dataInizio") String dateInizio,
@RequestParam("dataFine" ) String dateFine) {
ModelAndView model = new ModelAndView("put your html page name");
model.addAttribute("hostGroup", Hostgroup);
model.addAttribute("dateStart", dateInizio);
model.addAttribute("dateEnd", dateFine);
return model;
}
在控制器中,您只需返回模型,而不返回有关所用模板的任何信息。 如果要执行此操作,请将表单方法更改为"GET"。 我建议您在有要在 html 表单中使用的字段时创建一个表单类。然后,您只需发送此POST方法并在控制器方法中从表单中检索信息。
更多信息在这里: https://spring.io/guides/gs/handling-form-submission/
如果您需要将其用作 POST 请求,请使用此请求。
创建主机.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";
}
处理开机自检请求的方法
@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>