如何填充在Spring MVC的下拉框中(How to populate dropdown box

2019-06-24 06:12发布

我一直在试图找出如何填充在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", 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);
    model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers());

    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";  
}

}

Answer 1:

不知道控制器方法被调用,以显示你的看法documentNumberList ,但你需要的是集合添加到传递给这个视图的模型:

model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers());

尽管从您的异常堆栈跟踪你也错过了一个@Autowired上documentService场。



Answer 2:

@ModelAttribute("numberList")
public List<Document> documentNumberList(){
    List<LabelValue> selectItems = new ArrayList<LabelValue>();
    List<Document> docList = documentService.retrieveAllDocumentNumbers();
    for (Document doc : docList) {
     selectItems.add(new LabelValue(doc.id,doc.value));
}
    return selectItems;
}

FYI LabelValue类是我们用来进行下拉标签和值项的简单DTO。 这将有一个标签和值属性,以及相应的getter / setter方法。

LabelValue.java

 private String lable;
 private String value;

 //getters/setters

---- JSP -----

<tr>
    <td>DocumentNumber</td>
    <td><form:select id="docNo" path="document_number">
           <form:option value="NONE" label="--- Select ---" />
           <form:options items="${numberList}" itemValue="value" itemLabel="lable"/>
         </form:select>
    </td>
    <td><form:errors path="document_number" cssClass="error" /></td>
</tr>

希望这可以帮助..



Answer 3:

我今天由我自己解决这类问题。 这是非常简单和容易理解。 在Spring MVC 3.0控制器只是把这个代码 -

 @ModelAttribute("creditCardTypes")
 public Map<String,String> populateCreditCardTypes() {
        Map<String,String> creditCardTypes = new LinkedHashMap<String,String>();
        creditCardTypes.put("VS", "Visa");creditCardTypes.put("MC", "MasterCard");
        creditCardTypes.put("AE", "American Express");
        creditCardTypes.put("DS", "Discover");creditCardTypes.put("DC", "Diner's Club");                
        return creditCardTypes;
    }

现在,“creditCardTypes”属性会在页面加载或提交页面范围缴费,意味着它将可无论requestmapping网址会。

在JSP中,放在这里面的代码 - 信用卡类型:

<form:select path="creditCardType">
    <option value="Select" label="Select a card type"></option>
    <form:options items="${creditCardTypes}" />
</form:select> 

这里,路径=“CREDITCARDTYPE”是指在Spring MVC模式/命令对象属性,项=“$ {} creditCardTypes”是指所有人口的信用卡类型将在“creditCardTypes”的ModelAttribute可用。 而已 !!!



文章来源: How to populate dropdown box in Spring MVC