Maintain the user status-Use of session bean in Spring and user status bean

Source: Internet
Author: User

Maintain the user status-Use of session bean in Spring and user status bean

We all know that in web development, once a user logs on to the system, the system must maintain the user's status before logging off, in this way, he can see his content (such as the personal homepage and message ).

So how to maintain the user status? Spring provides a bean scope mechanism to help us easily manage user statuses.

Here we mainly use the session bean. It can be seen from the name that its scope is bound to the session. That is to say, each session corresponds to a session bean, and the session bean does not affect each other.

For example, the user statuses we want to maintain here include: User Name and employee ID. To facilitate management, we create a class UserPreferences

public class UserPreferences implements Serializable{/** *  */private static final long serialVersionUID = 1L;private String empno;private String name;public String getEmpno() {return empno;}public void setEmpno(String empno) {this.empno = empno;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

Now we want to bind UserPreferences and session. According to the bean scope mechanism, we need to set the scope of UserPreferences to session:

<bean id="userPreferences" class="com.test.dto.UserPreferences" scope="session"><aop:scoped-proxy /></bean>

In this case, a proxy object is generated, because when session bean, request bean, or global session bean is injected into singleton, singleton bean is initialized only once, its Dependencies are only injected once. If <aop: scoped-proxy/> is not added, all operations are performed on the same session bean, that is, the earliest injected one (but in use, I found that an error message is prompted If <aop: scoped-proxy/> is not added ). After adding <aop: scoped-proxy/>, userPreferences injected into singleton is actually a proxy object, which has the same public method as userPreferences. When the proxy object method is called, it searches for the real userPreferences object from the Http session, and then calls its corresponding method. In this way, we can use session bean in singleton (such as Controller.

Below is a simple login instance: First, write a login page:

<Html> 

Then there is the background Method for login:

@RequestMapping(value = "/login", method = RequestMethod.POST)public ModelAndView login(Employee employee) {preferences.setEmpno(employee.getEmpno());preferences.setName(employee.getName());ModelAndView mv = new ModelAndView("kft/success.htm");return mv;}

The success page is used to display the login user name and employee ID:

Log out with the logout button.


This mechanism of Spring provides us with convenience. In essence, we still use HttpSession to maintain the user's status.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.