项目问题笔记汇总

1、根目录:

String rootPath = application.getRealPath("/");

 

2、文件是否可写:

public boolean isCanWrite(String dirPath) {
File file = new File(dirPath);
if(!file.exists()) {
file.mkdir();
}
if(file.canWrite()) {
return true;
} else{
return false;
}
}

 

3、两个工具类需要导入的包

<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.apache.commons.io.FileSystemUtils"%>

StringUtils.equals(欲验证字符串,"欲验证字符串的值");

参看:apache.commons.io真的很不错 http://hi.baidu.com/chenxiaowen/item/275004c3f834622def466545

 

4、admin目录输入任何地址都定位到login.jsp 应该是shiro的配置,待研

其中 org.springframework.context.ApplicationContextAware 使用理解  需要注意 ,普通类拿任何spring bean的方法 需要继承该接口

String base = request.getContextPath();
ApplicationContext applicationContext = SpringUtils.getApplicationContext();
if (applicationContext != null) {
response.sendRedirect("login.jsp");
}  这个跳转 需要研究,为何判断 applicationContext != null

 

5、动态参数

JDK1.5特性
void fun(Object... objs){}
拿这个举例子
你调用fun方法
fun(里面写多少参数都OK);
比如fun(1,"s");fun(1,2,"s");fun("s");
都可以
动态参数

 6、LocaleResolver(本地化解析器)

参看 http://blog.csdn.net/wendellup/article/details/8532038

 

 7、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

 

8、shiro里面<property name="filterChainDefinitions"><value>的配置是上面的压住下面的。

还需注意的是:admin/login 这个登录验证网址是要 anno 的,因为这个是可以匿名访问的,所以这个无论登录成败其返回的页面均在该网址下,故就不需要配置登录成功和登录失败的页面的匿名访问了。

 9、md5加密  DigestUtils.md5Hex("admin");  要添加的包是commons-codec

 

10、登陆的先后顺序:先是FormAuthenticationFilter 然后是Realm ,再是controller

11、

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

12、request method 'post' not supported http://blog.csdn.net/huanyingfengxing/article/details/8135590

13、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}的方式取得值。

18、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);

19、
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

21、enum枚举用法 http://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html

http://blog.csdn.net/congqingbin/article/details/7520137

enmu.values()方法返回

22、

ClassPathResource

ClassPathResource类,如果没有指定相对的类名,该类将从类的根路径开始寻找某个resource,如果指定了相对的类名,则根据指定类的相对路径来查找某个resource。
Resource rs = new ClassPathResource("onlyfun/caterpillar/beans-config.xml");
或者
Resource rs = new ClassPathResource("beans-config.xml",SpringDemo.class);
 
23、dom4j 根据attributeValue 获得节点内容
 
24 、properties配置文件中千万注意行末不要有空格,否则会出错
25、比较两个文件是否是同一个用equals()方法,用== 不奏效。原因待研。比较字符串是否为空,一般用 str == null || "".equals(str)  具体可参考 http://blog.csdn.net/qq799499343/article/details/8492672
26、EhCache的设定和删除,注意key的写法 @Cacheable(value = "CUSTOM_CACHE", key = "'articleCategory'")    @CacheEvict(value = "CUSTOM_CACHE", key = "'articleCategory'")
如果再设定 @Cacheable(value = "CUSTOM_CACHE", key = "'another'")  ,则上面 @CacheEvict(value = "CUSTOM_CACHE", key = "'articleCategory'") 缓存的删除不影响key为another的缓存。
 
 27、用jquery validator.js 做表单验证时,验证密码(password)和确认密码(rePassword)两项相等时,equalTo:"#password",则password表单属性一定有一个 id="password" 否则不能验证
28、myeclipse修改文字大小 http://jingyan.baidu.com/article/b87fe19ebc17fd5218356831.html
29、

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

 

31、时间互转

用SimpleDateFormat来转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2008-08-08 12:10:12");

32、注意@Transient 一定放在get方法上,而不要放在属性上。

33、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类型的,所以不行,必须得用包装类型才可以。

34、angularjs directive 的 templateUrl 指向的页面,一定要有根元素!!再者,定义directive名字成驼峰  anName 样式的时候,调用的格式是 <an-name></an-name>

35、乱码问题  总览,参考 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

36、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 自定义菜单

 

 

windows linux 下,获取java项目绝对路径的方法

 

 

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")

posted @ 2014-06-26 14:08  ProgrammerZHANG  阅读(485)  评论(0编辑  收藏  举报