0

我正在使用 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;
    }

}

网络应用类

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

}
4

1 回答 1

0

WebApp.getSession().getUser() ?

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

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

于 2010-07-28T20:31:29.940 回答