properties中定义的属性怎样在jsp中使用EL表达式进行访问呢?
两种办法,核心思想都是在初始化视图解析器时向内写入attribute。首先说xml配置方式的
在配置视图时
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/web/" />
<property name="suffix" value=".jsp" />
<property name="attributes">
<props>
<prop key="imgServer">${imgServer}</prop>
</props>
</property>
</bean>
其中
${imgServer}
可从配置文件获取
javaconfig的方式
首先定义
public class WebConfig {
public WebConfig(InternalResourceViewResolver resolver) {
Resource resource = new ClassPathResource("server.properties");
Properties props = null;
try {
props = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
resolver.setAttributes(props);
}
}
在main主类中进行注入
@Autowired
InternalResourceViewResolver resolver;
@Bean
public WebConfig webConfig(){
return new WebConfig(resolver);
}
jsp中<img src="${imgServer}/"/>即可获取值
如果谁有更简便的方式请告知谢谢