In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what is the difference between Model and Session in SpringMVC. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
The difference between Model and Session
What is Session:Session: in computers, especially in network applications, it is called a "session". It specifically refers to the time interval between an end user and the interactive system, which usually refers to the time between registering into the system and logging out of the system.
In the use of the website, in order to save user information. The server creates a Session for each user (browser).
Process flow
1. Client request server
2. The server creates a Session to save user data on the server side.
3. The server returns a SessionId (JSESESSION) to the client, which is a Cookie.
4. If the user accesses the server again within the time of Session, the user's Session will be taken out according to SessionId.
Model:Model is an interface
ModelMap is the implementation of the interface. Populate the data from model into the request field and return it to the client
Make a brief summary
The session data is saved on the server, and the model data is placed in the view. Session can be used on different pages. Model can only be used on pages returned by Controller
Session and model problems cause login to fail to exit normally
Use ssm+maven+jsp+eclipse to do a login registration function, there is no problem, but I also made a "do not log in" button, just. When you click exit, you find that there is no exit. Here is mainly about the login problem. Don't push. Look at the code.
Here are the login and logout features in controller.
1. Notice that I used an @ SessionAttributes on line 2. I used a model.addAttribute ("user", user) in the authentication login method.
2. Notice that I use session.invalidate (); and session.removeAttribute ("user") in my login method; of course, neither of these methods can make me quit. This is the problem, and there is an analysis below. First, the solution: use sessionStatus.setComplete (); exit successfully.
@ Controller@SessionAttributes ("user") public class LoginController {@ Autowired private LoginService loginService; / / the path to the form submission @ RequestMapping ("/ checkLogin") / / @ ModelAttribute public String checkLogin (@ ModelAttribute ("abc") User user,Model model) {/ / call the service method user = loginService.checkLogin (user.getUsername (), user.getPassword ()) / / add user to model and jump to the home page if (user! = null) {model.addAttribute ("user", user); return "redirect:/user/index.do";} return "login" } / / logout method @ RequestMapping ("/ outLogin") public String outLogin (HttpSession session,SessionStatus sessionStatus) {/ / use the session.invalidata () method to log out the current session sessionStatus.setComplete (); / / session.invalidate (); / / session.removeAttribute ("user"); return "redirect:/checkLogin.do";}}
Here is part of the jsp page code. Take a look at the third line: how the page gets the session value. If you can't get it, it means you quit successfully.
${user.username} personal information exit login registration
The code posted is the code that solves the problem and can exit normally.
The following shows some service layer code for fun.
@ Servicepublic class LoginServiceImpl implements LoginService {@ Autowired private UserMapper userMapper; @ Override public User checkLogin (String username, String password) {User user = userMapper.findByUsername (username); if (user! = null & & user.getPassword (). Equals (password)) {return user;} return null;}} Analysis
Ask some good brother and he asks me to find out about the model object.
The information is sorted out as follows:
1. Model is equal to the setAttribute of ordinary request, model layer storage, view layer fetch. But request is just one request and it's over.
2. There are two kinds of session, one is to pass httpsession directly, and the other is that I use @ sessionAttribute annotation. With this annotation, he will automatically save the corresponding attribute name in model to session. This is after loading @ controller and before rendering view. Then clean it up with httpsession's remove.
3. My solution this time is to clean it up with sessionStatus.setComplete ().
So much for sharing what is the difference between Model and Session in SpringMVC. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.