考虑以下问题。如何在Spring mvc中为同一个url控制两个操作?
用户已选择通过点击创建文档创建一个文档,然后他将数据写入到文件。用于创建文档的URL是/ document/save。
对于后续的写了起来,现有的文件必须保存,而不是创建一个新的。
这里是我的代码。
@Controller
public MyController implements Controller, TemplateTypeAware
{
@RequestMapping("/document/save")
public String saveOrCreateDocument(@ModelAttribute DocumentWrapper wrapper, ModelAndView m)
{
if(m.getModel().get("document_id")==null)
{
Document doc=createDocument(wrapper);
m.addObject("document_id",doc.getId());
}
else
{
saveDocument(m.getModel().get("document_id"), wrapper);
}
return documentView;
}
}
模板:
<form>
<input type="hidden" name="document_id" th:value="*{document_id}"/>
<!-- other fields -->
</form>
这里的问题是,我得到document_id
总是null
。有没有解决这个问题的方法?
在此先感谢。希望你能尽快回复。
它为什么会比'null'其他什么吗?如果您要发送现有文档的请求,则需要在请求中标识该文档。 – 2014-09-26 15:49:38
您是否将document_id作为请求参数发送? – 2014-09-26 15:52:21
第一次访问页面时,必须完成创建,因此不会有'document_id',为了后续保存,我已将'document_id'模型添加为对象。当我设置该对象时,我将返回与更新后的问题中相同的视图。 – user12458 2014-09-26 16:02:19