“现在用户属于美国,因此在页面加载时应默认选择美国。”你知道手边的用户属于某个国家。
假设你有一个POJO国家为:
public class Country{
String countryName;
String countryId;
//setters and getters
}
public class YourForm{
List<Country> countryList;
String selectedCountryId;
...
//setters and getters
}
在你的控制器的方法委托给JSP:
...
YourForm form = new YourForm();
//set your countrylist to form
//set country user belongs to - selectedCountryId - since you know before hand user belongs to some country.
model.addAttribute("yourForm", form):
...
现在访问它在JSP为:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
</head>
<body>
<form:form id="yourForm" modelAttribute="yourForm" method="post">
<tr>
<td width="50%" class="label">
<form:select id="countryId" path="selectedCountryId" title='Select Country'>
<option value="">Please Select</option>
<form:options items="${countryList}" itemValue="countryId" itemLabel="countryName"/>
</form:select>
</td>
</tr>
</body>
由于selectedCountryId已经在控制器中设置,您将在jsp中看到该国家为自动选择的国家。
谢谢。工作得很好。我正在使用<选择标记.....用