1.Java路径在不同系统下的处理,路径有的时候会进行手动拼接。那么拼接之后可能会出现一些怪异的情况。
例如:C://Test\/Finder
window下是支持/和\路径分割的。但是有的系统是使用/那么就会出现一些问题。
File.separator,是一个可以获取当前系统下文件路径分割符的静态常量。
那么我做一个小demo,用正则的方式,吧乱七八糟重复的路径都转换成当前的系统支持的路径。
但是还是要记住一件事情,那就是OSX下和Linux下是没有win下那样E:\的啦~~~~
String path = "//123\\\\1231231///\\\\1231";
path = path.replaceAll("[\\\\|/]+", File.separator);
System.out.println(path);
/**
* Console:
* /123/123/1231/1231
*/
/**
* 修正win下操作失败转义出错的问题。
* win下默认分割符是\,在java中是转义符,进行了转义。
*/
String separator = File.separator;
if(File.separatorChar == 92)
separator = "" + File.separatorChar + File.separatorChar;
String path = "//123\\\\1231231///\\\\1231";
path = path.replaceAll("[/\\\\]+", separator);
System.out.println(path);
2.Java获取连接中的参数
public static String getUrlParam(String url,String name){
try {
url = java.net.URLDecoder.decode(url,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String exPar = "[?&]" + name + "\\=([^&]+)";
Pattern p = Pattern.compile(exPar);
Matcher m = p.matcher(url);
boolean flag = m.find();
if(!flag) return null;
String parame = m.group();
return parame.substring(parame.indexOf("=") + 1);
}
public static String getUrlParam(String url,String name){
try {
url = java.net.URLDecoder.decode(url,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int index = url.indexOf("&" + name + "=");
if(index == -1) index = url.indexOf("?" + name + "=");
if(index == -1) return null;
int last = url.indexOf("&", index + 1);
if(last == -1) last = url.length();
return url.substring(index + name.length() + 2,last);
}
3.IDEA设置:文件File-设置setting-Keymap 搜索 completion,找到ctrl+空格的快捷键,删除旧的值,然后添加新的值alt+/,保存退出,就可以实现代码提示
4.如果使用Spring4.1.0以上版本,@ResponseBody出现406错误,请将jackson版本替换成2.x版本,jackson-annotations-2.4.4.jar、jackson-core-2.4.4.jar、 jackson-databind-2.4.4.jar 。
5.如果一个项目中在Tomcat5中可以正常运行,在Tomcat6中出现jsp文件无法转成Servlet,可以查看一下EL表达式是否存在">这种双引号嵌套了双引号的问题。
6.反射执行当前类里面某对象的方法
/**
* 反射执行某属性对象的方法
* @param fieldName 属性对象的名称
* @param methodName 该对象的方法
* @param args 调用改方法的参数列表
* @return 返回方法的返回值
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
private Object invokeMethod(String fieldName,String methodName,Object... args)
throws SecurityException, NoSuchFieldException, IllegalArgumentException,
IllegalAccessException, NoSuchMethodException, InvocationTargetException{
Class> thisClass = this.getClass();
Field field = thisClass.getDeclaredField(fieldName);
Object fieldObject = field.get(this);
Class> fieldClass = fieldObject.getClass();
Class>[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = fieldClass.getMethod(methodName, argsClass);
return method.invoke(fieldObject, args);
}
7.SpringMVC获取Response和Request
7.1使用注解@ModelAttribute
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
@ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
this.request = request;
this.response = response;
this.session = request.getSession();
}
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用
7.2加入监听器
web.xml加入监听器
org.springframework.web.context.request.RequestContextListener
Controller:
HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
其中监听器方法中的获取response以验证不可用,会报错,转换异常。