如何在JSP中获取动态下拉框

时间:2013-06-27 11:37:45

标签: spring jsp spring-mvc

我想在Spring MVC中填充一个下拉框我试图这样做但我得到空指针异常

这是我的控制者:

public class CatalogueController  {
private CatalogueService catalogueService;
private PublisherService publisherService;
private SubjectService subjectService; 

   // some code and i generated setter and getter methods  
   ............

 @RequestMapping(value="/catalogue/new.action", method=RequestMethod.GET)  
   public ModelAndView newMember() throws Exception {
    ModelAndView mvc= null;
    mvc =   new ModelAndView("catalogue/catalogueForm", "catalogueForm", new CatalogueBase());
    mvc.addObject("copyDetailForm", new CatalogueCopyDetails());

    List<Publisher> publist = publisherService.getPublisherList();
    mvc.addObject("publist", publist);

    List<Subject> subjectlist = subjectService.getSubjectList();
    mvc.addObject("subjectlist", subjectlist);

    return mvc;
}  

这是我的服务方式:

    @Override
public List<Publisher> getPublisherList() {
    List<Publisher> list = publisherDAO.getPublisher();
    return list;
}
    @Override
public List<Subject> getSubjectList() {
    List<Subject> list = subjectDAO.getSubjects();
    return list;
}

这是我的DAO方法:

    @SuppressWarnings("unchecked")
public List<Publisher> getPublisher() {
      Query qry = getSession().createQuery("from Publisher");
        return qry.list();
}
    @SuppressWarnings("unchecked")
public List<Subject> getSubjects() {
      Query qry = getSession().createQuery("from Subject");
        return qry.list();
}

最后这是我的JSP页面:

  <form:form commandname="catalogueForm"    action="${pageContext.request.contextPath}/catalogue/create.action" method="post" modelAttribute="catalogueForm">

    <form:select path="publisher.id" id="publisher.id">
    <form:options items="${publist}" itemValue="id" itemLabel="name" />
    </form:select>                                                        

    <form:select path="subject.id" id="subject.id">                            
    <form:options items="${subjectlist}" itemValue="id" itemLabel="name" />
    </form:select>

   </form:form>

这是Stack Trace:

显示java.lang.NullPointerException     在com.easylib.elibrary.webapp.controller.catalogue.CatalogueController.newMember(CatalogueController.java:80)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)     在org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)     在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)     在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)     在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)

1 个答案:

答案 0 :(得分:0)

你在控制器的第80行得到了npe,但我看不出你问题的哪一行。

我的猜测是您没有正确连接服务,“publisherService”或“subjectService”为空。