如何临时存储表单数据

时间:2015-05-09 23:10:21

标签: java spring-mvc

我向Scala SMT-LIB申请表寻求帮助。 我创建了带有复选框和第二页的表单,以便在此复选框中添加新行。我不知道如何添加新项目,返回表单并且不会丢失输入的数据。

enter image description here enter image description here

第一个控制器:

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

2 个答案:

答案 0 :(得分:0)

您只能为 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>

答案 1 :(得分:0)

您需要使用ModelMap(或您喜欢的任何类型的模型)来使用这些对象。

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

}