Home  >  Article  >  Web Front-end  >  Detailed explanation of the steps to obtain the ModelAndView value in js

Detailed explanation of the steps to obtain the ModelAndView value in js

php中世界最好的语言
php中世界最好的语言Original
2018-04-28 10:58:225834browse

This time I will bring you a detailed explanation of the steps to obtain the ModelAndView value with js. What are the precautions for obtaining the ModelAndView value with js? Here is a practical case, let's take a look.

Can't the return value of ModelAndView be received in JS? Does it have to be received in a JSP page?

1 Method 1 [valid]

#Yes, follow the el

expression The access method is the same.

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;
  }

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>

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 + ")");
If it is json, then

var result = JSON.parse(responseText);

3 Method Three [Effective]

Add hidden fields,

<input id="autoflag" type="hidden" value="${autoflag}">
Easy for js to read

var passflag=document.getElementById("autoflag");
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How EL gets the context parameter value (with code)

How to do the el expression in js Non-empty judgment

The above is the detailed content of Detailed explanation of the steps to obtain the ModelAndView value in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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