2012-05-08 25 views
10

Tôi đã cố gắng tìm hiểu cách điền một hộp thả xuống trong Spring MVC. Có một vài chủ đề trên đó về chủ đề này nhưng không ai trong số họ mà tôi đã tìm thấy đã giúp tôi vì vậy tôi hy vọng một ai đó ở đây có thể giúp tôi.Cách điền vào hộp thả xuống trong Spring MVC

Đây là bộ điều khiển của tôi:

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

} 

và đây là trang 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> 

Tôi đã thử thêm một phương pháp @ModelAttribute mà lấy số tài liệu,

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

nhưng nó đã cho tôi lỗi. Có ai biết làm thế nào nó nên được thực hiện?

Cảm ơn bạn đã dành thời gian

/D

Sửa tôi nghĩ rằng tôi muốn làm rõ rằng mong muốn của tôi là phải có một hộp thả xuống cho số tài liệu được lấy ra bởi documentService.

Chỉnh sửa 2 Dưới đây là các bản ghi lỗi theo yêu cầu:

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) 

Giải pháp Tôi nghĩ tôi sẽ thêm mã điều khiển hoàn chỉnh các công trình trong trường hợp có những người khác có thể hưởng lợi từ nó:

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

} 
+0

Bạn có thể dán nhật ký lỗi không? –

Trả lời

8

Không chắc chắn phương thức Bộ điều khiển nào được gọi để hiển thị chế độ xem của bạn với documentNumberList, nhưng bạn cần phải thêm bộ sưu tập đó vào mô hình được truyền đến thứ là chế độ xem:

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

Mặc dù theo dõi ngăn xếp ngoại lệ của bạn, bạn cũng bỏ lỡ trường @Autowired on documentService.

+0

Cảm ơn bạn đã hoạt động! – dlinx90

8
@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 là một DTO đơn giản mà chúng tôi sử dụng để mang nhãn rơi xuống và các mục giá trị. Nó sẽ có một thuộc tính label và value, và các getters/setters tương ứng.

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> 

hy vọng điều này giúp ..

+0

Tôi thích giải pháp này tốt hơn vì bạn có thể không luôn luôn có/cần một mô hình như một tham số của các phương thức RequestHandler của bạn. 1 cho điều này. – Mukus

6

tôi đã giải quyết loại vấn đề hiện nay của bản thân mình . Điều này rất đơn giản và dễ hiểu. Trong điều khiển Spring MVC 3.0 chỉ cần đặt mã này -

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

Now "creditCardTypes" thuộc tính sẽ là avaiable trong việc tải trang hoặc trang trình phạm vi, có nghĩa là nó sẽ có sẵn bất cứ điều gì url requestmapping sẽ.

Trong jsp, đặt mã này bên trong - các loại thẻ tín dụng:

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

đây, path = "CREDITCARDTYPE" có nghĩa là các thuộc tính trong đối tượng mô hình/lệnh Spring MVC, mục = "$ {creditCardTypes } "có nghĩa là tất cả các loại thẻ tín dụng phổ biến sẽ có sẵn trong" creditCardTypes "ModelAttribute. Thats it !!!

Các vấn đề liên quan