简体   繁体   English

提交表单而不刷新Spring MVC中的页面

[英]Submit form without refreshing page in spring mvc

I have a listbox2 that's filled according to what has been selected in the listbox1. 我有一个listbox2,它根据listbox1中选择的内容进行填充。 So when i submit the form, i lose the data of listbox2 and i have to reselect in the listbox1 again. 因此,当我提交表单时,我丢失了listbox2的数据,并且不得不再次在listbox1中重新选择。 How can i keep data in listbox2 after submitting ? 提交后如何将数据保存在listbox2中?

<form:form action="choseElement" method="post"> 
<select name="listbox1">
 <j:forEach items="${elementList}" var="elem">
   <option>${elem.name}</option>
 </j:forEach>
</select>
</form:form>
<form:form action="addProduct" method="post"> 
 <input type="text" name="nameProd">
 <select name="listbox2">
 <j:forEach items="${LPbyElement}" var="lp">
   <option>${lp.name}</option>
 </j:forEach>
 </select>
</form:form>

You should use Ajax to achieve this 您应该使用Ajax来实现

    function getSubList(val){
           $.ajax({
                url: 'your-url',
                type: 'post',
                data: {param:val},
                success: function (msg) {
                    $('#id-of-sublist').html(msg);
                },

            });
    }

Now in your first list you can call onChange(this.value) 现在,在第一个列表中,您可以调用onChange(this.value)

Note : Your server should return values in the below format 注意:您的服务器应以以下格式返回值

<option>value1</option>
<option>value2</option>

when i submit the form, i lose the data of listbox2 and i have to reselect in the listbox1 again. 当我提交表单时,我丢失了listbox2的数据,必须再次在listbox1中重新选择。

you have not mentioned about how you binding your form data and passes to controller from jsp, am considering you have model bean like below: 您还没有提到如何绑定表单数据并从jsp传递到控制器,而是考虑使用如下所示的模型bean:

ProductBean class to hold form data: ProductBean类用于保存表单数据:

public class ProductBean {

private String listbox1Selected;
private String listbox2Selected;

  //no-arg constructor, getters and setters
}

Option class to load options in listbox or selectbox: 用于在列表框或选择框中加载选项的选项类:

public class Option {    
    private String name;

    public Option(){}

    public Option(String name) {
        super();
        this.name = name;
    }
}

and in jsp use like below to get selected option after form submit: 并在jsp中使用如下所示的方法在表单提交后获得所选选项:

<form:form method="post" modelAttribute="productBean">
    <label  for="listbox1">listbox1:</label>
    <form:select path="listbox1Selected">
        <option value="NONE">--listbox1--</option>
        <form:options items="${listbox1Options}" itemValue="name" itemLabel="name"/>
    </form:select>

    <label  for="listbox2">listbox2:</label>            
    <form:select path="listbox2Selected">
        <option value="NONE">--listbox2--</option>
        <form:options items="${listbox2Options}" itemValue="name" itemLabel="name"/>
    </form:select>

    <input type="submit" />
</form:form>

and in controller return the product model bean what spring passed to POST parameter. 并在控制器中返回弹簧传递给POST参数的产品模型bean。

@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<Option> populateOpt(){
        List<Option> lst = new ArrayList<Option>();
        lst.add(new Option("Op1"));
        lst.add(new Option("Op2"));
        lst.add(new Option("Op3"));
        return lst;
    }

    @ModelAttribute("listbox2Options")
    public List<Option> populateOpt2(){
        List<Option> lst = new ArrayList<Option>();
        lst.add(new Option("Op4"));
        lst.add(new Option("Op5"));
        lst.add(new Option("Op6"));
        return lst;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM