Struts 2 table dynamic details to the background.

Keywords: Java Struts JSP xml

Recently, we trained new people and finally practiced using Struts 2 framework, but we met the problem that the background of the screen List object can not be transmitted. Looked on the Internet for a circle and did not find the corresponding solution. Finally, I found one, which I can read after writing down.

Note: There are tens of millions of methods, there are other solutions, or I wrote something wrong, welcome to correct!

In the table above, I want to upload the content to the background, because the update button and delete button below want more than one operation, so the most painful way is to submit the whole table. (Of course, there are also through js to spell the selected object into strings and then pass to the background segmentation, but too low.)

Solution:

Two Java beans: ResultBean and ResultDetailBean (ResultDetailBean nested in ResultBean)

ResultBean code:

package Bean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ResultBean {
    private String stuId = "";
    private String stuName = "";
    private String classes = "";
    private BigDecimal grade;
    private String sex = "";
    private String detailSize = "";
    //ResultDetailBean Of get set Method
    private List<ResultDetailBean> detailList = new ArrayList<ResultDetailBean>();

    /**
     * @return the stuId
     */
    public String getStuId() {
        return stuId;
    }

    /**
     * @param stuId
     *            the stuId to set
     */
    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    /**
     * @return the stuName
     */
    public String getStuName() {
        return stuName;
    }

    /**
     * @param stuName
     *            the stuName to set
     */
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    /**
     * @return the classes
     */
    public String getClasses() {
        return classes;
    }

    /**
     * @param classes
     *            the classes to set
     */
    public void setClasses(String classes) {
        this.classes = classes;
    }

    /**
     * @return the grade
     */
    public BigDecimal getGrade() {
        return grade;
    }

    /**
     * @param grade
     *            the grade to set
     */
    public void setGrade(BigDecimal grade) {
        this.grade = grade;
    }

    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * @param sex
     *            the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    public List<ResultDetailBean> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<ResultDetailBean> detailList) {
        this.detailList = detailList;
    }

    public String getDetailSize() {
        return detailSize;
    }

    public void setDetailSize(String detailSize) {
        this.detailSize = detailSize;
    }
}

ResultDetailBean code:

package Bean;

public class ResultDetailBean {
    private String no = "";
    private String sel = "0";
    private String curId = "";
    private String curName = "";
    private String score = "";

    /**
     * @return the no
     */
    public String getNo() {
        return no;
    }

    /**
     * @param no
     *            the no to set
     */
    public void setNo(String no) {
        this.no = no;
    }

    /**
     * @return the sel
     */
    public String getSel() {
        return sel;
    }

    /**
     * @param sel
     *            the sel to set
     */
    public void setSel(String sel) {
        this.sel = sel;
    }

    /**
     * @return the curId
     */
    public String getCurId() {
        return curId;
    }

    /**
     * @param curId
     *            the curId to set
     */
    public void setCurId(String curId) {
        this.curId = curId;
    }

    /**
     * @return the curName
     */
    public String getCurName() {
        return curName;
    }

    /**
     * @param curName
     *            the curName to set
     */
    public void setCurName(String curName) {
        this.curName = curName;
    }

    /**
     * @return the score
     */
    public String getScore() {
        return score;
    }

    /**
     * @param score
     *            the score to set
     */
    public void setScore(String score) {
        this.score = score;
    }
}

JavaBean has been defined, and the next step is to bind to Action. I use model driver to bind by implementing the Model Driven interface.

Code:

package Action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import Bean.ResultBean;
import Bean.ResultDetailBean;

public class DeleteAction extends ActionSupport implements ModelDriven<ResultBean> {
    private ResultBean resultB = new ResultBean();

