jeudi 22 septembre 2016

How to get selected row objectsusing Displaytag, (TableDecorator), Checkboxes and struts2

I am making a Struts2 project which display student information using displaytag table with TableDecorator. The table has the pagination and checkboxes. When 'submit' button is clicked, all the selected rows object must return to Action class.

The problem is "all selected checkboxes' status are not passing through the pages and only the current page of the checkboxes status can only passed to action ".

So, I used displaytag's decorator as the demo of the displaytag checkboxes guides. But, Here is another problem occourred. The first page result of the pagination can display and other pages can't display. I used as they said, like "used <form name="disl"> and <display:table form="disl">". Below is my codes.

Thanks in Advance

displaytag.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="display" uri="http://ift.tt/JXB0VM"%>
<%@ taglib prefix="c" uri="http://ift.tt/QfKAz6"%>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Students Details</title>
<style type="text/css">
.odd {
    background-color: silver;
}

span {
    margin-top: 15px;
    margin-left: 60px;
    margin-bottom: 15px;
    color: maroon;
}
/* for hiding the page banner */
.pagebanner {
    display: none;
}
/* for coustamizing page links */
.pagelinks {
    color: maroon;
    margin-left: 60px;
    margin-top: 50px;
}

/* for shifting all the Export options*/
.exportlinks {
    margin-top: 5px;
    margin-left: 50px;
}
/* For changing the spaces between export link */
.export {
    margin-left: 30px;
}
</style>

<html>
<body>

    <s:form name="disl" theme="simple" method="post" action="checked">
        <display:table form="disl" name="students" id="table" pagesize="10" decorator="com.simplecode.form.DecorateurCheckbox" 
      excludedParams="_chk" requestURI="" > 
            <display:column property="checkBox" title="checkbox" ></display:column>

            <display:column property="id" title="ID" ></display:column>
            <display:column property="rollNo" title="Roll No" sortable="false"></display:column>
            <display:column property="studentName" title="Student Name"
                sortable="false"></display:column>
            <display:column property="department" title="Department"
                sortable="false"></display:column>
            <display:column property="percentage" title="Percentage"
                sortable="false"></display:column>
        </display:table>
        <s:submit value="Check" />
    </s:form>


</body>
</html>

DecorateurCheckbox.java

package com.simplecode.form;


import org.displaytag.decorator.CheckboxTableDecorator;

public class DecorateurCheckbox extends CheckboxTableDecorator{
    public String getCheckBox()
       {
        String retour = "";
        StudentBean user= (StudentBean) getCurrentRowObject();

        int idUser ;

        idUser = user.getId();
        System.out.println(user.getId());

       retour = "<input type='checkbox' name='id' value='%{checked}' id='" + idUser + "' />";

        return  retour;
       }
}

StudentAction.java

package com.simplecode.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.simplecode.form.StudentBean;

public class StudentAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private List<StudentBean> students;

    private String[] rowId;


    public String getSelected() {

        return SUCCESS;
    }

    public String fetchStudentList() {
        students = new ArrayList<StudentBean>();
        students.add(new StudentBean(1, "11", "Ajay", "ECE", 94));
        students.add(new StudentBean(2, "21", "Mohiadeen", "CSE", 89));
        students.add(new StudentBean(3,"31", "Sriram Ganesh", "CSE", 87));
        students.add(new StudentBean(4,"41", "Jaya Prakash", "ECE", 86));
        students.add(new StudentBean(5,"51", "Sundar", "MECH", 85));
        students.add(new StudentBean(6,"61", "Ibrahim Sha", "ECE", 85));
        students.add(new StudentBean(7,"71", "Ranjith", "MECH", 84));
        students.add(new StudentBean(8,"81", "Aswin", "ECE", 84));
        students.add(new StudentBean(9,"91", "Sharmila", "IT", 83));
        students.add(new StudentBean(10,"101", "Nilafar", "IT", 82));
        students.add(new StudentBean(11,"111", "Nisha", "CSC", 81));
        students.add(new StudentBean(12,"121", "Guru Prasad", "ECE", 80));
        students.add(new StudentBean(13,"131", "Gowtham Raj", "CSE", 76));
        students.add(new StudentBean(14,"141", "Dinesh Babu", "MECH", 71));
        students.add(new StudentBean(15, "151", "Lavanya", "IT", 70));

        return SUCCESS;
    }

    public List<StudentBean> getStudents() {
        return students;
    }

    public void setStudents(List<StudentBean> students) {
        this.students = students;
    }


    public String[] getRowId() {
        return rowId;
    }

    public void setRowId(String[] rowId) {
        this.rowId = rowId;
    }
}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://ift.tt/1iwXH3l">
<struts>

    <package name="default" extends="struts-default">
        <action name="studentList" class="com.simplecode.action.StudentAction"
            method="fetchStudentList">
            <result name="success">/displaytag.jsp</result>
        </action>

        <action name="checked" class="com.simplecode.action.StudentAction"
            method="getSelected">
            <result name="success">/welcome.jsp</result>
            <result name="input" type="redirect">studentList.action</result>
        </action>
    </package>
</struts>




Aucun commentaire:

Enregistrer un commentaire