gpt4 book ai didi

javascript - Spring jsp页面中页面提交(页面重新加载)后保留下拉选定值

转载 作者:行者123 更新时间:2023-11-28 06:33:28 29 4
gpt4 key购买 nike

我有一个 Spring jsp 页面,其中有多个下拉菜单。用户可以从下拉列表中选择值,然后单击提交以仅查看所选值的结果。为此,我必须在提交后保留用户选择的值。我尝试将选定的索引存储在 javasript 中,但页面加载后它设置为默认值。

<form:form id="fee" method="post" modelAttribute="clientForm" commandName = "clientForm" 
action="<%= request.getContextPath().toString()%>/addFee.do">

<form:select path="client" class="myForm" style="width:235px">
<form:option value="0">All </form:option>
<form:options items="${clientsList}">
</form:options>

还有我的 javascript 函数:

function fnGo(action,elementId)
{
var elements = document.getElementsByClassName('myForm');
for (var i = 0; i < elements.length; i++)
{
if(elements[i].selectedIndex == 0)
elements[i].selectedIndex = 0;
}

document.getElementById(elementId).action = action;
document.getElementById(elementId).method = "POST";
document.getElementById(elementId).submit();
}

我的 Controller :

@RequestMapping(value="/addFee.do",method = RequestMethod.POST, params={"submit"})
protected @ResponseBody ModelAndView submitFeeValues(@ModelAttribute("clientForm") MyForm myForm )
throws Exception {

model = new ModelAndView("fee");
model.addObject("clientForm",myForm);
model.addObject("clientsList",myForm.getClientList());

return model;
}

是否有更好的方法来存储索引并在页面重新加载后传递到页面?

最佳答案

根据一个工作示例,您应该这样做,在你的jsp上你可以这样做:

    <form:form modelAttribute="clientForm"  method="post">
<form:select path="client" class="myForm" style="width:235px">
<form:option value="0">All</form:option>
<form:options items="${clientsList}">
</form:options>

</form:form>

您的客户表单如下

public class ClientForm {


private String client;

public String getClient() {
return client;
}

public void setClient(String client) {
this.client = client;
}
}

在你的 Controller 中执行如下操作:

@Controller
@RequestMapping(value = "/client")
@SessionAttributes(value = "clientForm")
public class TraductionController {
@ModelAttribute("clientForm")
public ClientForm getClientForm() {
return new ClientForm();
}

@RequestMapping(value = { "/list" }, method = RequestMethod.POST)
public String search(ModelMap model, ClientForm clientForm) throws ServiceTechException {
//get clientLists
model.addAttribute("clientsList", clientsList);
model.addAttribute("clientForm", clientForm);
return "client/list";
}

}

关于javascript - Spring jsp页面中页面提交(页面重新加载)后保留下拉选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493088/

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