action代码package action.loginandregister;
import java.util.*;
import com.opensymphony.xwork2.ActionContext;
import action.base.BaseAction;
import exception.CommonException;
public class Login extends BaseAction{
private String username;
private String password;
private String usertype;
private String vercode;

@Override
public String execute() throws Exception{
Map session = ActionContext.getContext().getSession();
String ver2 = (String )session.get("rand");
//清空用户Session的随机验证码字符串。
session.put("rand" , null);
if(vercode.equals(ver2)){
if(usertype.equals("student")){
Integer userId=studentUser.validLogin(username,password);
if(userId<0){
 addActionError("用户名/密码不匹配");
             return "failure";
}else{
 session.put("userId" , userId);
 session.put("username", username);
 session.put("usertype", usertype);
             return "student";
}
}else if(usertype.equals("social")){
Integer userId=socialUser.validLogin(username,password);
if(userId<0){
addActionError("用户名/密码不匹配");
            return "failure";
}else{
 session.put("userId" , userId);
 session.put("username", username);
 session.put("usertype", usertype);
             return "social";
}
}else if(usertype.equals("enterprise")){
Integer userId=enterpriseUser.validLogin(username,password);
if(userId<0){
addActionError("用户名/密码不匹配");
            return "failure";
}else{
 session.put("userId" , userId);
 session.put("username", username);
 session.put("usertype", usertype);
             return "enterprise";
}
}
}else{
addActionError("验证码不匹配,请重新输入");
return "failure";
}
return "failure";
}

public void setUsername(String username){
this.username=username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(String password){
return this.password;
}
public void setUsertype(String usertype){
this.usertype=usertype;
}
public String getUsertype(){
return this.usertype;
}
public void setVercode(String vercode){
this.vercode=vercode;
}
public String getVercode(){
return this.vercode;
}
}

解决方案 »

  1.   

