spring mvc 传参数

1.页面:(1)js传参数:location.href="${ctx }/forum/changeCtm.html?ctmId="+id;

(2)将内容写在form表单里面,然后用表单提交


<form method="post" action="${ctx }/forum/savePost.html" name="form1">
<input type="hidden" name="postId" value="${postId }">
<input type="hidden" name="postType" value="${forumPost.postType }" id="postType">
<input type="hidden" name="plateName" value="${forumPost.plateName }" id="plateName">
<a href="javascript:void(0);" class="comBtn" onclick="submitForm1(this);">提交</a>
</form>
如果需要验证表单,可以在js里面验证之后,然后用 "表单名".submit();来提交
function submitForm1(src){
//………………
form1.submit();
}
也可以修改表单提交的action,然后再提交。此方法可实现在同一表单内,点击不同按钮,跳转的不同的action
form1.action="${ctx }/forum/savePostReFeedback.html?postReId="+id ;
form1.submit();

后台:可以有三种方法获取

  (1)@RequestMapping(value="/changeCtm")
    public String changeCtm(Model model,HttpServletRequest request,Long ctmId){}

  直接在方法里面写参数名

  (2)@RequestMapping(value="/changeCtm")
    public String changeCtm(Model model,HttpServletRequest request,Customer customer){}

  Customer里面有属性,ctmId

  (3)@RequestMapping(value="/changeCtm")
    public String changeCtm(Model model,HttpServletRequest request){

    Long ctmId = request.getParameter("ctmId");

  }

  参数列表里面不用写,在方法体内通过原始方法获取

2.用json实现与后台交互

(1)前台:将参数写进链接里面

$.post("${ctx}/forum/savePostVote-" + postId + "-"+flag+".json", null, function(result) {
        result = eval("(" + result + ")");
        if (result.status == "true" || result.status == true) {
            
            alert(result.msg);
            state=0;
        } else {
            alert(result.msg);
            state=1;
        }
    });

后台,在映射的url里面配置对照的参数,然后在方法参数列表里面配置 处理requet uri 部分的注解PathVariable,与url参数对应即可

@RequestMapping(value="/savePostVote-{postId}-{flag}")
    public ResponseEntity<String> savePostVote(Model model , HttpServletRequest request,@PathVariable(value = "postId") Long postId,@PathVariable(value = "flag") Integer flag){}

(2)前台:将参数写进params里面,传到后台

var params = {
            id : id
        };
        $.post("${ctx}/admin/forum/deletePlate.json", params, function(
                result) {
            result = eval("(" + result + ")");
            if (result.status == "true" || result.status == true) {
                alert(result.msg);
                window.location.reload();
            }else{
                alert(result.msg);
            }
        });

后台:将参数写在参数列表内

@RequestMapping(value = "/deletePlate")
    public ResponseEntity<String> deletePlate(Model model,HttpServletRequest request,String id) throws Exception {}

 

posted @ 2014-08-26 16:37  CalronLoveRonnie  阅读(232)  评论(0编辑  收藏  举报
AmazingCounters.com