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>
I think the way you are adding the js and css files to your views are wrong.
You should add the resource handlers for the css and js files in the the servlet xml configuration file. I use the following code for the same.
This will skip the Spring's Dispathcer Servlet for the all the resources in the resources folder of your web application root.
After doing this you just needs to include them in your jsp files wherever you need them.
Cheeers...
Try to use Set instead of
ArrayList
.Maybe you want to do
in the constructor if the parent controller is always alive?
How are you invoking the parent constructor's methods?
try to use
Set
instead ofArrayList
.Although I didn't test this code, the obvious thing what I'd try is :
Can you ellaborate what you mean by reset in
are not reset every time a controller method is touched
I've misunderstood your question at first here is revised version :
If you need to override some javascript then put the edit.jsp specific js after the
html
tag. So your edit file would look like this :