0

I am reloading a JSP page in every 10 seconds and I realised that everytime it reloads the script files and that increase the size and finally my page crashes.

This is my jsp and script to reload-

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<%
//JSP code
long ts = (new Date()).getTime(); //Used to prevent JS/CSS caching
%>
<title>Login Success</title>
<link href="assets/css/bootstrap-united.css?<%=ts %>" rel="stylesheet" />
<link href="assets/css/customtable.css?<%=ts %>" rel="stylesheet" />

    <script src="bootstrap/js/jquery-1.11.1.min.js?<%=ts %>"></script>  
    <script src="bootstrap/js/bootstrap.js?<%=ts %>">
    </script>

</head>
<body>
<div id="buntha">


<div class="navbar navbar-default">

    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse"
            data-target=".navbar-responsive-collapse">
            <span class="icon-bar"></span> <span class="icon-bar"></span> <span
                class="icon-bar"></span>
        </button>
    </div>

    <div class="navbar-collapse collapse navbar-responsive-collapse">
        <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search">
        </form>
        <ul class="nav navbar-nav navbar-right">
            <li><a href=".index.html">Home</a></li>
            <li><a href="signup.html">Signup</a></li>
            <li class="active"><a href="login.html">Login</a></li>
            <li class="dropdown"><a href="#" class="dropdown-toggle"
                data-toggle="dropdown">Explore<b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#">Contact us</a></li>
                    <li class="divider"></li>
                    <li><a href="#">Further Actions</a></li>
                </ul></li>
        </ul>
    </div>
    <!-- /.nav-collapse -->
</div>

And My script reload-

<script type="text/javascript">
    var url = '/Motion/success.html'

    $(document).ready(function() {      
        $.ajaxSetup({ cache: false }); 
        var username = '';
        var password = '';                  
        var cubename = '';  
        var status = 'load';

    setInterval(function() {
                $("#buntha").load('/Motion/success.html',
                    {username:'<%=userName%>',password:'<%=Password%>',status:status})
                    .fadeIn("slow");
                }, 10000);

    });

</script> 

and my Controller Post method-

changed the controller to return JSON

@RequestMapping(value="/success", method=RequestMethod.POST,headers="Accept=*/*")
public Model success(@Valid @ModelAttribute("username") String username,@Valid @ModelAttribute("password") String password,@Valid String cubename,@Valid String status, BindingResult result,Model model) {     
    log.info("Posting Data in Success With "+username+" passowrd "+password+" Cubename "+cubename+" status "+status);

    boolean found = studentService.findByLogin(username, password);
    if (found) {        
        if(cubename != null)
            updateDataBaseContent(username,cubename,status);

        StudentLogin studentLogin = new StudentLogin(); 
        studentLogin.setUserName(username);
        studentLogin.setPassword(password);
        List<Cube> cubes = cubeService.getCubes();
        model.addAttribute("name", studentLogin);
        model.addAttribute("message", cubes);       
    } 
    return model;
}

How can I avoid reloading all script files I am anyway calling only the div tag to load, but why its loading the netire page. I am very new to Spring and Web, so it may be a silly query but I couldn't crack it yet.

1 Answer 1

2

By watching your code, I suppose that the controller calls the entire success.jsp view resource (that contains the scripts).

model1.setViewName("success");

A good solution could be to setup your controller just as response producer, by using for instance JSON as MediaType and parsing that response through JQuery in order to manage AJAX requests/reponses.

Sequence

Usefull references:

Sign up to request clarification or add additional context in comments.

2 Comments

I did change the return type as JSOn but still its loading the entire page with script ... above I updated the controller..but I do believe you gave the right approach , may be I'll figure out what still left in my code
You should't use Spring Model even now, 'cause their instances are used to render dynamic content inside a web resource (a page) through a Template Engine (in you case, the JSP engine). I suggest you to use a RESTful Web Service as shown here: spring.io/guides/gs/rest-service. To be clear, you have to send only a data stream, in that case a JSON file, and render its content on client-side by using JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.