    /*
     * (non-Javadoc)
     * 
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public String execute() throws Exception {
System.out.println("No.     Sel    Curriculum Id");
for (ResultDetailBean detail : resultB.getDetailList()) { System.out.println(detail.getNo() + "    " + detail.getSel() + "    " + detail.getCurId()); } return SUCCESS; } @Override public ResultBean getModel() { return resultB; } }

You can see that after this binding, we can use the instantiated object directly in the execute method.

Definitions in struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="keyInAction" class="Action.KeyInAction">
            <result name="success" type="redirectAction">/resultAction</result>
            <result name="error">/Key-In.jsp</result>
        </action>
        <action name="resultAction" class="Action.ResultAction">
            <result name="success">/Result.jsp</result>
        </action>
        <action name="deleteAction" class="Action.DeleteAction">
            <result name="success">/Result.jsp</result>
        </action>
    </package>
</struts>

Next comes the code for the jsp page:

 

Result.jsp: <%@ page language="java" contentType="text/html; charset=ISO-8859    pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
table.PCxxxx_table2 {
    border-top: 1px solid #696969;
    border-left: 1px solid #696969;
    border-collapse: collapse;
    border-spacing: 0;
    background-color: #ffffff;
}

.PCxxxx_table2 tr {
    
}

.PCxxxx_table2 th {
    color: #000000;
    background-color: #C0C0C0;
    padding: 0px 1px;
    text-align: center;
    border: 1px solid #696969;
}

.PCxxxx_table2 td {
    height: 26px;
    padding: 0px 1px;
    border: 1px solid #696969;
    vertical-align: middle;
}
</style>
<script type="text/javascript">
    function deleteButton() {
// Point to the deleteAction edited above document.resultAction.action
= "deleteAction";
// Submission document.resultAction.submit(); }
function setCheckbox() { var detailSize = document.getElementById("detailSize").value; for (var i = 0; i < detailSize; i++) { if(document.getElementById("sel" + i).value == "1"){ document.getElementById("chk"+i).checked = "checked"; } } } function set(index) { if(document.getElementById("chk"+index).checked == "checked"){ document.getElementById("chk"+index).checked = ""; document.getElementById("sel"+index).value=""; }else{ document.getElementById("chk"+index).checked = "checked"; document.getElementById("sel"+index).value="1"; } } </script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body onload="setCheckbox();"> <form name="resultAction" action="resultAction" method="post"> <input type="hidden" name="detailSize" value="${detailSize}" id="detailSize"> <table> <tr> <td> <table> <tr> <td colspan="3"><span>Students Score MainTenance Result</span></td> </tr> <tr> <td colspan="3"><hr></td> </tr> <tr> <td width="100px">Student Id</td> <td align="left" width="50px"><s:text name="stuId" /></td> <s:hidden name="stuId" value="%{stuId}"></s:hidden> <td width="100px">Student Name</td> <td align="left" width="50px"><s:text name="stuName" /></td> <s:hidden name="stuName" value="%{stuName}"></s:hidden> <td width="100px"></td> <td align="left" width="50px"></td> </tr> <tr> <td width="100px">Class</td> <td align="left" width="50px"><s:text name="classes" /></td> <s:hidden name="classes" value="%{classes}"></s:hidden> <td width="100px">Grade</td> <td align="left" width="50px"><s:text name="grade" /></td> <s:hidden name="grade" value="%{grade}"></s:hidden> <td width="100px">Sex</td> <td align="left" width="50px"><s:text name="sex" /></td> <s:hidden name="sex" value="%{sex}"></s:hidden> </tr> <tr> <td colspan="3"><hr></td> </tr> </table> </td> </tr> <tr> <td>
// The beginning of the list table
<table border="1" class="PCxxxx_table2"> <tr> <th width="30px">No.</th> <th width="30px">Sel</th> <th width="150px">Curriculum Id</th> <th width="150px">Curriculum Name</th> <th width="30px">Score</th> </tr> <s:iterator value="detailList" status="status"> <tr> <td><s:property value="no"></s:property></td>
//The examples of text and CheckBox need only be written in this way, but the information found in the radio box type= "radio" project can not be submitted to the background at present. My suggestion is to create a new field "state" to save whether the radio box is selected. Of course, the "state" field needs type= "hidden". Tags hide saved values. <td><input type="checkbox" name="sel${status.index}" id="chk${status.index}" onclick="set(${status.index});" /></td> <td><input type="text" name="detailList[${status.index}].curId" value="${curId}" /></td> <td><s:property value="curName"></s:property></td> <td><s:property value="score"></s:property></td> </tr> </s:iterator>
//It is important to note that hidden items need to be added to the fields used to transfer paper to the background screen, otherwise the pages will not save the fields used for pure display.
<s:iterator value="detailList" status="status"> <input type="hidden" value="<s:property value="no"/>" name="detailList[${status.index}].no" /> <input type="hidden" value="<s:property value="sel"/>" name="detailList[${status.index}].sel" id="sel${status.index}" /> <input type="hidden" value="<s:property value="curName"/>" name="detailList[${status.index}].curName" /> <input type="hidden" value="<s:property value="score"/>" name="detailList[${status.index}].score" /> </s:iterator> </table> </td> </tr> <tr> <td> <table> <tr> <td><input type="button" onclick="" value="Update(U)"></td> <td><input type="button" onclick="deleteButton();"//This side only implements the action and submission specified in deleteButton() after the delete button is pressed. value="Delete(D)"></td> <td><input type="button" onclick="history.back();" value="Back(B)"></td> </tr> </table> </td> </tr> </table> </form> </body> </html>

Attached final results

The background display effect after the Delete button is pressed:

 

Result: The background can get the value of "No", check box "Sel" and text box "Curriculum Id".

The above solution to the dynamic size of Struts 2 table List items like background value transfer has been described. This is more than one way.

Posted by jfgreco915 on Tue, 27 Aug 2019 07:44:57 -0700