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

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就是空

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 chatgpt网页版侧边栏和模型切换点击没反应怎么办
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas