本文介绍了Spring MVC添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的帮助,因为标题说我从jsp表单插入多行时遇到问题.该代码来自此站点的答案.

i need your help as the title says i have a problem inserting multiple rows from a jsp form. The code is from an answer from this site.

控制器

@ModelAttribute("programform")
public ProgramForm populatePojos() {
    // Don't forget to initialize the pojos list or else it won't work
    ProgramForm programform = new ProgramForm();
    List<Programs> programs = new ArrayList<Programs>();
    for(int i=0; i<2; i++) {
        programs.add(new Programs());

    }
    programform.setPrograms(programs);
    return programform;
}




@RequestMapping(value = "/createprog")
public ModelAndView tiles2(@ModelAttribute("programform") ProgramForm  programform,
        BindingResult result) {
    //Map<String, Object> model = new HashMap<String, Object>();
    //model.put("articles",  articleService.listArticles());

    return new ModelAndView("createprogram");
}


@RequestMapping(value = "/saveprogram", method = RequestMethod.POST)
public ModelAndView saveProgram(@ModelAttribute("programform") ProgramForm  programform,
        BindingResult result) {
    for(Programs programs : programform.getPrograms()) {
           System.out.println(programs.getProgram_id());
           articleService.addProgram(programs);
        }

jsp

 <c:url var="saveProgramUrl" value="/articles/saveprogram.html" />
<form:form modelAttribute="programform" method="POST" action="${saveProgramUrl}">
<table>
<tr>
            <th>ProgramId</th>
            <th>Date</th>
            <th>Type</th>
            <th>Start</th>
            <th>End</th>
            <th>Location</th>
            <th>User</th>
            <th>Program Name</th>
</tr>
    <tr>
                <th><form:input path="programs[0].program_id" /></th>
                 <th> <form:input path="programs[0].date" id="datepick" class="date-pick"/>
        <script type="text/javascript">
            datepickr('datepick', { dateFormat: 'Y-m-d' });
        </script></th>
                <th><form:input path="programs[0].type" /></th>
                <th><form:input path="programs[0].start" /></th>
                <th><form:input path="programs[0].end" /></th>
                <th><form:input path="programs[0].location" /></th>
                <th><form:input path="programs[0].user_id" /></th>
                <th><form:input path="programs[0].program_name" /></th>
    </tr>
 <tr>
                <th><form:input path="programs[1].program_id" /></th>
                 <th> <form:input path="programs[1].date" id="datepick" class="date-pick"/>
        <script type="text/javascript">
            datepickr('datepick', { dateFormat: 'Y-m-d' });
        </script></th>
                <th><form:input path="programs[1].type" /></th>
                <th><form:input path="programs[1].start" /></th>
                <th><form:input path="programs[1].end" /></th>
                <th><form:input path="programs[1].location" /></th>
                <th><form:input path="programs[1].user_id" /></th>
                <th><form:input path="programs[1].program_name" /></th>
    </tr>
</table>
<br/>
<input type="submit" value="Insert User" />
</form:form>

程序

    package net.roseindia.model;



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "program")
public class Programs {




    @Id
    @Column(name = "Program_Id")
    private int program_id;
    @Column(name = "Date")
    private String date;
    @Column(name = "Duty_Type")
    private String type;
    @Column(name = "Duty_Start_Time")
    private String start;
    @Column(name = "Duty_End_Time")
    private String end;
    @Column(name = "Location")
    private String location;
    @Column(name = "User_Id")
    private int user_id;
    @Column(name = "Program_Name")
    private String program_name;


    public int getProgram_id() {
        return program_id;
    }


    public void setProgram_id(int program_id) {
        this.program_id = program_id;
    }


    public String getDate() {
        return date;
    }


    public void setDate(String date) {
        this.date = date;
    }


    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }




    public String getStart() {
        return start;
    }


    public void setStart(String start) {
        this.start = start;
    }


    public String getEnd() {
        return end;
    }


    public void setEnd(String end) {
        this.end = end;
    }


    public String getLocation() {
        return location;
    }


    public void setLocation(String location) {
        this.location = location;
    }


    public int getUser_id() {
        return user_id;
    }


    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }


    public String getProgram_name() {
        return program_name;
    }


    public void setProgram_name(String program_name) {
        this.program_name = program_name;
    }


    public Programs() {

    }





}

ProgramForm

ProgramForm

package net.roseindia.model;

import java.util.List;

public class ProgramForm {



    private List<Programs> programs;

    public List<Programs> getPrograms() {
        return programs;
    }

    public void setPrograms(List<Programs> programs) {
        this.programs = programs;
    }


}

当我按下按钮将其保存到没有插入数据库的程序时,我在做什么错?

When i push the button to save this to programs nothing inserted in the database, what i am doing wrong?

推荐答案

要添加多行,您需要延迟初始化列表.请按照以下步骤在@ModelAttribute("programform")中更改您的代码,然后重试.

To add multiple rows you need to lazy init your list. Please change your code in the @ModelAttribute("programform") as per following and then try again.

@ModelAttribute("programform")
public ProgramForm populatePojos() {
    // Don't forget to initialize the pojos list or else it won't work
    ProgramForm programform = new ProgramForm();
    List<Programs> programs = LazyList.decorate(new ArrayList<Programs>(), FactoryUtils.instantiateFactory(Programs.class));

    for(int i=0; i<2; i++) {
        programs.add(new Programs());

    }
    programform.setPrograms(programs);
    return programform;
}

通过如上所述使列表无效,您还可以使用javascript添加更多运行时字段,并可以将这些值绑定到列表中.

With having list inilialized as above you can also add more fields run time using javascript and can get those values binded in your list.

执行此更改后,如果它不起作用,则需要在jsp页面中进行更改.除了使用use simple标签之外,不要忘记在name属性中指定与在path属性中指定的值相同的值.

After doing this changes if it won't work then you need to do changes in your jsp page. Instead of using the use simple tag but don't forget to specify the same value in name attribute as you have specified in the path attribute here.

希望这对您有所帮助.干杯.

Hope this helps you. Cheers.

这篇关于Spring MVC添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:28