views:

30

answers:

0

Hi, I was wondering how can I validate fields in action classes' validate method? Because full-hibernate-plugin requires default package to extend "hibernate-default". So, would param intercept and validate? http://code.google.com/p/full-hibernate-plugin-for-struts2/wiki/2_Hibernate_Validator_integration


public class Services {

    @SessionTarget Session session;
    @TransactionTarget Transaction transaction;

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public Transaction getTransaction() {
        return transaction;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public void saveUser(User user) {
        try {
            session.save(user);
        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace();
        } 
    }
    public boolean checkUserByUserName(String username) {
        try {
            if(session.createQuery("from User u where u.loginId = :username").setParameter("username", username).list().size() == 1){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}

public class BaseAction extends ActionSupport {
    protected Services services = new Services();

    public Services getServices() {
        return services;
    }

    public void setServices(Services services) {
        this.services = services;
    }
}

public class LoginAction extends BaseAction {

    private String username;
    private String password;

    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 execute() {
        if(!super.services.checkUserByUserName(getUsername())) {
            addFieldError("username", getText("user.invalid"));
            return INPUT;
        }
        return SUCCESS;
    }

    public void validate() {
        if(getUsername().length() == 0) {
            addFieldError("username", getText("username.required"));
        } else if(!getUsername().matches("(PP|PT)\\d{5}")) {
            addFieldError("username", getText("username.wrongformat"));
        }
        if(getPassword().length() == 0) {
            addFieldError("password", getText("password.required"));
        }
    }
}

public class UserAction extends BaseAction implements ModelDriven {

    private static final long serialVersionUID = -6659925652584240539L;

    private User user = new User();
    private List userList = new ArrayList();

    @Override
    public User getModel() {
        return user;
    }

    public String add() {
        System.out.println(user);
        super.services.saveUser(user);
        return SUCCESS;
    }

    public String validateUser() {

        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/demo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password"></property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.currenst_session_context_class">thread</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.vaannila.domain.User" />
    </session-factory>
</hibernate-configuration>