[英]How to temporarily store form data
我在spring-mvc應用程序中尋求幫助。 我創建了帶有復選框的表單,並在第二頁上向該復選框添加了新行。 我不知道如何添加新項目,返回表單並且不會丟失輸入的數據。
第一控制器:
@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.