gpt4 book ai didi

java - 在 Spring MVC 中处理多个页面

转载 作者:行者123 更新时间:2023-12-01 18:21:36 25 4
gpt4 key购买 nike

我是 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);
}

ProjectDao

   public void addProject(Project project);

ProjectDaoImpl

   public void addProject(Project project) {
getCurrentSession().save(project);
}

项目 Controller

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

JSP1addProject.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>

JSP2addProjectExt.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 中使用提交按钮的目标,以便 Controller 知道并区分下一个按钮和提交按钮。

关于java - 在 Spring MVC 中处理多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com