一段基于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; } }