ClientBean
import java.util.ArrayList;
import java.util.Date;
public class ClientBean {
private Integer clientid;
private String clientname;
private String paymenttype;
private Date joiningdate;
private String email;
private ArrayList<String> paymentnames;
public ArrayList<String> getPaymentnames() {
return paymentnames;
}
public void setPaymentnames(ArrayList<String> paymentnames) {
this.paymentnames = paymentnames;
}
public Integer getClientid() {
return clientid;
}
public void setClientid(Integer clientid) {
this.clientid = clientid;
}
public String getClientname() {
return clientname;
}
public void setClientname(String clientname) {
this.clientname = clientname;
}
public String getPaymenttype() {
return paymenttype;
}
public void setPaymenttype(String paymenttype) {
this.paymenttype = paymenttype;
}
public Date getJoiningdate() {
return joiningdate;
}
public void setJoiningdate(Date joiningdate) {
this.joiningdate = joiningdate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
ClientController
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView editClient(@ModelAttribute("command") ClientBean clientBean,
BindingResult result){
Map<String, Object> model = new HashMap<String, Object>();
model.put("client", prepareClientBean(clientService.getClient(clientBean.getClientid())));
return new ModelAndView("addClient", model);
}
private ClientBean prepareClientBean(Client client){
ClientBean bean = new ClientBean();
ArrayList<String> modes = new ArrayList<String>();
modes.add("Monthly");
modes.add("Quaterly");
modes.add("Half-Yearly");
modes.add("Yearly");
bean.setPaymentnames(modes);
bean.setClientname(client.getClientname());
bean.setEmail(client.getEmail());
bean.setJoiningdate(client.getJoiningdate());
bean.setPaymenttype(client.getPaymenttype());
bean.setClientid(client.getClientid());
return bean;
和
addClient.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Client Data</title>
</head>
<script>src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
<h2>Add Client Data</h2>
<form:form method="POST" action="/ClientDetails/save.html" onsubmit="return ValidateForm();">
<table>
<tr>
<td><form:label path="clientid">Client ID:</form:label></td>
<td><form:input path="clientid" value="${client.clientid}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="clientname">Client Name:</form:label></td>
<td><form:input path="clientname" value="${client.clientname}"/></td>
<tr>
<td><form:label path="paymenttype">Client Payment Type:</form:label></td>
<td><form:select path="paymenttype">
<form:options items="${client.paymentnames}" itemValue="{client.clientid}" />
</form:select></td>
</tr>
<tr>
<td><form:label path="joiningdate">Client Joining Date:</form:label></td>
<td><form:input path="joiningdate" value="${client.joiningdate}"/></td>
</tr>
<tr>
<td><form:label path="email">Client Email:</form:label></td>
<td><form:input path="email" value="${client.email}"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
<c:if test="${!empty clients}">
<h2>List Clients</h2>
<table align="left" border="1">
<tr>
<th>Client ID</th>
<th>Client Name</th>
<th>Payment Type</th>
<th>Joining Date</th>
<th>Client Email</th>
</tr>
<c:forEach items="${clients}" var="client">
<tr>
<td><c:out value="${client.clientid}"/></td>
<td><c:out value="${client.clientname}"/></td>
<td><c:out value="${client.paymenttype}"/></td>
<td><c:out value="${client.joiningdate}"/></td>
<td><c:out value="${client.email}"/></td>
<td align="center"><a href="edit.html?clientid=${client.clientid}">Edit</a> </td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
我们如何在jsp中的paymentNames下拉列表中选择正确的值?
答案 0 :(得分:0)
请尝试在itemValue="paymenttype"
中添加form:select
。 Spring将选择path
匹配的选项值。
<form:select path="paymenttype" itemValue="paymenttype">
<form:options items="${client.paymentnames}" />
</form:select>
<强>更新强>
您的模型属性名称是“command”:
@ModelAttribute("command") ClientBean clientBean
但是在更新了client
之后,您将其保存在不同的模型属性(即客户端)名称中,如下所示:
model.put("client", prepareClientBean(clientService.getClient(clientBean.getClientid())));
因此绑定操作无法正常工作。
<强>解强>
因此,如果您更改模型名称和与之相关的表单元素,它可能会起作用。
模型名称与@ModelAttribute(“command”)相同:
model.put("command", prepareClientBean(clientService.getClient(clientBean.getClientid())));
弹簧形式:
<form:select path="paymenttype">
<form:options items="${command.paymentnames}" />
</form:select>
或强>
您可以使用名为client
的模型。但是,在这种情况下,您应该以spring形式添加modelAttribute =“client”(或commandName =“client”)。
@ModelAttribute("client") ClientBean clientBean
model.put("client", prepareClientBean(clientService.getClient(clientBean.getClientid())));
<form:form method="POST" modelAttribute="client">
<form:select path="paymenttype">
<form:options items="${client.paymentnames}" />
</form:select>
</form:form>
Also:
Java遵循camelcase语法来命名类,接口,方法和变量。您应该使用paymentType
代替paymenttype
。