我基本上有以下jsp,它检索弹簧模型广告列表$ {myAdverts}附带的广告对象。
我希望当单击其中一个时,将发布请求提交回我的控制器,但带有广告对象的实例。这有可能吗?
这是我的 JSP 代码:
<xc:forEach var="advertisement" items="${myAdverts}" varStatus="stats">
<li>
<a class="furtherinfo-link" onclick="javascript:submitJoin(${stats.count})" >${advertisement.getName()}</a>
</li>
</xc:forEach>
<form:form id="myform" method="POST" action="form_submit.html" commandName="myForm" name="MyForm">
<form:input id="advertisementObj" path="advertisementObj" type="hidden"/>
</form:form>
这是我尝试用 javascript 发回帖子,灵感来自 Spring MVC 和 javascript 中自动填充列表的处理:
javascript代码
<script src="js/webmenu_nav.js"></script>
<script type="text/javascript">
function submitJoin(position){
$('#advertisementObj').val("myAdverts["+position+"]");
document.MyForm.submit();
}
</script>
该代码的当前行为是,我的 Controller 对象中的 post 方法总是得到一个空的 AdvertisementObj。
控制器对象非常简单,但以防万一它是其代码的一部分:
@Controller
public class MyController {
@RequestMapping(value="/show_advertisements.html",method = RequestMethod.GET)
public ModelAndView showAdv(@RequestParam(value="response", required=false) String incomingResponse) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("response", incomingResponse);
List<AdvertisementRecord> adverts = methodThatReturnsList();
model.put("myAdverts", adverts);
MyForm jform = new MyForm();
model.put("myForm", jform);
return new ModelAndView("show_advertisements", model) ;
}
@RequestMapping(value = "/form_submit.html", method = RequestMethod.POST)
public ModelAndView formSubmit(MyForm myForm, BindingResult result, Map model){
if(null != myForm.getAdvertisement())
return showPage("adver " + myForm.getAdvertisement().getId());
else
return showPage("null advertisement on join");
}
}
解决方案!!
解决方案代码片段
JSP代码:
<xc:forEach var="advertisement" items="${myAdverts}" varStatus="stats">
<li>
<a class="furtherinfo-link" onclick="javascript:submitForm(${stats.count})" >${advertisement.getName()}</a>
</li>
</xc:forEach>
<form:form method="POST" id="theForm" action="form_submit.html" modelAttribute="myAdverts" name="MyForm">
</form:form>
javascript:
<script src="js/webmenu_nav.js"></script>
<script type="text/javascript">
function submitForm(position){
$('#theForm').attr("action","form_submit.html?position="+position);
document.MyForm.submit();
}
</script>
</head>
控制器:
@Controller
@SessionAttributes("myAdverts")
public class MyController {
@RequestMapping(value="/show_advertisements.html",method = RequestMethod.GET)
public ModelAndView showAdv(@RequestParam(value="response", required=false) String incomingResponse) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("response", incomingResponse);
List<AdvertisementRecord> adverts = methodThatReturnsList();
model.put("myAdverts", adverts);
//MyForm jform = new MyForm();
//model.put("myForm", jform);
return new ModelAndView("show_advertisements", model) ;
}
@RequestMapping(value = "/form_submit.html", method = RequestMethod.POST)
public ModelAndView formSubmit(@RequestParam("position") final int position, @ModelAttribute("adverts") @Valid List<AdvertisementRecord> adverts, BindingResult result, Map model){
if(null != adverts && null != adverts.get(position))
return showPage("adver " + adverts.get(position).getId());
else
return showPage("null advertisement ");
}
}
请注意上面的代码,在我调用“form_submit.html?position="+position" 时,将请求参数放在签名中很重要