繁体   English   中英

如何临时存储表格数据

[英]How to temporarily store form data

我在应用程序中寻求帮助。 我创建了带有复选框的表单,并在第二页上向该复选框添加了新行。 我不知道如何添加新项目,返回表单并且不会丢失输入的数据。

在此处输入图片说明在此处输入图片说明

第一控制器:

@Controller
@RequestMapping("/training")
public class TrainingController {

    @Autowired
    private TrainingDAO trainingDAO;

    @Autowired
    private TrainingNameDAO trainingNameDAO;

    @RequestMapping("/add")
    public ModelAndView addTraining() {

        ModelAndView modelAndView = new ModelAndView("addTraining");
        Training trainig = new Training();
        modelAndView.addObject("training", training);
        modelAndView.addObject("trainingNames", trainingNameDAO.findAll());

        return modelAndView;
    }

}

第二种形式的控制器:

@Controller
@RequestMapping("/trainingName")
public class TrainingNameController {

    @Autowired
    private TrainingNameDAO trainingNameDAO;

    @RequestMapping("/add")
    public ModelAndView addTrainingName() {

        ModelAndView modelAndView = new ModelAndView("addTrainingName");

        TrainingName trainingName= new TrainingName();
        modelAndView.addObject("trainingName", trainingName);
        return modelAndView;
    }
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String saveTrainingName(@Valid TrainingName trainingName, BindingResult binding) {

        if (binding.hasErrors()) {
            return "addTrainingName";
        } else {
            trainingNameDAO.save(trainingName);
            return "redirect:/training/add.htm";
        }
    }
}

addTraining.jsp

   <form:form  method="POST" action="add.htm" modelAttribute="training">
                <table>
                    <tr>
                        <td><form:hidden path="id"/></td>
                    </tr>
                    <tr>
                        <td><form:label path="no">Training name:</form:label></td>
                        <td><form:select items="${trainingNames}" path="trainigName.id" itemValue="id"
                                         itemLabel="name"></form:select></td>
                        <td><a href="<c:url value="/trainingName/add.htm"/>">+New name</a></td>
                    </tr>
                    <tr>
                        <td><form:label path="no">No:</form:label></td>
                        <td><form:input path="no" required="true"/><form:errors
                                path="no"/></td>
                    </tr>
                    <tr>
                        <td><form:label path="price">Price:</form:label></td>
                        <td><form:input path="price" required="true"/><form:errors
                                path="price"/></td>
                    </tr>
                    <tr>
                        <td><form:label path="date">Date:</form:label></td>
                        <td><form:input path="date" placeholder="yyyy-mm-dd" required="true"/><form:errors
                                path="date"/></td>
                    </tr>
                    <tr>
                        <td><form:label path="count">Count:</form:label></td>
                        <td><form:input path="count" required="true"/><form:errors
                                path="count"/></td>
                    </tr>

                    <tr>
                        <td><form:button>Save</form:button></td>
                    </tr>
                </table>
            </form:form>

addTrainingName.jsp

  <form:form commandName="trainingName" method="POST" action="add.htm">
        <table>
            <tr>
                <td><form:hidden path="id"/></td>
            </tr>
            <tr>
                <td><form:label path="name">Name:</form:label></td>
                <td><form:input path="name" required="true"/><form:errors
                        path="name"/></td>
            </tr>
            <tr>
                <td><form:label path="shortcut">Shortcut:</form:label></td>
                <td><form:input path="shortcut" required="true"/><form:errors
                        path="shortcut"/></td>
            </tr>

            <tr>
                <td><form:button>Save</form:button></td>
            </tr>
        </table>
    </form:form>

您只能将一个控制器用于CRUD操作:

@Controller
@RequestMapping("/training")
public class TrainingController {

    @Autowired
    private TrainingDAO trainingDAO;

    @Autowired
    private TrainingNameDAO trainingNameDAO;


    @RequestMapping(value = "/list")
    public ModelAndView trainingListRequest() {
        //here list all trainings
        ModelAndView modelAndView = new ModelAndView("trainings");
        modelAndView.addObject("trainingNames", trainingNameDAO.findAll());
        return modelAndView;
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ModelAndView issueSaveRequest(@ModelAttribute trainingName TrainingName) {
        //here save your training and redirect to list
        trainingNameDAO.save(trainingName);
        return "redirect:/training/list";
    }

    @RequestMapping(value = "/new")
    public ModelAndView trainingNewRequest() {
        /*here create a new training, attach it to the related view
          and return that view (the save button of it should be passed
          to the save method of this controller)*/
    }

    //you may chose edit by Id
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView trainingEditRequest(HttpServletRequest request) {
        /*here get the editing training, attach it to the related view
          and return that view (the save button of it should be passed
          to the save method of this controller)*/
    }

    //you may delete it by Id
    @RequestMapping(value = "/delete", method = RequestMethod.GET) 
    public ModelAndView trainingDeleteRequest(HttpServletRequest request) {
        //here delete the training and redirect to list of trainings
    }    
}

在这种情况下,培训形式应该是这样的:

<form:form action="save" method="post" modelAttribute="training">
.
.
.
<input type="submit" value="Save">
.
.
</form:form>

您需要使用ModelMap(或您喜欢的任何一种Model)来带那些对象。

@Controller
@RequestMapping("/training")
public class TrainingController {

@Autowired
private TrainingDAO trainingDAO;

@Autowired
private TrainingNameDAO trainingNameDAO;

@RequestMapping("/add")
public ModelAndView addTraining(ModelMap carryingObject) {

    //push your form object using the same way as those trainingNames in your previous Controller when you added new TrainingName
    YourFormObject form = carryingObject.get("your-modelmap-key-for-your-form");

    ModelAndView modelAndView = new ModelAndView("addTraining");
    Training trainig = new Training();
    modelAndView.addObject("training", training);
    modelAndView.addObject("trainingNames", trainingNameDAO.findAll());
    modelAndView.addObject("my-form-data", form);        

    return modelAndView;
}

}

暂无
暂无

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

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