java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring工具类

Spring常用一些工具类实例汇总

作者:codedot

这篇文章主要介绍了Spring常用一些工具类实例汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、内置Resource类型

二、本地化文件资源

org.springframework.core.io.support.LocalizedResourceHelper:允许通过文件资源基名和本地化实体获取匹配的本地化文件资源并以 Resource 对象返回

三、操作 Servlet API 的工具类

org.springframework.web.context.support.WebApplicationContextUtils 工具类获取 WebApplicationContext 对象。

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);

四、XML工具类

五、web相关工具类

六、参数检测工具类org.springframework.util.Assert

Assert断言工具类,通常用于数据合法性检查。

平时做判断通常都是这样写:

if (message== null || message.equls("")) {
throw new IllegalArgumentException("输入信息错误!");
}

用Assert工具类上面的代码可以简化为:

Assert.hasText((message, "输入信息错误!");
下面来介绍一下Assert 类中的常用断言方法:

七、请求工具类 org.springframework.web.bind.ServletRequestUtils

//取请求参数的整数值:
public static Integer getIntParameter(ServletRequest request, String name)
public static int getIntParameter(ServletRequest request, String name, int defaultVal) -->单个值
public static int[] getIntParameters(ServletRequest request, String name) -->数组

还有譬如long、float、double、boolean、String的相关处理方法。

八、其他工具类

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文