问题描述
- 在springmvc中做一个根据id删除一条数据的功能,出现500问题,代码如下,请大神指教
-
Model层代码
package com.pengyou.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "sg_registry")
public class registryEntity {private int id; private String name; private String phonenum; private int intArea; //意向面积 private String recHouse; private String logTime; //录入时间 private String salesMan; private String workArea; private int visitsCount; private String address; private String photoUrl; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id",nullable = false,length = 9) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "name",nullable = false,length = 25) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "phonenum",nullable = false,length = 11) public String getPhonenum() { return phonenum; } public void setPhonenum(String phonenum) { this.phonenum = phonenum; } @Column(name = "intArea",nullable = false,length = 9) public int getIntArea() { return intArea; } public void setIntArea(int intArea) { this.intArea = intArea; } @Column(name = "recHouse",nullable = false,length = 255) public String getRecHouse() { return recHouse; } public void setRecHouse(String recHouse) { this.recHouse = recHouse; } @Column(name = "logTime",nullable = false) public String getLogTime() { return logTime; } public void setLogTime(String logTime) { this.logTime = logTime; } @Column(name = "salesMan",nullable = false,length = 255) public String getSalesMan() { return salesMan; } public void setSalesMan(String salesMan) { this.salesMan = salesMan; } @Column(name = "workArea",nullable = false,length = 255) public String getWorkArea() { return workArea; } public void setWorkArea(String workArea) { this.workArea = workArea; } @Column(name = "visitsCount",nullable = false,length = 9) public int getVisitsCount() { return visitsCount; } public void setVisitsCount(int visitsCount) { this.visitsCount = visitsCount; } @Column(name = "address",nullable = true,length = 255) public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Column(name = "photoUrl",nullable = false,length = 255) public String getPhotoUrl() { return photoUrl; } public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; }
}
DAO实现类代码
package com.pengyou.dao.impl;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.pengyou.dao.IdelDao;
import com.pengyou.model.registryEntity;@Repository("deldao")
public class delDaoImpl implements IdelDao{@Resource protected SessionFactory sessionFactory; public Session getSession(){ return sessionFactory.getCurrentSession(); } @Override public void delreg(int id) { Session session = this.getSession(); //registryEntity regens = (registryEntity) session.get(registryEntity.class, id); registryEntity regens = new registryEntity(); regens.setId(id); session.delete(regens); }
}
sevice实现类代码
package com.pengyou.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import com.pengyou.dao.IdelDao;
import com.pengyou.service.IdelService;
@Transactional
@Service("delservice")
public class delServiceImpl implements IdelService{
private IdelDao deldao;
@Override
public void delreg(int id) {
// TODO Auto-generated method stub
System.out.println("delServiceImpl---1------>"+id);
deldao.delreg(id);System.out.println("delServiceImpl--------->"+id); }
/*
@Override public List allReg() { // TODO Auto-generated method stub return deldao.allReg(); } */
}
Controller层代码
package com.pengyou.controller;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import com.pengyou.service.IdelService;
@Controller
@RequestMapping("/del/")
public class delController extends BaseController{@Resource private IdelService idelservice; @ResponseBody @RequestMapping("delReg") public String toadd(HttpServletRequest request){ System.out.println("id++++++"+request.getParameter("id")); idelservice.delreg(Integer.parseInt(request.getParameter("id"))); /*System.out.println("delController------->");*/ return "————————————————————"; }
}
页面代码
style="width: 35px;height: 35px" src="images/${r.photoUrl }" />
${r.name}
${r.phonenum }
${r.intArea }${r.recHouse}
${r.logTime }
${r.salesMan }${r.workArea}
${r.visitsCount }
${r.address }编辑
删除
备注/c:forEach
运行时会报500错误,其余的地方都应该没错,应该是dao实现类中的方法错了 id++++++1
delServiceImpl---1------>1
四月 18, 2016 12:18:51 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [sao] in context with path [/house] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.pengyou.service.impl.delServiceImpl.delreg(delServiceImpl.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy18.delreg(Unknown Source)
解决方案
500是服务器后台处理错误,异常信息显示 delServiceImpl.java这个类有空指针异常,你定位到对应的行单步调试下吧。
解决方案二:
估计是你的持久层session根本就没有获取到