在spring jsp页面中提交页面提交(页面重新加载)后保留下拉选择的值

时间:2015-12-28 11:56:03

标签: javascript html spring jsp

我是一个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();
}

我的控制器:

@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;
}

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

1 个答案:

答案 0 :(得分:0)

基于一个工作示例,你应该这样做, 在你的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>

您的ClientForm喜欢这个

public class ClientForm {


    private String              client;

    public String getClient() {
        return client;
    }

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

在您的控制器中执行以下操作:

@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";
    }

}