如何将数据从JSP页面发送到Action类?

时间:2017-09-20 14:02:28

标签: jsp struts2

我有两个操作类,即loginAction和EditUserAction类.Login操作类包含一个在JSP页面中迭代的对象的arraylist,即View User Page。

这是我的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";
    }

}

}

这是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>

}

这是JSP的快照。 !(文件JSP SNAP:///家用/ AST-开发商/图片/截图%20from%202017-09-21%2010:20:33.png) 正如您所看到的,使用JSP打印对象的arraylist。JSP有一个提交按钮,针对每个用户详细信息调用一个动作。我想捕获该按钮被单击以传递给editAction类的特定用户的userId。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

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