我之前问了一个问题,如何使用 Webflow 做到这一点,但事实证明这对我的情况不切实际。
我正在尝试浏览 3 个屏幕,这些屏幕添加到对象信息中,然后需要确认最后添加到数据库中。[保持简单等]
例如,第一个屏幕输入用户名,然后下一个屏幕需要联系信息,然后第三个屏幕显示摘要并要求确认
我无法弄清楚如何通过多个屏幕传递同一个对象。我了解如何将信息从一个屏幕传递到下一个屏幕,但由于某种原因,相同的技术不适用于多个屏幕。
示例 3 页:
添加用户.jsp
<div id="form">
<h2 >Step 1</h2>
<form action="AddUserContact" method="post">
<table>
<tr>
<td>User Name:</td>
<td><input type="text" id="username" name="username"/></td>
</tr>
<tr>
<td><input type="submit" value="Next"/></td>
</tr>
</table>
</form>
</div>
AddUserContact.jsp
<div id="form">
<h2 >Step 2</h2>
<form action="UserSummaryConfirm" method="post">
<table>
<tr>
<td>${user.username}</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" id="address" name="address"/></td>
</tr>
<tr>
<td><input type="submit" value="Next"/></td>
</tr>
</table>
</form>
</div>
UserSummaryConfirm.jsp
<h2>Step 3</h2>
<form action="home" method="post">
<table>
<tr>
<td>User Name:</td>
<td>${user.username}</td>
</tr>
<tr>
<td>Address:</td>
<td>${address}</td>
</tr>
<tr>
<td><a href="home">Confirm</a></td>
</tr>
</table>
</form>
我的每个页面都有一个控制器 [它让我更好地理解发生了什么,稍后我会简化它]
添加用户控制器.java
@Controller
public class AddUserController{
@RequestMapping(value = "AddUser")
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = new User();
ModelAndView mav = new ModelAndView("AddUser");
user.setUserName(request.getParameter("username"));
mav.addObject("user", user);
return mav;
}
}
AddUserContactController.java
@Controller
public class AddUserContactController{
@RequestMapping(value = "AddUserContact")
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("AddUserContact");
mav.addObject("user", request.getParameter("user"));
return mav;
}
}
AddUserConfirm.java
@Controller
public class AddUserConfirm{
@RequestMapping(value = "UserSummaryConfirm")
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("UserSummaryConfirm");
mav.addObject("user", request.getParameter("user"));
mav.addObject("address", request.getParameter("address"));
return mav;
}
}
然后 User 类只是一个带有 getter 和 setter 的简单类。我遇到的问题是无论我尝试传递对象是什么,我似乎都无法弄清楚为什么相同的技术不起作用。
第三个屏幕上的地址显示没有问题,但用户名没有显示在任何屏幕上。
使用 webflow,我按照我的方式创建了一个对所有 webflow 屏幕都是全局的 UserBean。从任何屏幕添加到同一个对象并显示任何信息都很容易。我怎样才能达到同样的结果呢?
谢谢你。
工作代码:
使用会话属性
添加用户.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<div id="form">
<h2 >Step 1</h2>
<form:form action="AddUserContact" commandName="user" method="POST">
<table>
<tr>
<td>User Name:</td>
<td><form:input type="text" id="username" path="username"/></td>
</tr>
<tr>
<td><input type="submit" value="Next"/></td>
</tr>
</table>
</form:form>
</div>
AssUserContact.jsp
<div id="form">
<h2 >Step 2</h2>
<form:form action="UserSummaryConfirm" commandName="user" method="post">
<table>
<tr>
<td>Address:</td>
<td><input type="text" id="address" path="address"/></td>
</tr>
<tr>
<td><input type="submit" value="Next"/></td>
</tr>
</table>
</form>
</div>
UserSummaryConfirm.jsp
<h2>Step 3</h2>
<form:form action="home" method="get">
<table>
<tr>
<td>User Name:</td>
<td><%=session.getAttribute("username")%></td>
</tr>
<tr>
<td>Address:</td>
<td><%=session.getAttribute("address")%></td>
</tr>
<tr>
<td><a href="home">Confirm</a></td>
</tr>
</table>
</form:form>
添加用户控制器.java
@Controller
@SessionAttributes({ "username", "address" })
public class AddUserController{
User usr = new User();
@RequestMapping(value = "AddUser")
public String loadIndex(Model model, User user) {
model.addAttribute("User", user);
return "AddUser";
}
@RequestMapping(value = "AddUserContact")
public String processUserName(Model model, User user) {
usr.setUsername(user.setUsername());
model.addAttribute("User", user);
return "AddUserContact";
}
@RequestMapping(value = "UserSummaryConfirm")
public String processUserContact(Model model, User user) {
usr.setAddress(user.getAddress());
model.addAttribute("username", usr.getUsername());
model.addAttribute("address", usr.getAddress());
return "UserSummaryConfirm";
}