如何使用 aop 处理每个客户端的 Wicket 会话

发布于 2024-09-10 03:11:37 字数 7345 浏览 6 评论 0原文

我正在使用 Wicket 1.4.9 + Spring 3 + iBatis。我想在使用事务之前获取会话数据,但我有一个大问题,因为此数据放在 Wicket Web 会话上(此数据在登录页面上选择)。

我正在使用 spring aop 进行切入点,但我无法为每个客户端切入会话。如何解决这个问题。

用户类实体

package com.app.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    private String name;
    private String lastname;
    private String username;
    private String password;
    private String crtUser;
    private Date crtDate;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCrtUser() {
        return crtUser;
    }

    public void setCrtUser(String crtUser) {
        this.crtUser = crtUser;
    }

    public Date getCrtDate() {
        return crtDate;
    }

    public void setCrtDate(Date crtDate) {
        this.crtDate = crtDate;
    }

}

Web 应用程序类

package com.app.conf;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

import com.app.page.UserLoginPage;

public class WebApp extends WebApplication {

    @Override
    protected void init() {
        addComponentInstantiationListener(new SpringComponentInjector(this));
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return UserLoginPage.class;
    }

    @Override
    public Session newSession(Request request, Response response) {
        return new CustomSession(request);
    }

}

自定义会话类

package com.app.conf;

import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;

import com.app.entity.User;

public class CustomSession extends WebSession {

    private User user;

    public CustomSession(Request request) {
        super(request);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

用户登录页面:登录时保持会话

package com.app.page;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.app.conf.CustomSession;
import com.app.entity.User;

public class UserLoginPage extends WebPage {

    private LoginForm form;

    public UserLoginPage() {
        form = new LoginForm("form");
        add(form);
    }

    private class LoginForm extends Form{

        private User user;

        private TextField<String> username;
        private PasswordTextField password;
        private AjaxButton submitButton;

        public LoginForm(String id) {
            super(id);

            user = new User();

            username = new TextField<String>("username",
                    new PropertyModel<String>(this, "user.username"));
            add(username);

            password = new PasswordTextField("password" , 
                    new PropertyModel<String>(this, "user.password"));
            add(password);

            submitButton = new AjaxButton("submitButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
                    getCustomSession().setUser(user);
                    setResponsePage(new UserAddPage());
                }
            };
            add(submitButton);
        }
    }

    public CustomSession getCustomSession(){
        return (CustomSession)getSession();
    }

}

用户添加页面:我想要在服务类方法上切入点并获取会话以添加 创建用户并在用户实体类

package com.app.page;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.app.entity.User;
import com.app.service.IUserService;

public class UserAddPage extends WebPage {

    @SpringBean private IUserService userService;

    private UserAddForm form;

    public UserAddPage() {
        form = new UserAddForm("form");
        add(form);
    }

    private class UserAddForm extends Form {

        private User user;

        private TextField<String> name;
        private TextField<String> lastname;
        private TextField<String> username;
        private PasswordTextField password;
        private AjaxButton submitButton;

        public UserAddForm(String id) {
            super(id);
            user = new User();

            name = new TextField<String>("name",
                    new PropertyModel<String>(this, "user.name"));
            add(name);

            lastname = new TextField<String>("lastname" , 
                    new PropertyModel<String>(this, "user.lastname"));
            add(lastname);

            username = new TextField<String>("username",
                    new PropertyModel<String>(this, "user.username"));
            add(username);

            password = new PasswordTextField("password" , 
                    new PropertyModel<String>(this, "user.password"));
            add(password);

            submitButton = new AjaxButton("submitButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
                    /*
                     * HOW TO POINT CUT THIS METHOD AND GET SESSION BEFORE INSERT
                     */
                    userService.insert(user);
                }
            };
            add(submitButton);
        }

    }

}

用户方面创建日期

package com.app.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.app.entity.User;

@Aspect
public class UserAspect {

    @Pointcut("execution(* com.app.service..*.insert*(..))&&" + "args(user)")
    public void userPointcut(User user){}

    @Around("userPointcut(user)")
    public Object aroundUser(ProceedingJoinPoint pj, User user) throws Throwable{
        /*
         * How to get Create User from Session
         */
        return pj.proceed();
    }

}

I am using Wicket 1.4.9 + Spring 3 + iBatis. I want to get session data before used transactional but I have a big problem because this data put on Wicket web session(this data choose on login page).

I am using spring aop for point cut but I cannot point cut session per client. How to solve this problem.

User Class Entity

package com.app.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    private String name;
    private String lastname;
    private String username;
    private String password;
    private String crtUser;
    private Date crtDate;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCrtUser() {
        return crtUser;
    }

    public void setCrtUser(String crtUser) {
        this.crtUser = crtUser;
    }

    public Date getCrtDate() {
        return crtDate;
    }

    public void setCrtDate(Date crtDate) {
        this.crtDate = crtDate;
    }

}

Web App Class

package com.app.conf;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

import com.app.page.UserLoginPage;

public class WebApp extends WebApplication {

    @Override
    protected void init() {
        addComponentInstantiationListener(new SpringComponentInjector(this));
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return UserLoginPage.class;
    }

    @Override
    public Session newSession(Request request, Response response) {
        return new CustomSession(request);
    }

}

Custom session class

package com.app.conf;

import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;

import com.app.entity.User;

public class CustomSession extends WebSession {

    private User user;

    public CustomSession(Request request) {
        super(request);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

User Login Page : Keep Session when Login

package com.app.page;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.app.conf.CustomSession;
import com.app.entity.User;

public class UserLoginPage extends WebPage {

    private LoginForm form;

    public UserLoginPage() {
        form = new LoginForm("form");
        add(form);
    }

    private class LoginForm extends Form{

        private User user;

        private TextField<String> username;
        private PasswordTextField password;
        private AjaxButton submitButton;

        public LoginForm(String id) {
            super(id);

            user = new User();

            username = new TextField<String>("username",
                    new PropertyModel<String>(this, "user.username"));
            add(username);

            password = new PasswordTextField("password" , 
                    new PropertyModel<String>(this, "user.password"));
            add(password);

            submitButton = new AjaxButton("submitButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
                    getCustomSession().setUser(user);
                    setResponsePage(new UserAddPage());
                }
            };
            add(submitButton);
        }
    }

    public CustomSession getCustomSession(){
        return (CustomSession)getSession();
    }

}

User add page : i want point cut insert on Service Class method and get session for add Create user and Create date on User Entity Class

package com.app.page;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.app.entity.User;
import com.app.service.IUserService;

public class UserAddPage extends WebPage {

    @SpringBean private IUserService userService;

    private UserAddForm form;

    public UserAddPage() {
        form = new UserAddForm("form");
        add(form);
    }

    private class UserAddForm extends Form {

        private User user;

        private TextField<String> name;
        private TextField<String> lastname;
        private TextField<String> username;
        private PasswordTextField password;
        private AjaxButton submitButton;

        public UserAddForm(String id) {
            super(id);
            user = new User();

            name = new TextField<String>("name",
                    new PropertyModel<String>(this, "user.name"));
            add(name);

            lastname = new TextField<String>("lastname" , 
                    new PropertyModel<String>(this, "user.lastname"));
            add(lastname);

            username = new TextField<String>("username",
                    new PropertyModel<String>(this, "user.username"));
            add(username);

            password = new PasswordTextField("password" , 
                    new PropertyModel<String>(this, "user.password"));
            add(password);

            submitButton = new AjaxButton("submitButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
                    /*
                     * HOW TO POINT CUT THIS METHOD AND GET SESSION BEFORE INSERT
                     */
                    userService.insert(user);
                }
            };
            add(submitButton);
        }

    }

}

User Aspect

package com.app.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.app.entity.User;

@Aspect
public class UserAspect {

    @Pointcut("execution(* com.app.service..*.insert*(..))&&" + "args(user)")
    public void userPointcut(User user){}

    @Around("userPointcut(user)")
    public Object aroundUser(ProceedingJoinPoint pj, User user) throws Throwable{
        /*
         * How to get Create User from Session
         */
        return pj.proceed();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-09-17 03:11:37

WebApp.getSession().getUser() ?

我想您想在 spring 上下文中注册一个可以提供授权用户的 bean。这可以从您的 WebApp 会话中检索用户(WebApp 实例也将是一个已注册的 bean,但我认为它已经是)。 spring 文档/社区中应该有大量关于此用例的信息。

为了获得更好的答案,您应该清理您的问题(代码块的使用)并陈述用例。

WebApp.getSession().getUser() ?

I'd imagine that you want to register a bean in the spring context that can supply an authorised user. This can retrieve the user from your WebApp session (the WebApp instance would also be a registered bean, but I assume it is already). There should be plenty of information on this use-case in the spring docs/community.

For better answers, you should clean up your question (use of code blocks) and state the use case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文