gpt4 book ai didi

javascript - 在页面加载时在编辑 jsp 页面的下拉列表中显示所选值(Spring MVC)

转载 作者:行者123 更新时间:2023-11-30 08:44:44 25 4
gpt4 key购买 nike

我想在页面加载时在下拉列表中显示所选值。我的编辑页面显示一个值,国家。现在用户属于美国,因此在页面加载时默认选择美国。我可以使用 javascript 或 jquery。

我在 Internet 上找到的帮助很少,但它们都带有 scriplet (<% %>),我不想使用它们。

我正在传递一个包含 Id 和 Value 的列表。我在另一个对象中也有 Id。我可以通过在 jsp 中引入 for 循环并显示值来编写代码,但可能是我在那里犯了一些错误。也想知道是否有更好的方法来做到这一点。

我正在使用 Spring MVC。

谢谢。

最佳答案

“现在用户属于美国,因此在页面加载时应默认选择美国。”。您事先知道用户属于某个国家/地区。

假设您有一个 pojo Country 作为:

    public class Country{ 
String countryName;
String countryId;
//setters and getters
}

public class YourForm{
List<Country> countryList;
String selectedCountryId;
...
//setters and getters
}

在委托(delegate)给 jsp 的 Controller 方法中:

    ...
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 已在 Controller 中设置,您将在 jsp 中看到该国家/地区被自动选中。

关于javascript - 在页面加载时在编辑 jsp 页面的下拉列表中显示所选值(Spring MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com