    service代码/**
     * 根据用户名,密码验证用户登陆
     * @param username用户名
     * @param pass密码
     * @return 返回用户id号
     */
    public Integer validLogin(String username,String pass)throws CommonException{
    try{
    Fstudentuser user=fuserDao.findUserByNameAndPass(username, pass);
    if(user==null){
    return null;
    }
    return user.getId();
    }catch(Exception e){
    log.debug(e.getMessage());
    throw new CommonException("对不起,获取用户出错,请重试!");
    }
    }
    dao代码
    [code=Java]
    /**
     * 根据用户名和密码查找用户
     * @param username使用户的用户名
     * @param pass是用户的密码
     * @return 对应用户
     */
    public Fstudentuser findUserByNameAndPass(String username,String pass){
    String agrs[]={username,pass};
    List ul=getHibernateTemplate().find("from Fstudentuser fs where fs.username=? and fs.password=?",agrs);
    if(ul.size()==1){
    return (Fstudentuser)ul.get(0);
    }
    return null;
    }
    /code]
      

  2.   

    model代码/**
     * 根据用户名和密码查找用户
     * @param username使用户的用户名
     * @param pass是用户的密码
     * @return 对应用户
     */
    public Fstudentuser findUserByNameAndPass(String username,String pass){
    String agrs[]={username,pass};
    List ul=getHibernateTemplate().find("from Fstudentuser fs where fs.username=? and fs.password=?",agrs);
    if(ul.size()==1){
    return (Fstudentuser)ul.get(0);
    }
    return null;
    }
      

  3.   

    model代码  package model;
    import java.io.Serializable;
    import java.util.Date;
    import java.util.Set;
    import java.util.HashSet;
    public class Fstudentuser implements Serializable{
    private Integer id;
    private Integer freeze;//冻结标志0为部冻结1为冻结
    private Date freezedate;//冻结日期
    private String image;
    private String username;
    private String password;
    private String email;
    private Fortes fortes;
    private FortesType sfortes;
    private StudentType studenttype;
    private Provance provance;
    private City city;
    private Town town;
    private School school;
    private BigQualify bigqualify;//专业粗略分类
    private Qualify qualify;
    private Date enrolldate;
    private String realname;
    private Date dateofbirth;
    private String sex;
    private Nation nation;
    private String marrige;
    private String chance;
    private String netname;
    private String personnet;
    private NetType nettype;
    private Integer groom;
    private Date overdue;//推荐过期
    private Set<Tstudentuser> threefile=new HashSet<Tstudentuser>();
    //private Set<Confidant> confidant=new HashSet<Confidant>();
    private Date regdate;
    private Integer count;
    public void setId(Integer id){
    this.id=id;
    }
    public Integer getId(){
    return this.id;
    }
    public void setFreeze(int freeze){
    this.freeze=freeze;
    }
    public int getFreeze(){
    return this.freeze;
    }
    public void setFreezedate(Date freezedate){
    this.freezedate=freezedate;
    }
    public Date getFreezedate(){
    return this.freezedate;
    }
    public void setImage(String image){
    this.image=image;
    }
    public String getImage(){
    return this.image;
    }
    public void setUsername(String username){
    this.username=username;
    }
    public String getUsername(){
    return this.username;
    }
    public void setPassword(String password){
    this.password=password;
    }
    public String getPassword(){
    return this.password;
    }
    public void setEmail(String email){
    this.email=email;
    }
    public String getEmail(){
    return this.email;
    }
    public void setFortes(Fortes fortes){
    this.fortes=fortes;
    }
    public Fortes getFortes(){
    return this.fortes;

    public void setSfortes(FortesType sfortes){
    this.sfortes=sfortes;
    }
    public FortesType getSfortes(){
    return this.sfortes;
    }
    public void setStudenttype(StudentType studenttype){
    this.studenttype=studenttype;
    }
    public StudentType getStudenttype(){
    return this.studenttype;
    }
    public void setProvance(Provance provance){
    this.provance=provance;
    }
    public Provance getProvance(){
    return this.provance;
    }
    public void setCity(City city){
    this.city=city;
    }
    public City getCity(){
    return this.city;
    }
    public void setTown(Town town){
    this.town=town;
    }
    public Town getTown(){
    return this.town;
    }
    public void setSchool(School school){
    this.school=school;
    }
    public School getSchool(){
    return this.school;
    }
    public void setBigqualify(BigQualify bigqualify){
    this.bigqualify=bigqualify;
    }
    public BigQualify getBigqualify(){
    return this.bigqualify;
    }
    public void setQualify(Qualify qualify){
    this.qualify=qualify;
    }
    public Qualify getQualify(){
    return this.qualify;
    }
    public void setEnrolldate(Date enrolldate){
    this.enrolldate=enrolldate;
    }
    public Date getEnrolldate(){
    return this.enrolldate;
    }
    public void setRealname(String realname){
    this.realname=realname;
    }
    public String getRealname(){
    return this.realname;
    }
    public void setDateofbirth(Date dateofbirth){
    this.dateofbirth=dateofbirth;
    }
    public Date getDateofbirth(){
    return this.dateofbirth;
    }
    public void setSex(String sex){
    this.sex=sex;
    }
    public String getSex(){
    return this.sex;
    }
    public void setNation(Nation nation){
    this.nation=nation;
    }
    public Nation getNation(){
    return this.nation;
    }
    public void setMarrige(String marrige){
    this.marrige=marrige;
    }
    public String getMarrige(){
    return this.marrige;
    }
    public void setChance(String chance){
    this.chance=chance;
    }
    public String getChance(){
    return this.chance;
    }
    public void setNetname(String netname){
    this.netname=netname;
    }
    public String getNetname(){
    return this.netname;
    }
    public void setPersonnet(String personnet){
    this.personnet=personnet;
    }
    public String getPersonnet(){
    return this.personnet;
    }
    public void setNettype(NetType nettype){
    this.nettype=nettype;
    }
    public NetType getNettype(){
    return this.nettype;
    }
    public void setGroom(int groom){
    this.groom=groom;
    }
    public int getGroom(){
    return this.groom;
    }
    public void setOverdue(Date overdue){
    this.overdue=overdue;
    }
    public Date getOverdue(){
    return this.overdue;
    }
    public void setthreefile(Set<Tstudentuser> threefile){
    this.threefile=threefile;
    }
    public Set<Tstudentuser> getThreefile(){
    return this.threefile;
    }
    /*
    public void setConfidant(Set<Confidant> confidant){
    this.confidant=confidant;
    }
    public Set<Confidant> getConfidant(){
    return this.confidant;
    }
    */
    public void setRegdate(Date regdate){
    this.regdate=regdate;
    }
    public Date getRegdate(){
    return this.regdate;
    }
    public void setCount(int count){
    this.count=count;
    }
    public int getCount(){
    return this.count;
    }
    public int hashCode(){
    return username.hashCode()+email.hashCode(); 
    }
    public boolean equals(Object obj){
    if(obj instanceof Fsocialuser){
    Fstudentuser user=new Fstudentuser();
    if(user.getUsername()==username||user.getEmail()==email){
    return true;
    }
    }
    return false;
    }
    }
      

  4.   

    你把你的Login类里下面的变量。初始化数值!!!!

        private String username;
        private String password;
        private String usertype;
        private String vercode;改成:
        private String username=“”;
        private String password=“”;
        private String usertype=“”;
        private String vercode=“”;感觉。这里要改。
    如果有其他问题,请留言。。我们再讨论。
      

  5.   

    if(vercode.equals(ver2)){
    这句话肯定会报空指针异常的,(java.lang.NullPointerException)
    因为你这样private String vercode;就相当于private String vercode = null;
    null.eauals(ver2)不就异常了吗?
    还有你下面的usertype.equals("student")){也是会出现空指针异常的.
    给你一点建议,判断字符串是否相等的时候,把常量写在前面
    "student".equals(usertype)){这样就会避免无谓的空指针异常了.        
      

  6.   

    原因是,在你没输入用户名和密码时,用了equals方法,报空指针异常
    解决方法:if(null!=vercode&&vercode.equals(ver2)){null!=vercode很重要,是不出空指针的最好方法