浅谈js获取ModelAndView值的问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

浅谈js获取ModelAndView值的问题

蒋大帅   2020-05-16 我要评论

JS当中不能接收ModelAndView的返回值吗?一定要在JSP页面中才能接收吗?

1 方法一 【有效】

可以的,跟el表达式访问方式一样。

示例代码,一个数据展示请求的Action中存入一个userId:

 @RequestMapping(value="https://img.qb5200.com/download-x/diary")
  public ModelAndView toDiaryList(HttpSession session){
    ModelAndView view = new ModelAndView("https://img.qb5200.com/download-x/diary_list");
    TbUser user = (TbUser)session.getAttribute(SystemConstant.CURRENT_USER);
    //set info of current user
    if(user!=null){
      Integer id = user.getId();
      view.addObject("userId",id);
    }
    return view;
  }

在页面jsp文件中js中使用这个userId作为查询条件:

<script type="text/javascript">
    var path = '<%=basePath%>';
    var author=${userId};
    $(document).ready(function(){
      queryList();
    });
    function queryList(){
      $.ajax({
        type : 'POST',
        url : path+'queryDiaryList', //通过url传递name参数
        data : {
            author:author,
            page:_currentPage,
            pageSize:_pageSize,
            type:$("#queryType").val()
          },
        dataType : 'json',
        success:function(data){
          if(data.status){
            showTable(data.result);
            //调用分页插件,初始化分页Div
            pageShow("queryList",data.ext.total);
          }else{
            alert(data.description);
          }
        },
        error:function(e){
          alert("Net error ,try later.");
        }
      });
    }
    </script>

2 方法二

【有效?】

台返回的是js,还是json?这个一定要搞清楚!

假设后台返回的字符串存储在responseText里,那么

如果是js,就

var result = eval("(" + responseText + ")");

如果是json,就

var result = JSON.parse(responseText);

3 方法三 【有效】

加入隐藏字段,

<input id="autoflag" type="hidden" value="${autoflag}"> 

便于js读取 

var passflag=document.getElementById("autoflag");

以上这篇浅谈js获取ModelAndView值的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们