In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the difference between @SessionAttributes and @ SessionAttributes. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can learn something from the detailed introduction of this article.
Difference between @SessionAttributes and @ SessionAttributes
There are two very similar annotations in Spring MVC: @SessionAttributes and @SessionAttribute.
Let's look at the definition of @SessionAttributes:
@SessionAttributes is used to store model attributes in HTTP Servlet sessions between requests. It is a type-level annotation used to declare session properties used by a particular controller. This typically lists the names or types of model properties that should be transparently stored in the session for subsequent access requests.
For example:
@SessionAttributes("user")
public class LoginController {
@ModelAttribute("user")
public User setUpUserForm() {
return new User();
}
}
We can see that @SessionAttributes is a class annotation that is used to store models in sessions. As in the example above, we define a model named "User" and store it in Session.
Let's look at the definition of @SessionAttribute:
If you need access to pre-existing session attributes that exist globally (e.g., managed outside the controller (e.g., through filters)) and may or may not exist, you can use the @SessionAttribute annotation on method parameters, as shown in the following example:
@Controller
@RequestMapping("/user")
public class UserController {
/*
* Get user from session attribute
*/
@GetMapping("/info")
public String userInfo(@SessionAttribute("user") User user) {
System.out.println("Email: " + user.getEmail());
System.out.println("First Name: " + user.getFname());
return "user";
}
}
@SessionAttribute simply gets the attributes stored in the session. If you want to set (add or delete) session properties, consider injecting org.springframework.web.context.request.WebRequest or javax.servlet.http.HttpSession into the controller method.
The model bound in @SessionAttributes can be obtained in several ways:
Get in view via request.getAttribute or session.getAttribute
Get it in the view returned by the subsequent request via session.getAttribute or from model
Automatically set parameters to the Model type parameters of the processor for which the request is made or parameters annotated with @ModelAttribute.
@SessionAttributes users can call SessionStatus.setComplete to clear, this method only clears the parameters in SessionAttribute, and does not apply the parameters in Session.
@Controller
@SessionAttributes("pet")
public class EditPetForm {
// ...
@PostMapping("/pets/{id}")
public String handle(Pet pet, BindingResult errors, SessionStatus status) {
if (errors.hasErrors) {
// ...
}
status.setComplete();
// ...
}
}
}
To sum up:
@SessionAttributes is to set model to session.
@SessionAttribute is the data set into the session before getting from the session.
What is the difference between @SessionAttributes and @SessionAttribute? Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
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.