简体   繁体   中英

How to refresh the content of a JSP?

I have a jsp page , on a button click I want to fetch values from bean and do some action on the basis of that data.

(This button is not posting or getting anything but only makes some UI level changes.)

Conditions: Page must not be refreshed , only a certain part of page is refreshed and that too only when that button is pressed.

I have

 <wcbase:useBean id="beanUtil" classname="UtilBean">
        <c:set property="studentId" value="${WCParam.studentId}" target="${beanUtil}" />
        </wcbase:useBean>
            <c:set property="classId" value="${class.uniqueID}" target="${beanUtil}" />
            <c:set var="listOfStudents" value="${beanUtil.StudentIdsforTransfer}" />

            <c:if test="${not empty listOfStudents}">
                <script>var listOfStudents= true; </script>
            </c:if>
            <c:if test="${empty listOfStudents}">
                <script>var listOfStudents = false;</script>
                </c:if>

I need to call this whole section on a button click

how to do it?

All the help would really be appreciated.

Note: I have tried to put this code in div and then tried to refresh that div on button click but it didnt work.

something like :

Put in js

  $('#studentDiv').load(document.URL +  '#studentDiv');

Put in jsp.

 <div id="studentDiv">

 <wcbase:useBean id="beanUtil" classname="UtilBean">
        <c:set property="studentId" value="${WCParam.studentId}" target="${beanUtil}" />
        </wcbase:useBean>
            <c:set property="classId" value="${class.uniqueID}" target="${beanUtil}" />
            <c:set var="listOfStudents" value="${beanUtil.StudentIdsforTransfer}" />

            <c:if test="${not empty listOfStudents}">
                <script>var listOfStudents= true; </script>
            </c:if>
            <c:if test="${empty listOfStudents}">
                <script>var listOfStudents = false;</script>
                </c:if>
</div>

This code refreshed the page but inserted the whole page again with in the main page and stayed in process.

You can refresh the JSP page with the help of the following statement

TimeUnit.SECONDS.sleep(5); 

It reloads the page after 5 seconds..You can change it according to your need

I have used the above line in my code as follows..have a look..it might help you

  <form action="haryana.jsp" method="get" class="values">
            <table cellspacing="4" cellpadding="4">
                <tr>
                    <td>Select Industry</td>
                    <td><select name="sta" class="dropdown">
                            <option>Industry</option>
                            <option>Cotton</option>
                            <option>Sugar</option>
                            </select></td>
                </tr>
                <tr>
                    <td>Select Category </td>
                    <td><select name="ind" class="dropdown">
                            <option>Category</option>
                            <option>Area</option>
                            <option>Production</option>
                            <option>Yield</option>
                        </select></td>
                </tr>
                <tr class="blank_row">

                </tr>
                <tr>
                    <td><input type="submit" value="Submit" style="margin-top: 10px;" name="bt" class="btn btn-primary btn1"/></td>
                </tr>
            </table>
            <%
                if (request.getParameter("bt") != null) 
                {
                    categoryDataset = new DefaultCategoryDataset();
                    try 
                    {
                        st=request.getParameter("sta").toLowerCase();
                        indus = request.getParameter("ind").toLowerCase();
                        String qry = "select year,"+indus+" from "+st+" where year between '2007-08' and '2013-14'  and state='Haryana'";
                        utility ut = new utility();
                        ResultSet rs = ut.DQL(qry);
                        while (rs.next()) 
                        {
                            a=Float.parseFloat(rs.getString(""+indus));
                            b=Math.round(a);
                            categoryDataset.setValue(b, "" + st, "" + rs.getString("year"));
                            data1.setValue("" + rs.getString("year"), b);
                        }
                        JFreeChart chart1 = ChartFactory.createPieChart(""+indus,   data1, true, true, false);
final ChartRenderingInfo info1 = new ChartRenderingInfo(new StandardEntityCollection());
                        final File file2 = new File("F:/JavaAptech/Netbeans 74 applications/MinorProj/web/" + "har1" + st + indus + ".png");
                        img1 = "har1" + st + indus + ".png";
                        ChartUtilities.saveChartAsPNG(file2, chart1, 450, 300, info1);
                        chart1.setBorderVisible(true);
                    } 
                    catch (Exception e) 
                    {
                       // out.println(e);
                        %>
                        <script>
                            alert("Invalid Input/Data Unavailable");
                        </script>
                        <%
                    }%>
        </form>
    </div>
  <div style="float:left;width:70%;height:380px;">
     <%TimeUnit.SECONDS.sleep(5); %>
     <img style="margin-left: 15px;margin-top:3px;width:450px;height: 350px;"  src=     <%=img1%> USEMAP="#chart1"/>

</div>
    <%
         }
    %>

My suggestion is call ajax where your content to be refreshed. In "test.html" write the code to get values from your bean. Sample code

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

I give reference

Calling a backingBean method from javascript

how to call backing bean method from java script

How to call managed bean methods with parameters via JavaScript

Calling Managed Bean Method From JavaScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM