请帮我解决这个问题。下面是我的LoginController类,我从JSP传递用户名和密码,然后在不同的jsp页面中验证并显示用户名的待办事项列表。
这是我的代码
LoginController.class
@RequestMapping(value = "/loginvalidate")
public String validatelogin(LoginForm request, ModelMap map) throws Exception {
String userName = request.getUserName();
sessionUser.setUserName(userName);
map.addAttribute("user", sessionUser.getUserName());
map.addAttribute("dateTime", sessionscopebillingdata.getDate());
System.out.println("username from the controller class " + sessionUser.getUserName());
List result = loginservice.ValidateLogin(request);
map.addAttribute("ToDoList", result);
return "UserToDoList";
}
这是我的UserToDoList.jsp
<html>
<head>
<title>Home</title>
<div class="inset">
<%=session.getAttribute("dateTime")%>
<h2> TO DO LIST </h2>
<%=session.getAttribute("user")%>
<link href="style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<form action="logout" method="POST">
<button type="submit">Logout</button>
</form>
<form name="Add" action="Adduser" method="post">
<button type="submit">ADD</button>
</form>
</link>
<form:form>
<c:if test="${not empty ToDoList}">
<table>
<tr><th>LOGIN ID</th>
<th>LAST UPDATE BY</th>
<th>TRANSACTION TYPE</th>
<th>STATUS</th>
</tr>
<c:forEach var="item" items="${ToDoList}">
<tr> <td>${item[0]}</td>
<td>${item[1]}</td>
<td>${item[2]}</td>
<td>${item[3]}</td>
</tr>
</c:forEach>
</table>
</c:if>
</form:form>
</body>
</html>
如果单击“添加”按钮,从上面的JSP中,它将转到下面的JSP
AddUser.jsp
<form:form method="POST" name="LoginForm" modelAttribute="roleId" action="adduser">
<ul>
<li>
<label>User Name</label> <input type="text" name="userName" />
</li>
<li>
<label>Password</label> <input type="text" name="passWord" />
</li>
<li>
<label>Approver </label> <input type="text" name="approver" />
</li>
<li>
<label>User Role </label>
<form:select path="userRole" items="${Codes}" tabindex="50">
<form:option value="----------Please select---------" />
<form:option value="${role.id}" />
<form:option value="${role.codeValue}" />
</li>
</form:select>
</ul>
<input type="submit" value="Submit">
</form:form>
从上面的JSP中点击提交然后它会在同一个Logincontroller类中调用下面的方法,这就是添加新用户。
添加新用户后,我希望此方法返回到具有相同输出的相同“UserToDoList.jsp”。 如何实现这一点,请帮忙。
下面的方法来自LoginController.class
@RequestMapping(value = "/adduser")
public String adduser(LoginForm request) throws Exception {
String result = loginservice.adduser(request);
return "/Adduser";
}
在我的bean下面获取用户名
@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class BillingSessionScopeUser {
public String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
答案 0 :(得分:2)
我认为您应该清楚地将登录阶段与待办事项列表提取分开。它可能看起来像:
@RequestMapping(value = "/loginvalidate")
public String validatelogin(LoginForm request, ModelMap map, Session session) throws Exception {
String userName = request.getUserName();
sessionUser.setUserName(userName);
map.addAttribute("user", sessionUser.getUserName());
map.addAttribute("dateTime", sessionscopebillingdata.getDate());
System.out.println("username from the controller class " + sessionUser.getUserName());
// BEGIN CHANGE
List result = loginservice.getTodoList(userName);
//END CHANGE
map.addAttribute("ToDoList", result);
return "UserToDoList";
}
然后在POST
的{{1}}部分,您可以:
adduser
或者,您可以尝试从@RequestMapping(value = "/adduser")
public String adduser(@ModelAttribute LoginForm request, ModelMap model) throws Exception {
String result = loginservice.adduser(request);
String userName = sessionUser.getUserName();
List result = loginservice.getTodoList(userName);
map.addAttribute("ToDoList", result);
map.addAttribute("user", userName);
map.addAttribute("dateTime", sessionscopebillingdata.getDate());
return "UserToDoList"; // ensure correct name for view
}
(以及最终其他会话范围的bean)重建LoginForm
,例如sessionUser
并直接使用:
sessionRequest
答案 1 :(得分:0)
@RequestMapping(value = "/adduser")
public String adduser(LoginForm request) throws Exception {
String result = loginservice.adduser(request);
return "UserToDoList";
}
单击“添加”按钮时,返回要显示的文件名。