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

时间:2010-07-19 08:14:51

标签: java aop wicket spring-aop

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

我正在使用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 App类

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();
    }

}

1 个答案:

答案 0 :(得分:0)

WebApp.getSession()。getUser()?

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

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