解决spring boot集成jsp时EL表达式获取全局变量值为空的问题

本文介绍如何在JSP页面中通过EL表达式访问properties文件定义的属性。提供了XML配置与Java配置两种方法,并展示了如何将属性值注入视图解析器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}/"/>即可获取值

如果谁有更简便的方式请告知谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值