This time I will bring you the steps required to obtain ModelAndView with js, and what are the precautions for obtaining ModelAndView with js. The following is a practical case, let's take a look.
1 Method 1 [Valid]
Yes, the access method is the same as el
expression.
Sample code, a userId is stored in the Action of a data display request:
1 2 3 4 5 6 7 8 9 10 11 | @RequestMapping(value= "/diary" )
public ModelAndView toDiaryList(HttpSession session){
ModelAndView view = new ModelAndView( "/diary_list" );
TbUser user = (TbUser)session.getAttribute(SystemConstant.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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <script type= "text/javascript" >
var path = '<%=basePath%>' ;
var author=${userId};
$(document).ready( function (){
queryList();
});
function queryList(){
$.ajax({
type : 'POST' ,
url : path+ 'queryDiaryList' ,
data : {
author:author,
page:_currentPage,
pageSize:_pageSize,
type:$( "#queryType" ).val()
},
dataType : 'json' ,
success: function (data){
if (data.status){
showTable(data.result);
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
1 | var result = eval ( "(" + responseText + ")" );
|
Copy after login
If it is json, then
1 | var result = JSON.parse(responseText);
|
Copy after login
3 Method Three [Effective]
Add hidden fields,
1 | <input id= "autoflag" type= "hidden" value= "${autoflag}" >
|
Copy after login
Easy for js to read
1 | var passflag=document.getElementById( "autoflag" );
|
Copy after login
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:
Detailed explanation of the steps for EL to obtain context parameters
Add ellipses when the text exceeds the specified number of lines
Detailed explanation of the steps to return to the previous page from the mini program sharing page
The above is the detailed content of What steps are needed to get ModelAndView with js. For more information, please follow other related articles on the PHP Chinese website!