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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我为你提供一个简单的Spring Boot集成JSP的示例项目,你可以参考一下: 1. 创建一个Maven项目,pom.xml文件中添加以下依赖: ```xml <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat Embed Jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> ``` 2. 在src/main目录下创建一个webapp目录,然后在webapp目录下创建一个jsp目录,用于存放JSP文件。 3. 在src/main/java目录下创建一个Spring Boot的配置类,例如: ```java @Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 在src/main/java目录下创建一个Controller类,例如: ```java @Controller public class HomeController { @RequestMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, World!"); return "home"; } } ``` 5. 在src/main/resources目录下创建一个application.properties文件,用于配置应用程序的属性,例如: ``` spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp ``` 6. 运行应用程序,访问http://localhost:8080/,会显示Hello, World!。 以上就是一个简单的Spring Boot集成JSP的示例项目,你可以在此基础上进行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值