Struts2和hibernate框架整合实现简单的注册登陆功能

Struts2和hibernate框架整合实现简单的注册登陆功能

项目结构:
这里写图片描述
LoginAction.java

package action;
import vo.User;
import vo.UserDAO;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
   
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private User user;
    public User getUser() {
        return user;}
    public void setUser(User user) {
        this.user = user;}
    public String execute()throws Exception{
        if((user=new UserDAO().LoginCheck(user.getUsername(), user.getPassword(),user.getRole()))==null){
            return ERROR;
        }
        if(user.getRole()=="1"){
                return "admin";
            }else{
                return SUCCESS;}    
    }
}

RegistAction.java

package action;

import vo.User;
import vo.UserDAO;

import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {
   

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private User user=null;
    private String repassword;
    public String getRepassword() {
        return repassword;
    }
    public void setRepassword(String repassword) {
        this.repassword = repassword;
    }
    //UserDAO userDao=new UserDAO();
    @Override
    public String execute() throws Exception {
        User selectUser=new User();
        if((selectUser=new UserDAO().selectRegist(user.getUsername()))==null){
            User u=new User();
            u.setUsername(user.getUsername());
            u.setPassword(user.getPassword());
            new UserDAO().saveRegist(u);
            return "success";
        }else{
            return "error";
        }

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

LoginAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
    <validators>
        <field name="user.username">
            <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>用户名不能为空</message>
            </field-validator>
        </field>
        <field name="user.password">
        <field-validator type="regex">
            <param name="expression"><![CDATA[(\w{4,8})]]></param>
            <message>密码长度必须在4~8之间</message>
            </field-validator>
        </field>
    </validators>

RegistAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
    <validators>
        <field name="user.username">
            <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>用户名不能为空</message>
            </field-validator>
        </field>
        <field 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值