Abhishek Choudhary
Abhishek Choudhary

Reputation: 8385

Avoid script file reloading JSP in Spring

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.

Upvotes: 0

Views: 329

Answers (1)

vdenotaris
vdenotaris

Reputation: 13637

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:

Upvotes: 2

Related Questions