2
votes

I have two action classes,namely loginAction and EditUserAction class.The Login action class contains an arraylist of objects which is iterated in the JSP page,namely View User Page.

This is me LoginAction.java {

package posidex.Demo.UAM;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.dispatcher.SessionMap;

import posidex.Demo.DAO.DataModel;
import posidex.Demo.DAO.GetDBCOnnection;
import posidex.Demo.DAO.SessionBase;

public class LoginAction extends SessionBase {

    Connection conn = null;

    Properties readFile = new Properties();

    FileInputStream fileStream = null;

    ResultSet recordSet = null;

    DataModel resultModel = null;

    PreparedStatement forResult = null;

    SessionMap<String, Object> loginMap = null;

    ArrayList<DataModel> list = new ArrayList<DataModel>();

    public ArrayList<DataModel> getList() {
        return list;
    }

    public void setList(ArrayList<DataModel> list) {
        this.list = list;
    }

    Logger log = LogManager.getLogger(LoginAction.class);

    public String getDbDetails() {

        try {

            log.debug("Inside getDbDetails Method");
            fileStream = new FileInputStream(
                    "/home/ast-developer/NewWorkspace/User Access Management/src/Database.properties");

            readFile.load(fileStream);

            conn = GetDBCOnnection.getDbConnection();

            if (conn != null) {
                forResult = conn.prepareStatement(readFile.getProperty("db.selectQuery"));

                recordSet = forResult.executeQuery();

            }

            if (recordSet != null) {

                while (recordSet.next()) {

                    resultModel = new DataModel();
                    resultModel.setUserId(recordSet.getInt(1));
                    resultModel.setFirstName(recordSet.getString(2));
                    resultModel.setLastName(recordSet.getString(3));
                    resultModel.setEmailId(recordSet.getString(4));
                    resultModel.setUserRole(recordSet.getString(5));
                    resultModel.setUserPhone(recordSet.getLong(6));
                    resultModel.setUserStatus(recordSet.getString(7));
                    list.add(resultModel);
                }

                loginMap = getSession();

                loginMap.put("mylist", list);

                setSession(loginMap);

            }
        } catch (IOException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } finally {
            try {
                fileStream.close();
                conn.close();
                forResult.close();
                recordSet.close();
            } catch (SQLException | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        return "success";
    }

}

}

This is View User JSP {

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View User Page</title>
</head>
<body>
    <s:form action="createUser" method="post" id="options">
        <div id="outer div"
            style="width: 180px; display: inline-block; margin: 2px">
            <select
                style="width: 150px; vertical-align: top; height: 30px; margin: 1px"
                name="user_Status" id="status_List">
                <option selected>Active</option>
                <option>Suspended</option>
            </select>
        </div>
        <div style="display: inline-block; margin: 1px">
            <div>
                <s:submit
                    style="width: 150px; align: left; height: 30px; margin:1px"
                    value="Create User"></s:submit>
            </div>
        </div>
    </s:form>

    <s:form action="editUser" method="post">

        <div id="results" style="border: solid 1px #444; margin: 3px">

            <table>

                <tr>
                    <td>User ID</td>
                    <td>First Name</td>
                    <td>Last Name</td>
                    <td>Email Id</td>
                    <td>Role</td>
                    <td>Contact</td>
                    <td>Status</td>
                    <td>Action</td>

                </tr>
                <s:iterator value="list">
                    <tr>
                        <td><s:property value="userId"></s:property></td>
                        <td><s:property value="firstName"></s:property></td>
                        <td><s:property value="lastName"></s:property></td>
                        <td><s:property value="emailId"></s:property></td>
                        <td><s:property value="userRole"></s:property></td>
                        <td><s:property value="userPhone"></s:property></td>
                        <td><s:property value="userStatus"></s:property></td>
                        <td><s:url value="EditUserAction.action" var="url">
                                <s:param name="userId" value="userID" />
                            </s:url> <s:a href="%{url}">
                                <s:submit value="E" />
                            </s:a></td>

                    </tr>
                </s:iterator>
            </table>
        </div>

    </s:form>
</body>
</html>

}

This is ths snap of JSP. !(fileJSP SNAP:///home/ast-developer/Pictures/Screenshot%20from%202017-09-21%2010:20:33.png) As you can see the arraylist of objects values are printed using JSP.The JSP has a submit button against each user details which calls an action.I want to capture the userId of that particular user whose button was clicked to be passed to editAction class.How can I do that?

1
You send data from the client side with a form submission or an Ajax request (or, technically, something like WebSockets). Those are your basic options--what's the specific issue?Dave Newton
you can make ajax call and send data using query parameter like editAction.action?userId="TEST" then in action class you can do request.getParameter("userId"); to get the userId value which pass from front end .Anand Dwivedi
@DaveNewton I have added a snap of the JSP page.As you can see every user has a E button.This button has an action class where I will edit the details that I have stored in the ArrayList already in the loginAction class and displaying them on JSP too using same List.For edit purpose,I am using the same ArrayList in EditAction class by implementing SessionAware and putting that List in a map which is accessible to EditUser class.I need to search the ArrayList for a particular user based on its userId,which I need to pass from JSP page,which I am not able to do.Please Help?Himanshu Chaudhary

1 Answers

1
votes
<html>
<body>
<script>
    function abc()
    {
        var xhttp=new XMLHttpRequest();
        xhttp.onreadystatechange=function(){
        if(this.status=200 && this.readyState==4)
        {
            document.getElementById("divi").innerHTML=this.responseText;
        }   
    };
    xhttp.open('GET','abc.txt',true);
    xhttp.send();
}
</script>
<div id="divi">
    <h1>AJAX CHANGE MAGIC</h1>
    <input type="button" value="click me for magic" onclick="abc()"/>
</div>

</body>
</html>