最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Spring 3 - Including javascript through a JSP view resolver? - Stack Overflow

programmeradmin5浏览0评论

I'm trying to localize my application, and it would be nice if I could simply send all JS files through a JSP resolver to get access to localization bundles.

Right now, I just have this:

<bean id="viewResolver" class=
        "org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

and I was wondering if there was an easy way to have both .js and .jsp resolve through the InternalResourceViewResolver without adding in some pattern matching hackery.

I'm trying to localize my application, and it would be nice if I could simply send all JS files through a JSP resolver to get access to localization bundles.

Right now, I just have this:

<bean id="viewResolver" class=
        "org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

and I was wondering if there was an easy way to have both .js and .jsp resolve through the InternalResourceViewResolver without adding in some pattern matching hackery.

Share Improve this question asked Apr 5, 2011 at 21:23 Stefan KendallStefan Kendall 67.9k69 gold badges258 silver badges409 bronze badges 1
  • What do you want to do: have a js file for each language (like property files), or put some language dependen placeholder in a (one) js file? – Ralph Commented Apr 15, 2011 at 13:22
Add a ment  | 

2 Answers 2

Reset to default 8 +50

You don't actually need your .js files to be stored as .js, as long as their content-type is text/javascript. But having dynamic information in your .js files is wrong:

  • you cannot cache them properly
  • you might be tempted to add jsp logic in the .js file, which will be hard to maintain
  • you cannot use contend-delivery networks (if needed)
  • (and perhaps there are more downsides to that, which I can't think of right now)

Instead, you should initialize some settings object from a jsp page that is using the .js file. See this answer for more details.

Here is a concrete (simplified) example from my code. This snippet is in the .jsp:

<script type="text/javascript">
var config = {
    root : "${root}",
    language: "${user.language.code}",
    currentUsername: "${user.username}",
    messages : {
        reply : "${msg.reply}",
        delete : "${msg.delete}",
        loading : "${msg.loading}",
    }
};
init(config);
</script>

The init(config) is in the .js file, and is just setting the config object as a global variable. (I actually have some default values, but that doesn't matter)

Put all your javascripts under webapp/scripts. Then programmatically add this implementation of the addResourceHandlers() method to your WebConfig.xml:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "package.base.your")
public class WebConfig extends WebMvcConfigurerAdapter {

//your other WebMvcConfigurerAdapter class implementations here

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  //other handlers here
  registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/**");
}
发布评论

评论列表(0)

  1. 暂无评论