当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC国际化与文件上传(五)

SpringMVC国际化与文件上传(五)

2018年09月28日  | 移动技术网IT编程  | 我要评论
点击阅读上一章 其实SpringMVC中的页面国际化与上一章的验证国际化基本一致。 1.对页面进行国际化 1)首先我们对Spring配置文件中添加国际化bean配置 2)然后添加我们的资源文件,这个资源文件的前缀要与我们上面配置的bean的value相同。 我们在资源文件写入我们需要国际化的内容 然 ...

点击阅读上一章

其实springmvc中的页面国际化与上一章的验证国际化基本一致。

1.对页面进行国际化

1)首先我们对spring配置文件中添加国际化bean配置

<!-- 注册国际化信息,必须有id,指定资源文件名称,资源文件在src目录下 -->
    <bean id="messagesource"
        class="org.springframework.context.support.resourcebundlemessagesource">
        <property name="basename" value="i18n"></property>
    </bean>
    <!-- 配置localresolver用来获取本地化语言 -->
    <bean id="localeresolver"
        class="org.springframework.web.servlet.i18n.sessionlocaleresolver"></bean>
    <!-- 配置localechanceinterceptor拦截器 -->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.localechangeinterceptor" />
    </mvc:interceptors>

2)然后添加我们的资源文件,这个资源文件的前缀要与我们上面配置的bean的value相同。

我们在资源文件写入我们需要国际化的内容

然后我们在跳转页面的时候需要加载国际化资源

@requestmapping(value="login",method=requestmethod.get)
    public string login(locale locale,map<string ,object> map){
        string name = messagesource.getmessage("name", null, locale);
        string pass = messagesource.getmessage("pass", null, locale);
        string title = messagesource.getmessage("title", null, locale);
        string submit = messagesource.getmessage("submit", null, locale);
        map.put("title", title);
        map.put("pass", pass);
        map.put("name", name);
        map.put("submit", submit);
        map.put("user", new user());
        return "login";
    }

我们在页面中可以使用jstl或者spring标签数据国际化信息,spring中的form标签是用来输入spring验证错误之后的提示信息(上章有提过)

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

然后我们页面中的国际化信息可以可这显示出来

<form:form action="login" method="post" commandname="user">
        <fmt:message key="name"/>
        <form:input path="username"/>
        <form:errors path="username"/>
        <br>
        <fmt:message key="pass"/>
        <form:input path="userpass"/>
        <input type="submit" value="<spring:message code="submit"/>">
 </form:form>

我们还可以完成一个语言切换功能,我们在页面上加入两个超链接,当接收到请求,springmvc会在上下文中查找一个本地解析器,找到后使用它获取请求所对应的本地化类型信息,springmvc还允许还允许装配一个动态更改本地化类型的拦截器,这样通过指定一个请求参数就可以控制单个请求的本地化类型。

<a href="login?locale=zh_cn">中文</a>
<a href="login?locale=en_us">英文</a>

springmvc接到请求后首先会判断有没有这个参数,如果有这个参数就添加到session里面,如果没这个参数会到session里面去找,session里面没有找到会默认读取浏览器的语言。

2.文件上传

springmvc的文件上传非常的简单,它直接提供了直接的支持,这种支持是通过即插即用的multipartresolver接口实现的。spring用它的实现类commonsmultipartresolver来实现。springmvc上下文中没有自动装配所以需要我们手动来配置。我们这里就直接实现一个多文件上传,多文件上传会了害怕单文件上传不会?

配置之前我们首先来导入文件上传的jar包,这些包在我们spring中是没有的。

 

然后我们手动配置下bean

<!-- 配置文件上传 -->
    <bean id="multipartresolver"
        class="org.springframework.web.multipart.commons.commonsmultipartresolver">
        <!-- 指定默认的编码格式 -->
        <property name="defaultencoding" value="utf-8" />
        <!-- 指定允许上传的文件大小,单位byte -->
        <property name="maxuploadsize" value="512000" />
    </bean>

这个bean里还可以设置类型啊什么什么的,有需要的可以阅读源码。

<form action="${pagecontext.request.contextpath}/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file"/><br>
        <input type="file" name="file"/><br>
        <input type="file" name="file"/><br>
        <input type="submit" value="submit">
    </form>

enctype="multipart/form-data" 注意文件上传必须加上这个,而且必须是post请求。
然后我们来看下我们服务器是怎么接收到文件的,多文件上传使用 multipartfile[] 并且一定要在参数前加上注解 @requestparam("file")否则会报错。单文件上传我们只需要使用一个multipartfile对象,并且无需加注解。

 

/**
     *    单文件上传 :  multipartfile file
     *    多文件上传 :  @requestparam("file") multipartfile[] file
     *    多文件上传必须加上 @requestparam("file")注解否则会报错
     *  @author:miya.
     *  @time:2018-9-28 11:50
     */
    @requestmapping(value="upload",method=requestmethod.post)
    public string testfileupload(httpservletrequest request , @requestparam("file") multipartfile[] file){
        for (int i = 0; i < file.length; i++) {
            multipartfile multipartfile = file[i];
            system.out.println(" contenttype: " + multipartfile.getcontenttype());
            system.out.println(" name: " + multipartfile.getname());
            system.out.println(" originalfilename: " + multipartfile.getoriginalfilename());
            system.out.println(" size: " + multipartfile.getsize());
            //判断是否提交文件,如果没有那么跳过上传
            if(multipartfile.isempty()){
                continue;
            }
            // 获取文件的上传路径
            string uploadpath = request.getservletcontext().getrealpath("uploads");
            //获取文件名称
            string filename = multipartfile.getoriginalfilename();
            //截取文件后缀
            string fileext = filename.substring(filename.lastindexof("."));
            //生成新的随机文件名称
            string newfilename = uuid.randomuuid() + fileext;    
            //文件保存路径
            file savepath = new file(uploadpath + "/" + newfilename);
            //上传文件
            try {
                multipartfile.transferto(savepath);
            } catch (illegalstateexception e) {
                e.printstacktrace();
            } catch (ioexception e) {
                e.printstacktrace();
            }

        }
        return "welcome";
    }

 

整理下multipartfile经常使用的方法:

isempty():判断是否提交文件上来 

getcontexttype():获取文件类型

getname():获取表单元素名称

getoriginalfilename():获取文件名

getsize():获取文件大小

getbytes():获取字节数组

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网