java spring框架与hibernate提交表单错误

时间:2014-12-22 09:37:45

标签: java spring hibernate spring-mvc

我遇到了这个问题,我不知道如何解决它基本上是一种形式...我还有另一种形式,即注册,它的工作完全正常..另一种形式有一个空这里的指针异常是

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
appdev.ecomplain.controller.ComplainController.saveComplains(ComplainController.java:74)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我有这个控制器

@Controller
@RequestMapping("/ecomplain")
public class ComplainController 
{
  @Autowired
  private UserService userService;
  private ComplainService complainService;

@RequestMapping(value = "/Homepage")
public String listUsers(Map<String, Object> map) 
{
    map.put("users", new Users());
    return "/ecomplain/Homepage";
}

@RequestMapping(value = "/ComplaintForm")
public String showComplainForm(Map<String, Object> map) 
{
    map.put("users", new Users());
    map.put("complains", new Complains());
    return "/ecomplain/ComplaintForm";
}

@RequestMapping(value = "/About")
public String showAbout(Map<String, Object> map) 
{
    map.put("users", new Users());
    return "/ecomplain/About";
}

@RequestMapping("/get/{userId}")
public String getUsers(@PathVariable Long userId, Map<String, Object> map) 
{
    Users users = userService.getUsers(userId);
    map.put("users", users);

    return "/ecomplain/CreateUserForm";
}

@RequestMapping("/get/{complainId}")
public String getComplains(@PathVariable Long complainId, Map<String, Object> map) 
{
    Complains complains = complainService.getComplains(complainId);
    map.put("complains", complains);

    return "/ecomplain/ComplaintForm";
}

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveUsers(@ModelAttribute("users") Users users, BindingResult result) 
{
    userService.saveUsers(users);
    return "redirect:Homepage";
}

@RequestMapping(value = "/savecomplain", method = RequestMethod.POST)
public String saveComplains(@ModelAttribute("complains") Complains complains, BindingResult result) 
{
    complainService.saveComplains(complains);
    return "redirect:ComplaintForm";
}

@RequestMapping("/delete/{userId}")
public String deleteUsers(@PathVariable("userId") Long userId) 
{
    userService.deleteUsers(userId);
    return "redirect:/ecomplain/Homepage";
}

@RequestMapping("/delete/{complainId}")
public String deleteComplains(@PathVariable("complainId") Long complainId) 
{
    complainService.deleteComplains(complainId);
    return "redirect:/ecomplain/Homepage";
}

这是我的班级

@Entity
@Table(name = "complains")
public class Complains 
{
  private Long complainId;
  private String subject;
  private String category;
  private String placeOfIncident;
  private String complainDetail;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getComplainId() 
{
       return complainId;
}

@Column(nullable = false)
public String getSubject() 
{
       return subject;
}

@Column(nullable = false)
public String getCategory() 
{
       return category;
}

@Column(nullable = false)
public String getPlaceOfIncident() 
{
       return placeOfIncident;
}

@Column(nullable = false)
public String getComplainDetail() 
{
       return complainDetail;
}

public void setComplainId(Long complainId) 
{
    this.complainId = complainId;
}

public void setSubject(String subject) 
{
    this.subject = subject;
}

public void setCategory(String category) 
{
    this.category = category;
}

public void setPlaceOfIncident(String placeOfIncident)
{
    this.placeOfIncident = placeOfIncident;
}

public void setComplainDetail(String complainDetail)
{
    this.complainDetail = complainDetail;
}

这是我的DAO及其实现

public interface ComplainsDao 
{
/*
 * CREATE and UPDATE
 */
public void saveComplains(Complains complains);

/*
 * READ
 */
public List<Complains> listComplains();
public Complains getComplains(Long complainId);

/*
 * DELETE
 */
public void deleteComplains(Long complainId);
}

实施

@Repository
public class ComplainsDaoImpl implements ComplainsDao 
{
@Autowired
private SessionFactory sessionFactory;

public void saveComplains(Complains complains) 
{
    getSession().merge(complains);
}

public List<Complains> listComplains() 
{
    return getSession().createCriteria(Complains.class).list();
}

public Complains getComplains(Long complainId) 
{
    return (Complains) getSession().get(Complains.class, complainId);
}

public void deleteComplains(Long complainId) 
{
    Complains complains = getComplains(complainId);

    if(null != complains) 
    {
        getSession().delete(complains);
    }
}

private Session getSession() 
{
    Session sess = getSessionFactory().getCurrentSession();

    if(sess == null) 
    {
        sess = getSessionFactory().openSession();
    }

    return sess;
}

private SessionFactory getSessionFactory() 
{
    return sessionFactory;
}
}

服务及其实施

public interface ComplainService 
{
/*
 * CREATE and UPDATE
 */
public void saveComplains(Complains complains);

/*
 * READ
 */
public List<Complains> listComplains();
public Complains getComplains(Long complainId);

/*
 * DELETE
 */
public void deleteComplains(Long complainId);
}

实施

@Service
public class ComplainsServiceImpl implements ComplainService 
{
@Autowired
private ComplainsDao complainsDao;

@Transactional
public void saveComplains(Complains complains) 
{
    complainsDao.saveComplains(complains);
}

@Transactional(readOnly = true)
public List<Complains> listComplains() 
{
    return complainsDao.listComplains();
}

@Transactional(readOnly = true)
public Complains getComplains(Long complainId) 
{
    return complainsDao.getComplains(complainId);
}

@Transactional
public void deleteComplains(Long complainId) 
{
    complainsDao.deleteComplains(complainId);
}
}

老实说,这是我第一次使用spring框架和hibernate,我很难理解它,希望有人可以提供帮助

0 个答案:

没有答案