Vinoth
Vinoth

Reputation: 63

How to go back to the same JSP page in Spring MVC with the same values

Please help me on this. Below is my LoginController class where I pass the username and password from a JSP and then validate and display the to do list of the username in diffrent jsp page.

Here is my code

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

Here is my 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>

From the above JSP if I click the "Add" button then it will goes to the below 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>  

From the above JSP if click submit then it will call the below method inside the same Logincontroller class and this is to add new users .

Once new user added I want this method to go back to the same "UserToDoList.jsp" with the same output. How to achieve this , Please help.

Below method is from LoginController.class

@RequestMapping(value = "/adduser")
public String adduser(LoginForm request) throws Exception {
    String result = loginservice.adduser(request);
    return "/Adduser";
 }

Below id my bean to get the username

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

}

Upvotes: 0

Views: 5226

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148890

I think you should clearly separate the login phase from the todo list extraction. It could look like :

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

Then in the POST part for adduser, you could have :

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

Alternatively, you could try to rebuild a LoginForm from sessionUser (and eventually other session scoped beans), say sessionRequest and directly use :

List result = loginservice.ValidateLogin(sessionRequest);

Upvotes: 2

Basavaraj
Basavaraj

Reputation: 1111

@RequestMapping(value = "/adduser")
public String adduser(LoginForm request) throws Exception {
String result = loginservice.adduser(request);
return "UserToDoList";
}

return the file name which you want to show when you click on add button.

Upvotes: 0

Related Questions