sunlarry
2012-12-05 08:03
浏览 221
已采纳

Struts2+Spring+JPA+FREEMARKER 登录程序错误

一段基于Struts2+Spring+JPA+FREEMARKER登录程序,总是跳出java.lang.NullPointerException错误!

 

数据结构如下:

 

create table SYSUSER
(
  loginid      VARCHAR2(20) not null,
  pwd          VARCHAR2(20),
 )

 STRUTS2配置如下:

 

<action name="userlogin" class="LoginAction">
            <result name="loginsuccess" type="freemarker">/WEB-INF/templates/user.ftl</result>        
            <result name="errorlogin" type="freemarker">/WEB-INF/templates/admin/login.ftl</result>
                </action>

 ACTION如下:

 

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.office.model.Sysuser;
import com.office.service.SysuserService;
import com.office.util.ChanageLetter;
import com.office.util.MD5;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
  private String loginid;
  private String pwd;
  private String checkcode;
  private Sysuser sysuser;
  private ArrayList<Sysuser> sysusers;
  
public ArrayList<Sysuser> getSysusers() {
    return sysusers;
}
public void setSysusers(ArrayList<Sysuser> sysusers) {
    this.sysusers = sysusers;
}
private SysuserService sysuserService;
public SysuserService getSysuserService() {
    return sysuserService;
}
public void setSysuserService(SysuserService sysuserService) {
    this.sysuserService = sysuserService;
}
 
  public String execute() throws Exception{
 
      ActionContext actionContext = ActionContext.getContext();
      Map<?, ?> session = actionContext.getSession();                
      String code = ChanageLetter.chg(session.get("rand").toString());
          MD5 md5 = new MD5();  
        String pwd=md5.getMD5ofStr(this.getPwd());
        String um=this.getLoginid();
        this.setSysusers(this.sysuserService.find(um, pwd));
        System.out.println(this.sysusers);
        System.out.println(this.sysusers.size());
         
        if(0==this.sysusers.size()){
            return "errorlogin";
        }
        else{
         
            return "loginsuccess";
        }
         
        
  }
   
    public String getLoginid() {
        return loginid;
    }
    public void setLoginid(String loginid) {
        this.loginid = loginid;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
 
    public String getCheckcode() {
        return checkcode;
    }
 
     
    public void setCheckcode(String checkcode) {
        this.checkcode = checkcode;
    } 
}

 MODEL如下:

 

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name = "SYSUSER", schema = "TEST")
public class Sysuser implements java.io.Serializable {
 
    private String loginid; 
    private String pwd;        
 
    @Id
    @Column(name = "LOGINID", unique = true, nullable = false, length = 20)
    public String getLoginid() {
        return this.loginid;
    }
    public void setLoginid(String loginid) {
        this.loginid = loginid;
    }
    @Column(name = "PWD", length = 20)
    public String getPwd() {
        return this.pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

 SERVICE如下:

 

import java.util.ArrayList;
import java.util.List;
public interface SysuserService {
    public void save(Sysuser sysuser);
 
    public void delete(Sysuser sysuser);
 
    public void update(Sysuser sysuser);    
         
    public ArrayList<Sysuser> find(final String username, final String password);
     
 
}
 
 
 
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
 
@Transactional
public class SysuserServiceImpl implements SysuserService {    
    private EntityManager entityManager;
    public void delete(Sysuser sysuser) {
        sysuser = entityManager.getReference(Sysuser.class, sysuser.getLoginid());
        entityManager.remove(sysuser);
    }    
    public void save(Sysuser sysuser) {
        entityManager.persist(sysuser);
    }
    public void update(Sysuser sysuser) {
        entityManager.merge(sysuser);
    }
 
    public ArrayList<Sysuser> find(String username, String password) {
        String jpql="select u from Sysuser u where u.loginid like :username and u.pwd like :password";
        Query query=getEntityManager().createQuery(jpql);
        query.setParameter("username", username);
        query.setParameter("password", password);
        List<Sysuser> list=query.getResultList();        
        return (ArrayList<Sysuser>) list;
    }    
    public EntityManager getEntityManager() {
        return entityManager;
    }    
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • 已采纳

    空指针是因为没有实现注入,你使用的JPA首先应该在loginAction上加入注解@Controller
    和@Scope(“prototype”),并且action的service童养需要加入注解@Resource(name = "SysuserService "),同时service的实现类上也需要加入注解@Service("SysuserService ")还有事务,你的代码上好像都没有加,所以spring是不会注入的,因此LoginAction中SysuserServiceImpl就是空

    点赞 评论
  • iteye_7115 2012-12-05 10:53

    this.setSysusers(this.sysuserService.find(um, pwd));

    先查下um pwd传的是啥值,对应后台数据库能否查到记录

    点赞 评论
  • chawei784533 2012-12-05 10:59

    查看spring中是否在action中注入了sysuserService

    点赞 评论
  • 不美不萌不倾城 2012-12-05 11:23

    service没有注入,看你使用的注解, SysuserServiceImpl上面加上@Service试试

    点赞 评论
  • jinnianshilongnian 2012-12-05 12:21

    1、你的action定义时没有写包

    2、 因此由struts2直接new的 所以没有享受到spring管理,即没有注入sysuserService

    3、改 class="loginAction” 然后在spring中定义

    点赞 评论
  • goodluck_wgw 2012-12-05 13:42

    private ArrayList sysusers = new ArrayList();ok,应该是该变量为空

    点赞 评论
  • changyibiao126 2012-12-05 16:44

    [list]
    [*]
    [/list]

    点赞 评论

相关推荐 更多相似问题