Using jQuery to get the list collection stored in the session

I found a lot of information on the Internet that jQuery can't get session. If you have a better way, please leave a mes...

I found a lot of information on the Internet that jQuery can't get session. If you have a better way, please leave a message

Here is using jQuery to send Ajax request to get session in the background

No code in jsp

js code

<script type="text/javascript">
//Here is a private method to get the root path of the project. This method is at the bottom of js. You can view it if you are interested var basePath = getRootPath(); $(function(){
//To get the session as soon as the page is loaded getSession(); });function getSession(){ $.ajax({ url:basePath + "/getSession", type:"GET", async:false,//false The representative is only waiting ajax Only after execution success:function(result){
//Here, the session data is printed out on the console console.log(result);
//If you want to get the specific data in the list, you can click the content under it to get it in turn. For details, you can see the content printed on the console. That level is very clear /* console.log(result.data); console.log(result.data.userSession); console.log(result.data.userSession[0]); */ } }); } /** * Get the root path of the project, such as: http: / / localhost: 8083 / SSM dynamic * @returns */ function getRootPath(){ //Get the current web address, such as: http://localhost:8083/ssm-dynamic/jsp/jsonList.jsp var curWwwPath=window.document.location.href; //Obtain the directory after the host address, such as: ssm-dynamic/jsp/jsonList.jsp var pathName=window.document.location.pathname; var pos=curWwwPath.indexOf(pathName); //Obtain the host address, such as: http://localhost:8080 var localhostPaht=curWwwPath.substring(0,pos); //Acquisition band"/"Project name of, such as:/ssm-dynamic var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); return(localhostPaht+projectName); } </script>

controller code

Code to store session

@RequestMapping(value="/",method=RequestMethod.GET) public String sayHello(HttpSession session){ List<User> users = loginService.selectAll(); session.setAttribute("users", users); return "login"; }

Get the code of the session

//Specify the url address, which should be consistent with the url of the above Ajax request; specify the request method, which should be consistent with the type of the above Ajax request
@RequestMapping(value="/getSession",method=RequestMethod.GET)
//Add @ ResponseBody to return json data @ResponseBody public Msg getSession(HttpSession session){
//Get previously added session data List<User> users = (List<User>) session.getAttribute("users");
//Put the session data in the custom msg class, which will be shown below return Msg.success().add("userSession", users); }

Msg.java

import java.util.HashMap; import java.util.Map; /** * Generic return class * @author ws * */ public class Msg { /** * Status code * 100:Success * 200:fail */ private int code; /** * Tips */ private String msg; /** * Data the user wants to return to the browser */ private Map<String, Object> data = new HashMap<String,Object>(); public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Map<String, Object> getData() { return data; } public void setData(Map<String, Object> data) { this.data = data; } /** * Processing successful * @return */ public static Msg success(){ Msg msg = new Msg(); msg.setCode(100); msg.setMsg("Processing successful"); return msg; } /** * Processing failed * @return */ public static Msg fail(){ Msg msg = new Msg(); msg.setCode(200); msg.setMsg("Processing failed"); return msg; } /** * Add data with key value pair * @param key * @param value * @return */ public Msg add(String key, Object value) { this.getData().put(key, value); return this; } }

In this way, you can see the effect in the browser console (usually press F12 to open it)

Now it's done

You can get session if you have a way that is not applicable to ajax. Welcome to leave a message and discuss together

5 May 2020, 09:41 | Views: 8963

Add new comment

For adding a comment, please log in
or create account

0 comments