I have a basic app in SpringMVC. All of my controllers extend a super class shown below.
The problem here is that the cssFiles
and jsFiles
are not reset every time a controller method is touched. So I end up with content/view.js
being loaded x+1
times for every page view. If I've loaded the page 3 times, it'll contain 4x content/view.js
files.
I'm seeing these values be appended to each time the page is loaded. Why is this happening and how do I fix it?
public class Controller {
private List<String> cssFiles = new ArrayList<String>();
private List<String> jsFiles = new ArrayList<String>();
public Controller () {
this.addCss("global");
this.addJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min");
this.includejQueryUI();
this.addJs("global");
}
public ModelAndView prepareModel (ModelAndView model) {
model.addObject("cssFiles", cssFiles);
model.addObject("jsFiles", jsFiles);
return model;
}
public ModelAndView prepareModel (ModelAndView model, String title) {
model.addObject("title", title);
return prepareModel(model);
}
/*
* Add a css file to the page
*/
public void addCss (String cssPath) {
if (cssPath.indexOf("://") < 1) {
cssPath = "/cmt/css/"+cssPath;
}
cssFiles.add(cssFiles.size(), cssPath);
}
/*
* Add a javascript file to the page
*/
public void addJs (String jsPath) {
if (jsPath.indexOf("://") < 1) {
jsPath = "/cmt/js/"+jsPath;
}
jsFiles.add(jsFiles.size(), jsPath);
}
/**
* Add a Rich Text Editor (TinyMCE) library to the page
*/
public void includeRichTextEditor() {
addJs("../lib/tiny_mce-3.5b3/tiny_mce");
}
/**
* Add the jQuery UI library to the page
*/
public void includejQueryUI() {
addCss("../lib/jquery-ui-1.8.19/custom-theme/jquery-ui-1.8.19.custom");
addJs("../lib/jquery-ui-1.8.19/jquery-ui-1.8.19.custom.min");
}
}
I'm still struggling to determine the cause of this issue.... any ideas?
Part of web.xml
<!-- Standard spring configuration -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- Spring Web MVC dispatcher servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.json</url-pattern>
</servlet-mapping>