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