无需刷新页面Spring MVC中提交表单 [英] Submit form without refreshing page in spring mvc

查看:468
本文介绍了无需刷新页面Spring MVC中提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有真实依据什么在为ListBox被选中填补了listbox2。因此,当我提交表单,我失去listbox2的数据,我必须重新选择为ListBox了。 我怎么可能保持listbox2数据提交后?

 <形式:形式的行动=choseElement方法=邮报>
<选择名称=ListBox1中>
 <记者:的forEach项目=$ {元素列表}VAR =ELEM>
   <选项> $ {elem.name}< /选项>
 < / J:的forEach>
< /选择>
< /形式:形式GT;
<形式:形式的行动=addProduct方法=邮报>
 <输入类型=文本名称=nameProd>
 <选择名称=listbox2>
 <记者:的forEach项目=$ {LPbyElement}VAR =LP>
   <选项> $ {lp.name}< /选项>
 < / J:的forEach>
 < /选择>
< /形式:形式GT;
 

解决方案
  

当我提交表单,我失去listbox2的数据,我必须   重新选择再次为ListBox。

你没有提到你如何绑定你的表单数据,并通过从JSP到控制器,正考虑你

有模型bean象下面这样:

的ProductBean类来保存表单数据:

 公共类的ProductBean {

私人字符串listbox1Selected;
私人字符串listbox2Selected;

  //无参数的构造函数,getter和setter
}
 

选项类加载列表框或选择框的选项:

 公共类选项{
    私人字符串名称;

    公共选择(){}

    公共选择(字符串名称){
        超();
        this.name =名称;
    }
}
 

和JSP中使用如下表格后,得到选择的选项提交:

 <形式:形式方法=邮报的ModelAttribute =的ProductBean>
    <标签=ListBox1中> ListBox1中:其中; /标签>
    <形式:选择路径=listbox1Selected>
        <期权价值=NONE>  -  ListBox1的 - < /选项>
        <形式:选择项目=$ {listbox1Options}itemValue =名称itemLabel =名/>
    < /形式:选择>

    <标签=listbox2> listbox2:LT; /标签>
    <形式:选择路径=listbox2Selected>
        <期权价值=NONE>  -  listbox2  - < /选项>
        <形式:选择项目=$ {listbox2Options}itemValue =名称itemLabel =名/>
    < /形式:选择>

    <输入类型=提交/>
< /形式:形式GT;
 

和控制器返回什么春天传递给POST参数的产品模型bean。

  @Controller
@RequestMapping(/选择框)
公共类SelectBoxController {

    @RequestMapping(方法= RequestMethod.GET)
    公共字符串showSelectBox(型号机型){

        model.addAttribute(的ProductBean,新的ProductBean());
        返回选择框;
    }

    @RequestMapping(方法= RequestMethod.POST)
    公共字符串showSelectBox(@ModelAttribute(一个ProductBean)的ProductBean的ProductBean,BindingResult结果,ModelMap模型){
        的System.out.println(的ProductBean);
        model.addAttribute(的ProductBean的ProductBean);
        返回选择框;
    }

    @ModelAttribute(listbox1Options)
    公开名单<选项> populateOpt(){
        名单<选项> LST =新的ArrayList<选项>();
        lst.add(新选项(OP1));
        lst.add(新选项(符Op2));
        lst.add(新选项(OP3));
        返回LST;
    }

    @ModelAttribute(listbox2Options)
    公开名单<选项> populateOpt2(){
        名单<选项> LST =新的ArrayList<选项>();
        lst.add(新选项(OP4));
        lst.add(新选项(OP5));
        lst.add(新选项(OP6));
        返回LST;
    }
}
 

I have a listbox2 that's filled according to what has been selected in the listbox1. So when i submit the form, i lose the data of listbox2 and i have to reselect in the listbox1 again. How can i keep data in listbox2 after submitting ?

<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>

解决方案

when i submit the form, i lose the data of listbox2 and i have to reselect in the listbox1 again.

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:

ProductBean class to hold form data:

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:

<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.

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

这篇关于无需刷新页面Spring MVC中提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