Home > Web Front-end > JS Tutorial > body text

js gets ModelAndView value

php中世界最好的语言
Release: 2018-06-13 10:23:43
Original
1635 people have browsed it

This time I will bring you js to get the ModelAndView value. What are the precautions for js to get the ModelAndView value? The following is a practical case, let's take a look.

1 Method 1 [Valid]

Yes, it is the same as the el expression access method.

Sample code, a userId is stored in the Action of a data display request:

 @RequestMapping(value="/diary")
  public ModelAndView toDiaryList(HttpSession session){
    ModelAndView view = new ModelAndView("/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;
  }
Copy after login

Use this userId as a query condition in the js in the page jsp file:

<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);
            //调用分页插件,初始化分页p
            pageShow("queryList",data.ext.total);
          }else{
            alert(data.description);
          }
        },
        error:function(e){
          alert("Net error ,try later.");
        }
      });
    }
    </script>
Copy after login

2 Method 2

【Effective? 】

Is the platform returning js or json? This must be clarified!

Assume that the string returned by the background is stored in responseText, then

If it is js, then

var result = eval("(" + responseText + ")");
Copy after login

If it is json, then

var result = JSON.parse(responseText);
Copy after login

3 Method Three [Effective]

Add hidden fields,

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

Easy for js to read

var passflag=document.getElementById("autoflag");
Copy after login

I believe you will read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of Vue two-way data binding examples

Vue application reference vector diagram

The above is the detailed content of js gets ModelAndView value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template