在spring mvc中提交没有刷新页面的表单
我有一个listbox2,根据listbox1中选择的内容填充。 因此,当我提交表单时,我丢失了listbox2的数据,我必须再次在listbox1中重新选择。 如何在提交后将数据保存在listbox2中?
当我提交表单时,我丢失了listbox2的数据,我必须再次在listbox1中重新选择。
你还没有提到你如何绑定表单数据并从jsp传递给控制器,我认为你有类似下面的模型bean:
用于保存表单数据的ProductBean类:
public class ProductBean { private String listbox1Selected; private String listbox2Selected; //no-arg constructor, getters and setters }
用于在列表框或选择框中加载选项的选项类:
public class Option { private String name; public Option(){} public Option(String name) { super(); this.name = name; } }
并在jsp中使用如下所示在表单提交后选择选项:
并在控制器中返回产品模型bean传递给POST参数的spring。
@Controller @RequestMapping("/selectBox") public class SelectBoxController { @RequestMapping(method=RequestMethod.GET) public String showSelectBox(Model model){ model.addAttribute("productBean", new ProductBean()); return "selectBox"; } @RequestMapping(method=RequestMethod.POST) public String showSelectBox(@ModelAttribute("productBean")ProductBean productBean, BindingResult result, ModelMap model){ System.out.println(productBean); model.addAttribute("productBean", productBean); return "selectBox"; } @ModelAttribute("listbox1Options") public List
您应该使用Ajax来实现这一目标
function getSubList(val){ $.ajax({ url: 'your-url', type: 'post', data: {param:val}, success: function (msg) { $('#id-of-sublist').html(msg); }, }); }
现在在你的第一个列表中你可以调用onChange(this.value)
注意:您的服务器应返回以下格式的值