java – 如何在Spring MVC中填充下拉列表框

前端之家收集整理的这篇文章主要介绍了java – 如何在Spring MVC中填充下拉列表框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在试图找出如何在 Spring MVC中填充一个下拉框.有一些关于这个问题的线程,但我没有找到帮助我,所以我希望有人可以帮助我.

这是我的控制器:

@Controller
@RequestMapping("/document-revision") 
public class DocumentRevisionController {


@Autowired
private DocumentRevisionService documentRevisionService;
private DocumentService documentService;

@RequestMapping(value="/list",method=RequestMethod.GET) 
public String getDocumentRevisionList(Model model) {
    List<DocumentRevision> documentRevisions = documentRevisionService.retrieveAllDocumentRevisions();
    model.addAttribute("documentRevisions",documentRevisions);

    return "document-revision";
}

@RequestMapping(value="/add",method=RequestMethod.GET)
public String getDocumentRevision(Model model) {
    DocumentRevision documentRevision = new DocumentRevision();
    model.addAttribute("documentRevisionAttribute",documentRevision);
    return "new-documnent-revision";
}


@RequestMapping(value="/add",method=RequestMethod.POST)
public String postDocumentRevision(@modelattribute("documentRevisionAttribute") @Valid DocumentRevision documentRevision,BindingResult result) {

    if(result.hasErrors()){
        return "new-document-revision";
    }

    documentRevisionService.createDocumentRevision(documentRevision);
    return "redirect:/testapp/document-revision/list";  
}

}

这里是jsp页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
        <style>
        .error { color: red; }
        </style>
</head>
<body>

    <h1>Create New Document Revision</h1>

    <c:url var="saveUrl" value="/testapp/document-revision/add" />
    <form:form modelAttribute="documentRevisionAttribute" method="POST" action="${saveUrl}">
        <table>
            <tr>
                <td>DocumentNumber</td>
                <td><form:select path="document_number">
                    <form:option value="NONE" label="--- Select ---" />
                    <form:options items="${documentNumberList}" />
                    </form:select>
                </td>
                <td><form:errors path="document_number" cssClass="error" /></td>
            </tr>


            <tr>
                <td><form:label path="documentRState">Document R-State</form:label></td>
                <td><form:input path="documentRState"/></td>
                <td><form:errors path="documentRState" cssClass="error"/></td>
            </tr>

        </table>

        <input type="submit" value="Save" />
    </form:form>

</body>
</html>

我已经尝试添加一个@modelattribute方法来检索文档编号,

@modelattribute
    public List<Document> documentNumberList(){
        return documentService.retrieveAllDocumentNumbers();
    }

但它给了我错误.有没有人知道应该怎么做?

感谢您的时间

/ D

编辑我以为我会澄清,我的愿望是为documentService检索的文档编号有一个下拉框.

编辑2这是根据请求的错误日志:

java.lang.NullPointerException
testapp.controller.DocumentRevisionController.documentNumberList(DocumentRevisionController.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.method.annotation.ModelFactory.invokeModelAttributeMethods(ModelFactory.java:123)
org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:97)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:614)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

解决方案我以为我会添加完整的控制器代码,以防其他人可以从中受益:

@Controller
@RequestMapping("/document-revision") 
public class DocumentRevisionController {


@Autowired
private DocumentRevisionService documentRevisionService;

@Autowired
    private DocumentService documentService;

@RequestMapping(value="/list",documentRevision);
    model.addAttribute("documentNumberList",documentService.retrieveAllDocumentNumbers());

    return "new-documnent-revision";
}


@RequestMapping(value="/add",BindingResult result) {

    if(result.hasErrors()){
        return "new-document-revision";
    }

    documentRevisionService.createDocumentRevision(documentRevision);
    return "redirect:/testapp/document-revision/list";  
}

}

解决方法

不知道调用什么Controller方法显示与documentNumberList的视图,但是您需要将该集合添加到传递给此视图的模型中:
model.addAttribute("documentNumberList",documentService.retrieveAllDocumentNumbers());

虽然从你的异常堆栈跟踪你也错​​过了@Autowired在documentService字段.

原文链接:https://www.f2er.com/java/123354.html

猜你在找的Java相关文章