我基本上有以下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通過autopopulating列出的處理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 ");
}
}
注意的代碼ABOV即,它是重要的是有要求PARAM來第一次在簽名林叫「form_submit.html?位置=」你把你的模型只對有默認情況下,當前請求+位置」
如果你發佈你的控制器,它會讓事情變得更容易。 – Yevgeniy
@YevgeniyM。只是粘貼了一部分,儘管那裏沒有什麼特別的東西。真正的控制器有更多的RequestMappings指向不同的URL。表單對象只有一個廣告對象,其中包含幾個字段 – Thomas
thx,現在我們可以更全面地瞭解您正在做什麼,並且可以提供更好的答案。 – Yevgeniy