我是SpringMVC的新手,正在尝试在SpringMVC3中实现此示例。 http://www.mkyong.com/spring- mvc/spring-mvc-handling-multipage-forms-with- abstractwizardformcontroller/ 当我提交第一个Jsp时,我可以转到下一个Jsp,但是在提交第二个Jsp之后,它正在重定向返回到具有新Jsp(空值)的第一个Jsp。请提出建议,以使我更清楚地理解。
项目
@Id @Column(name="PROJECT_ID") @GeneratedValue(strategy=GenerationType.AUTO) private int projectId; @Column(name="PROJECT_NAME") private String projectName; @Column(name="LOCATION") private String location; @Column(name="DESCRIPTION") private String description; @Column(name="DEV_TYPE") private String developmentType;
项目服务
public void addProject(Project project);
ProjectServiceImpl
@Autowired private ProjectDao projectDao; public void addProject(Project project) { projectDao.addProject(project); }
道计划
ProjectDaoImpl
public void addProject(Project project) { getCurrentSession().save(project); }
项目控制器
@RequestMapping(value = "/addProject.htm", method = RequestMethod.GET) public ModelAndView reportForm(ModelMap model, HttpServletRequest request, Project project) { model.addAttribute("project", project); ModelAndView mav = new ModelAndView("add"); return mav; } @RequestMapping(value = "/addProject.htm", method = RequestMethod.POST) public ModelAndView addConfirm(@ModelAttribute("project") Project project, BindingResult result, ModelMap model, SessionStatus status, HttpServletRequest request) { HttpSession session = request.getSession(); validator.validateProject(project, result); if (result.hasErrors()) { return new ModelAndView("addProject"); } else if (project.getDescription() == null) { //return the form that will set field two's value return new ModelAndView("addProjectExt"); } //and so on for all the other field that need to be set... else{ model.addAttribute("project", project); projectService.addProject(project); return "redirect:listProject"; } }
JSP1 addProject.jsp
<form:form method="POST" modelAttribute="project" action="addProject.htm" > <div class="input"> <form:label path="projectName" type="text" value="" >Project Name :*</form:label> <form:input path="projectName"/> </div> <div class="input"> <form:label path="location" type="text" value="" >Location :</form:label> <form:input path="location"/> </div> </form:form>
JSP2 addProjectExt.jsp
<form:form method="POST" modelAttribute="project" action="addProject.htm" > <div class="input"> <form:label path="description" type="text" value="" >Description :*</form:label> <form:input path="description"/> </div> <div class="input"> <form:label path="developmentType" type="text" value="" >Development Type :</form:label> <form:input path="developmentType"/> </div> </form:form>
请尝试使用@SessionAttribute(“ project”)
并在JSP中使用提交按钮的目标,以便控制器知道并区分下一个按钮和提交按钮。