我在 SpringMVC 中有一个基本的应用程序。我所有的控制器都扩展了一个超类,如下所示。这里的问题是每次触摸控制器方法时都不会重置cssFiles
and 。jsFiles
所以我最终会为每个页面视图content/view.js
加载时间。x+1
如果我已加载页面 3 次,它将包含 4xcontent/view.js
文件。
我看到每次加载页面时都会附加这些值。为什么会发生这种情况,我该如何解决?
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");
}
}
我仍在努力确定这个问题的原因....有什么想法吗?
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>