SpringMVC中MessageSource的获取方法

SpringMVC中MessageSource的获取方法

有时候需要在controller中获取message信息,有三种方法可以获得MessageSource。

        // 资源文件ApplicationResources.properties放在src目录下,也就是classes目录
        // 配置文件messages.xml也放在src目录下,也就是classes目录
        // 第一种方法
        MessageSource resources = new ClassPathXmlApplicationContext("messages.xml");
        String msg = resources.getMessage("XXXXX", new String[] { "OOOOO" }, null);

        // 第二种方法
        ReloadableResourceBundleMessageSource messageSource;
        messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:/ApplicationResources");
        messageSource.setUseCodeAsDefaultMessage(true);
        msg = messageSource.getMessage("XXXXX", new String[] { "OOOOO" }, Locale.CHINA);

        // 第三种方法
        WebApplicationContext wac = RequestContextUtils.getWebApplicationContext(request);
        msg = wac.getMessage("XXXXX", new String[] { "OOOOO" }, Locale.CHINA);

messages.xml文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>ApplicationResources</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="false" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="cacheSeconds" value="60" />
    </bean>

</beans>
PS:第三种方法messageSource Bean是配置在spring-servlet.xml中的。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC获取MySQL的图片可以通过以下步骤完成: 1. 在MySQL数据库创建一个表来存储图片,可以使用BLOB类型来存储二进制数据。 2. 在Spring MVC创建一个Controller用于获取图片数据。 3. 在Controller使用JDBC或MyBatis等框架从MySQL数据库读取图片数据。 4. 将读取到的图片数据以字节数组的形式返回给前端。 5. 在前端使用<img>标签来显示图片,将图片的src属性设置为返回的字节数组的Base64编码。 下面是一个简单的示例代码: ```java @Controller public class ImageController { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping(value = "/image/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public ResponseEntity<byte[]> getImage(@PathVariable("id") int id) throws IOException { byte[] imageBytes = jdbcTemplate.queryForObject("SELECT image_data FROM images WHERE id = ?", new Object[]{id}, byte[].class); return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(imageBytes); } } ``` 在上面的代码,我们定义了一个`getImage`方法用于获取图片数据。方法的参数`id`表示要获取的图片的ID。方法的返回值是一个`ResponseEntity<byte[]>`对象,其包含了图片的字节数组和图片的MIME类型。在方法,我们使用JdbcTemplate从MySQL数据库查询图片数据,并将查询结果直接返回给前端。在前端,我们可以使用以下HTML代码来显示图片: ```html <img src="/image/1"> ``` 其,`/image/1`表示要获取ID为1的图片。当浏览器加载这个图片时,会向服务器发送一个HTTP请求,服务器会返回图片的字节数组,浏览器会将这个字节数组解码并显示图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值