一、根目录:html
String rootPath = application.getRealPath("/");java
二、文件是否可写:node
public boolean isCanWrite(String dirPath) {
File file = new File(dirPath);
if(!file.exists()) {
file.mkdir();
}
if(file.canWrite()) {
return true;
} else{
return false;
}
}jquery
三、两个工具类须要导入的包linux
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.apache.commons.io.FileSystemUtils"%>angularjs
StringUtils.equals(欲验证字符串,"欲验证字符串的值");spring
参看:apache.commons.io真的很不错 http://hi.baidu.com/chenxiaowen/item/275004c3f834622def466545数据库
四、admin目录输入任何地址都定位到login.jsp 应该是shiro的配置,待研apache
其中 org.springframework.context.ApplicationContextAware 使用理解 须要注意 ,普通类拿任何spring bean的方法 须要继承该接口windows
String base = request.getContextPath();
ApplicationContext applicationContext = SpringUtils.getApplicationContext();
if (applicationContext != null) {
response.sendRedirect("login.jsp");
} 这个跳转 须要研究,为什么判断 applicationContext != null
五、动态参数
JDK1.5特性
void fun(Object... objs){}
拿这个举例子
你调用fun方法
fun(里面写多少参数都OK);
好比fun(1,"s");fun(1,2,"s");fun("s");
均可以
动态参数
六、LocaleResolver(本地化解析器)
参看 http://blog.csdn.net/wendellup/article/details/8532038
七、net.shopxx.service.impl.TemplateServiceImpl
Spring 3中新增的@value注解 http://www.linuxidc.com/Linux/2012-01/52464.htm
spring @Cacheable http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
http://www.oschina.net/question/82993_70254
八、shiro里面<property name="filterChainDefinitions"><value>的配置是上面的压住下面的。
还需注意的是:admin/login 这个登陆验证网址是要 anno 的,由于这个是能够匿名访问的,因此这个不管登陆成败其返回的页面均在该网址下,故就不须要配置登陆成功和登陆失败的页面的匿名访问了。
九、md5加密 DigestUtils.md5Hex("admin"); 要添加的包是commons-codec
十、登录的前后顺序:先是FormAuthenticationFilter 而后是Realm ,再是controller
十一、
String str = "abcd";
char [] c = str.toCharArray();
String s = new String(c); // 由char数组构建一个String对象
String s2 = c.toString(); // 将对象c的toString结果(一个String对象)赋给s2对象
s和s2都是String对象,他们的建立方式不一样
s值是 "abcd"
s2值是对象c的hascode,由于toStrng方法默认返回当前对象(c)的内存地址,即hashCode
十二、request method 'post' not supported http://blog.csdn.net/huanyingfengxing/article/details/8135590
1三、beanUtils 介绍:ORG.APACHE.COMMONS.BEANUTILS.BEANUTILS介绍
bean validation 介绍:小试Bean Validation SpringMVC介绍之Validation
14 interface BaseDao<T extends Serializable> 为何要这样写
15 MyEclipse10 中的两种FreeMarker插件的安装与配置
16 获取实体类
public BaseDaoImpl() {
Type type = getClass().getGenericSuperclass();
Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
entityClass = (Class<T>) parameterizedType[0];
}
17 类A中须要有属性xxx的public的 getXXX() 方法,这样的类的实例,赋值给freemarker模板以后,才能在模板中以${a.xxx}的方式取得值。
1八、SAXReader解析XML
String xmlAddress = "./aaa.xml";
SAXReader reader = new SAXReader();
File xmlFile = new File(xmlAddress);
Document document = reader.read(xmlFile);
这里用FILE方式读取,其实用URL是同样的
------------------------------------------
用dom4j就要用XPath,索引节点很是方便
这里的XPathExpression就是相似"/root/element/element"的字符串
具体表达式的应用去看下XPath教程就行,入手很容易
//返回符合表达式的节点LIST
List list = document.selectNodes(XPathExpression);
//返回符合表达式的一个节点
Node node = document.selectSingleNode(XPathExpression);
1九、
ServletContextAware 是一个接口,经过这个接口,能够将 ServletContext 对象赋值给类中的属性,好比:
public class TestAction implements ServletContextAware{
private ServletContext servletContext;
public void setServletContext(ServletContext context){
this.servletContext = context;
}
}
耦合的方式:ServletActionContext.getServletContext()
解耦的方式:Map application = ActionContext.getContext().getApplication();
1.在javax.servlet.Filter中直接获取
ServletContext context = config.getServletContext();
2.在HttpServlet中直接获取
this.getServletContext()
3.在其余方法中,经过HttpRequest得到
request.getSession().getServletContext();
20、为避免出现NullPointerException 应判断下 != null 如:if (item.getParent() != null && articleCategory.getId() == item.getParent().getId()) 若是item.getParent() 为空,要是不加item.getParent() != null的话,就会出现空指针异常的报错。加上item.getParent() != null 则能够避免,由于它自己返回假了,就不会再进行后面的代码执行了,就避免后面出现空指针异常了。
再一个,XXXServiceImpl 必定要将setBaseDao()方法用@Resource注入basedao
2一、enum枚举用法 http://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html
http://blog.csdn.net/congqingbin/article/details/7520137
enmu.values()方法返回
2二、
FreeMarker 缓存处理
FreeMarker 的缓存处理主要用于模版文件的缓存,通常来说,模版文件改动不会很频繁,在一个流量很是大的网站中,若是频繁的读取模版文件对系统的负担仍是很重的,所以 FreeMarker 经过将模版文件的内容进行缓存,来下降模版文件读取的频次,下降系统的负载。
当处理某个模版时,FreeMarker 直接从缓存中返回对应的 Template 对象,并有一个默认的机制来保证该模版对象是跟模版文件同步的。若是使用的时候 FreemarkerServlet 时,有一个配置项 template_update_delay 用来指定更新模版文件的间隔时间,至关于多长时间检测一下是否有必要从新加载模版文件,0 表示每次都从新加载,不然为多少毫秒钟检测一下模版是否更改。
FreeMarker 定义了一个统一的缓存处理接口 CacheStorage ,默认的实现是 MruCacheStorage 最近最少使用的缓存策略。通常状况下,不多须要对缓存进行扩展处理。您能够经过下面的代码指定最大缓存的模版数:cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250))
其中第一个参数是最大的强引用对象数,第二个为最大的弱引用对象数。这两个值 FreeMarker 默认的是 0 和 Integer.MAX_VALUE,代表模版缓存数是无限的
30、关于springmvc国际化的Message所引起的问题
首先参考第一篇文章 学习Spring必学的Java基础知识(8)----国际化信息 http://stamen.iteye.com/blog/1541732
里面有一句话 HierarchicalMessageSource接口最重要的两个实现类是ResourceBundleMessageSource和ReloadableResourceBundleMessageSource。
第二篇文章 解决: org.springframework.beans.factory.BeanNotOfRequiredTypeException办法 http://ekisstherain.iteye.com/blog/1569236
仔细钻研下。
以及 spring中MessageSource的配置使用方法1 http://blog.csdn.net/qyf_5445/article/details/8124306 http://blog.csdn.net/qyf_5445/article/details/8124431#comments
3一、时间互转
用SimpleDateFormat来转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2008-08-08 12:10:12");
3二、注意@Transient 必定放在get方法上,而不要放在属性上。
3三、springmvc的 @requestparam的问题。以下示例:
@RequestMapping("/test")
public String test(@RequestParam Integer pageNumber){
}
这种写法状况下,若是访问网址没带 pageNumber的参数,例如直接 访问 /test ,则会报错 :pageNumber is not present. 究其缘由应该是 默认required 为true
正确的写法应该是 @RequestParam(value = "pageNumber" , required = false ) Integer pageNumber ,此时直接访问/test ,pageNumber会被赋值 null
若是这样写了:@RequestParam(value = "pageNumber" , required = false ) int pageNumber ,仍是会报错pageNumber is not present. 究其缘由是pageNumber会被赋值null,但其类型倒是int类型的,因此不行,必须得用包装类型才能够。
3四、angularjs directive 的 templateUrl 指向的页面,必定要有根元素!!再者,定义directive名字成驼峰 anName 样式的时候,调用的格式是 <an-name></an-name>
3五、乱码问题 总览,参考 http://tchen8.iteye.com/blog/993504
遇到乱码问题,一般的检查项包括:
1. 编辑器保存文件的字符集;
2. 数据库的字符集;
3. 应用服务器或者Web服务器处理字符串采用的字符集
4. JSP对于字符集声明
5. Servlet过滤器,以及MVC框架拦截器对于字符集的处理
6. 其它涉及字符集处理的环节
(1) tomcat 设置编码 可参考 http://blog.csdn.net/loveaborn/article/details/44450873
3六、shiro 中获取servletContext 和WebApplicationContext
ServletRequest request = ((WebSubject)SecurityUtils.getSubject()).getServletRequest();
HttpSession httpSession = ((HttpServletRequest)request).getSession();
logger.debug("httpSession.getServletContext():"+httpSession.getServletContext());
context = WebApplicationContextUtils.getWebApplicationContext(httpSession.getServletContext());
37 设置myeclipse自动注释
preferences -> 搜 code template -> Comments -> Type
/**
*
*
* @author xxxxxxx
* @date ${date} ${time}
*/
而后把同类其余的注释都去掉
最后选择下面的Automatically add comments....
解决办法
在项目上右键Properties-》Project Facets,在打开的Project Facets页面中的Java下拉列表中,选择相应版本。
有多是java1.6 改为java6之类的
点击Eclipse上方菜单Window——Customize Perspective 自定义菜单
struts2设置了struts.multipart.saveDir后会在根目录创建文件夹,这样会涉及linux下的权限问题,
最好不要设置,使用struts默认
须要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}
接收变量防止乱码措施
request.getParameter("reply").getBytes("iso-8859-1"), "utf-8")