man*_*anu 1 controller jsp-tags spring-mvc taglib
我在JSP中有一些代码如下:
<c:iterate name="list" id="payment" index="idx">
<tr class="gRowEven"
_paid="<c:write name="payment" property="paid"/>">
Run Code Online (Sandbox Code Playgroud)
现在我的问题是我想基于变量调用控制器中的方法_paid.我可以做一个request.setAttribute("_paid", _paid)
我假设它会起作用.但我不应该这样做.所以我想知道是否有其他方法可以做到这一点?
您可以将该值传递给隐藏的输入字段(在表单提交上)
<form action='your controller' method='POST'>
...
<input type='hidden' id='newfield' name='newfield' value=''/>
</form>
Run Code Online (Sandbox Code Playgroud)
那么你的控制器可以检索它的值(使用request.getParameter('newfield'),或MVC框架提供的方法)
如果您的控制器接受GET请求,或者只是附加到URL
顺便说说,
request.setAttribute("_paid",_paid);
Run Code Online (Sandbox Code Playgroud)
可能不适合你.因为此调用仅在加载页面时执行,而不是在您提交页面时执行.此外,当您提交页面时,它将有一个新的新请求
编辑:(这就是我的意思'将该值传递给隐藏的输入字段(在表单提交上)')
<script type='text/javascript'>
function updateHiddenField() {
var trPaid = document.getElementById('trPaid'); //assume tr field's id is trPaid
//if tr doesn't have an id, you can use other dom selector function to get tr
//element, but adding an id make things easier
var value = trPaid.getAttribute('_paid');
document.getElementById('newfield').value = value;
document.getElementById('form1').submit();
return false;
}
</script>
<form action='your controller' id='form1' method='POST'
onsubmit='javascript:updateHiddenField();'>
...
</form>
Run Code Online (Sandbox Code Playgroud)
然后_paid的值将在newfield参数中与请求一起传递
以下是使用Spring MVC时获取参数的方法
@Controller
@RequestMapping("/blah")
public class MyController {
@RequestMapping(value="/morepath" method = RequestMethod.POST)
public ModelAndView test(@RequestParam("newfield") int paid) {
logger.debug("here's the value of paid: " + paid);
}
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29200 次 |
| 最近记录: |