如何在 Spring MVC 中填充下拉框
Posted
技术标签:
【中文标题】如何在 Spring MVC 中填充下拉框【英文标题】:How to populate dropdown box in Spring MVC 【发布时间】:2012-05-08 13:31:36 【问题描述】:我一直在尝试找出。关于这个主题有一些线程,但我发现它们都没有帮助我,所以我希望这里有人可以帮助我。
这是我的控制器:
@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";
【问题讨论】:
【参考方案1】:@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;
仅供参考 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>
希望这会有所帮助..
【讨论】:
我更喜欢这个解决方案,因为您可能并不总是拥有/需要模型作为 RequestHandler 方法的参数。为此 +1。【参考方案2】:不确定调用了哪个 Controller 方法来使用 documentNumberList
显示您的视图,但您需要将该集合添加到传递给此视图的模型中:
model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers());
尽管从您的异常堆栈跟踪中您也错过了 @Autowired on documentService
字段。
【讨论】:
【参考方案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”属性将在页面加载或页面提交范围内可用,这意味着无论请求映射 url 是什么,它都可用。
在 jsp 中,将此代码放在 - 信用卡类型中:
<form:select path="creditCardType">
<option value="Select" label="Select a card type"></option>
<form:options items="$creditCardTypes" />
</form:select>
这里,path="creditCardType" 表示Spring MVC模型/命令对象中的属性, items="$creditCardTypes" 表示所有填充的信用卡类型都将在“creditCardTypes”模型属性中可用。 就是这样!!!
【讨论】:
以上是关于如何在 Spring MVC 中填充下拉框的主要内容,如果未能解决你的问题,请参考以下文章
HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC)