struts2+hibernate+srping学习笔记

来源:互联网 发布:php解析视频 编辑:程序博客网 时间:2024/05/01 19:53

struts2常用的拦截器
conversionError:类型转换错误拦截器
exception:异常拦截器
fileUpload:文件上传拦截器
i18n:国际化拦截器
logger:日志拦截器
params:解析请求参数拦截器
validation:校验拦截器
timer:这个拦截器负责输出Action的执行时间,以分析该Action的性能瓶颈。

www.apache.org
www.opensymphony.com
www.allimant.org/javadoc/index.php  chm
www.oracle.com/technetwork/java/archive-139210.html  Oracle Java Archive
www.jfree.org
http://www.springsource.org/

 

 

1.  添加Spring(
spring 2.0 AOP libraries;
spring 2.0 core libraries;
spring 2.0 persistence core libraries;
spring 2.0 persistence JDBC libraries;
spring 2.0 web libraries
)注意copy checked library jars to project folder.
Enable AOP builder去掉
Folder 改为 WebRoot/WEB-INF下
web.xml中添加<listener><listener-class>org.springframework.web.context.ContextLoaderListener></...>
需手动添加commons-dbcp包到lib下,否则org.apache.commons.dbcp.BasicDataSource 报错找不到
在applicationContext.xml中增加default-autowire="byName",否则出现"sessionFactory " or "hibernateTemplate " is required异常

2. 添加Hibernate(将SessionFactory交给Spring管理)-- copy library
Eclipse中右键点击Hibernate Reverse Engineering -- 选中Create POJO和Java Data Object(取消create abstract class)

3. 添加Struts2的Jar包:
commons-logging-1.0.4.jar;
freemarker-2.3.8.jar;
ognl-2.6.11.jar;
struts2-core-2.0.11.jar;
struts2-spring-plugin-2.0.11.jar;
xwork-2.0.4.jar
struts2.3.4还必须添加以下包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-lang-2.4.jar
javassist-3.11.0.GA.jar

将项目目录struts 2 core libraries下包拷贝到webroot得lib目录下
项目SRC目录下建立 struts.xml,添加以下内容
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

修改 web.xml,添加以下内容
<!-- 过滤器 -->
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
<!-- struts2.1之前版本用
    org.apache.struts2.dispatcher.FilterDispatcher
-->
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter> 
 
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <!-- 监听器 -->
  <listener>
   <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
  </listener>

4. spring中对于没有状态的Bean:scope="singleton"(对于每一个IOC容器只会生成唯一的实例)
设计模式中对于JVM只生成一个类的实例

5. asm.jar是hibernate的,asm-2.2.3.jar是spring,两个jar包冲突,需删除第二个
需要加载的包(必须,在apache官方网站下载commons项目):commons-dbcp.jar和commons-pool.jar
   需要删除的包(必须):asm-2.2.3.jar


6. OGNL表达式

7. 检查是否使用正确的Struts标签,不然页面无法找到

8. 将网页导出到Excel:1,从数据库读出,POI组件生成EXCEL文件。2,然后以InputStream形式输出到浏览器用户

9. JAVA产生输出数据的,包含左边,不含右边

10. 对于Struts2文件下载,默认是inline,如果需要另存为则设置成attachment
 struts.xml中<param name="contentDisposition">attachment;filename="AllUsers.xls"</param>

11. struts标签库所在目录:struts2-core-2.0.11.jar---META-INF---struts-tags.tld

12. "".equals(this.getUsername().trim()常量通常写在前面,这样可以避免出现异常(变量为NULL时出现)

13. Object是所有类的根类

14. int x = Integer.parseInt(y);

15. 当进行输入校验,有错误出现后,表单的内容自动清除,这是因为页面为请求转发(dispatcher)方式,重定向(redirect)。需配置

struts.xml
<action>
  <result name="input" type="dispatcher">/register.jsp</result>
</action>

16. 样式 <td style="color:red">

17. struts2会自动添加表格,要改变可以使用 theme="simple"(ajax),采用actionerror时可以使用,但是fielderror出错信息将不会出现,表

单国际化时不能用simple。

18. 在struts.xml中<action name="register" class="com.test.action.RegisterAction" method="abc">这样配置后,就会执行abc方法,不再

执行execute方法。
如果一个action需执行多个方法,可以添加多个配置<action method="xyz">,验证方法名为validateXyz()。
不让validate方法默认执行,有两种方式,一种是什么也不写,另一种是改名为validateExecute()。
方法过滤拦截器:
<interceptor-ref name="myInterceptor">
  <param name="includeMethods">abc,xyz</param>
  <param name="excludeMethods">abc,xyz</param>
</interceptor-ref>

19. equals与==比较:从Object层次来说,==与equals是相同的,都是比较内存地址,也就是说都是比较两个引用是否指向同一个对象,是则返回

true,否则返回false。 很多类overwrite了equals方法,最典型的是String类。(在此equals比较的是内容,==比较内存地址)

20. 拦截器interceptor将action包在中间,进行出入管理

21. 如果手工添加了任何一个拦截器,默认拦截器将失效,需手动添加<interceptor-ref name="defaultStack">

22. OS,IS使用完后记得关闭 os.close(); is.close();

23. struts.xml中的 <include>可以包含多个配置文件,<constant>可以改变struts.properties的默认配置。

24. 在<s:form>中添加<s:token>可以防止表单重复提交,注意在struts.xml配置文件的<action>中还要添加拦截器<interceptor-ref

name="token">和<interceptor-ref name="defaultStack">,<result name="invalid.token">/register.jsp</result>,重复提交的错误信息在

actionerror中。

25. struts2对JFreeChart进行集成时,struts.xml配置要修改成extends="jfreechart-default",而struts-plugin.xml配置文件也要进行修改,

改成<package name="jfreechart-default" extends="struts-default">
更简便的方法:extends="struts-default,jfreechart-default" 两个之间用逗号分隔。

26. jar打包程序:jar cvf name.jar -C *

27. Jasperreport对于字段的表示方法:$F{field name}
 对于变量的表示方法:$V{variable name}
 对于参数的表示方法:$P{param name}
  将iReport生成的test.jasper文件copy到项目WebRoot下。

28. Jasperreport与struts整合:添加struts包中的*.plugin.jar,添加jasperreports-3.7.2.jar,添加jasperreport-3.7.2-project中lib下所

有的jar包。

29. 使用MYECLIPSE自动生成STRUTS2,必须将JAR包拷贝到项目LIB目录下(myeclipse-project capabilities-struts2-add

jar/zip),struts.xml在SRC目录下


30. 使用MYECLIPSE自动生成SPRING,org.apache.commons.dbcp.BasicDataSource 找不到 是因为你没有加依赖包 commons-dbcp包

31. MyEclipse修改为默认打开JSP源码方式, window--preferences--general--editors--file associations-*.jsp-JSP editor(default)

32. struts2类型转换:convertFromString, convertToString

33. struts2校验框架xml对输入内容校验参考  xwork-2.0.4.jar/com/opensymphony/xwork2/validator/validators/default.xml
  错误信息保存在fieldError中,JSP页面需改成<s:fielderror/>


34. struts2使用客户端校验将JSP页面中form validate="true",theme不能设置为simple

35. 拦截器可拦截、排除指定方法,excludeMethods, includeMethods
  <interceptor-ref name="myInterceptor">
 <param name="excludeMethods">execute</param>

35. 编写拦截器类继承 AbstractInterceptor

36. struts2中配置struts.xml全局结果:
   <global-results>
 <result name="login" type="redirect">/login.jsp</result>

37. JSP上传文件页面必须具备两个条件 <form method="post" enctype="multipart/form-data">
  action中定义三个变量 file, fileFileName, fileContentType,在源代码..struts2.interceptor.FileUploadInterceptor中可找到
  上传有中文文件名文件时在struts.xml中添加<constant name="struts.i18n.encoding" value="gbk">
  struts2-core-2.0.11.jar.org.apache.struts2.default.properties中修改上传大小,action后缀等

38. struts2指定上传文件类型可查TOMCAT中web.xml,<interceptor-ref><param name="allowedType">,错误信息显示查找 struts-

messages.properties
 
39. 表单国际化时不能用simple。国际化包括:JSP页面国际化,action(方法)信息传到页面国际化<s:text>,类型转换提示信息,输入校验
  在struts.xml中添加 <constant name="struts.custom.i18n,resources" value="message">

40. 修改类不用重启Tomcat加载整个项目(手工启动) 解决方法: 修改tomcat  conf目录下的server.xml,添加:<Context path="/struts2"

docBase="D:\workspace\lee_struts2\WebRoot" reloadable="true"/> , 注:docBase="",中的内容为项目的WebRoot目录

41. struts.xml模块化,包含多个配置文件方法 <include file="struts1.xml"></include>

42. struts.xml的package中添加namespace属性可改变URL目录,利于项目分级

43. 改变JSP页面表单错误信息显示位置,在<s:fielderror>中添加<s:param>字段名</s:param>

44. 文件上传必commons-fileupload...jar, commons-io...jar 包

45. 出现"sessionFactory " or "hibernateTemplate " is required异常解决方案,在applicationContext.xml中修改:
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="   
                http://www.springframework.org/schema/beans    
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
                http://www.springframework.org/schema/aop    
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                http://www.springframework.org/schema/tx    
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-2.5.xsd"
                 default-autowire="byName" default-lazy-init="true">

总的原因就是缺少了这句default-autowire="byName"。

 

 

 

 

 

 

 

 

 

 

 

 

 

 


debug
f5:进入当前方法,
f6:运行下一行代码
f7:执行完当前方法,再跳出当前方法
f8:执行到最后, 一直执行到下一个断点。


如果不怎么懂的话,建议使用f5。因为f5的话如果没有方法可以进去的话。f5就等于f6。只不过步骤多一些而已。当然如果你比较熟悉的话。想不

进入某个方法就直接使用f6。当有一些源码没有的时候我们就直接f7。跳过是最好的了。比如说tomcat源码。我们没有必要了解那么多。所以就

f7.

 

 

 

 

 

 

Eclipse
1.ctrl+t 快速找到方法
2.alt+/ 提示
删除行(Delete Line):CTRL+D
    查找下(上)一位置:CTRL+K/CTRL+SHIFT+K
    定位具体某一行:CTRL+L
    向上(下)移动选中的行:ALT+UP/DOWN ARROW
    文件中出现次数:CTRL+SHIFT+A
    打开检索对话框:CTRL+H
    单词自动完成:ALt+/
自动格式化:CTRL+SHIF+F

 


MYSQL
1. mysql -uroot -p;
2. create database news;
3. use news;
4. source c:/news.sql;
5. show tables;
6. desc users;
7. select * from users;

 

遇到类型转换错误的时候(也就是说不能进行类型转换),struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面

类型转换与输入校验的流程

1. 首先Struts2对客户端传来的数据进行类型转换

2. 类型转换完毕后再进行输入校验

3. 如果类型转换和输入校验都没有错误发生,那么进入execute方法(调用商业逻辑)


注意:如果类型转换不成功,也同样要进行输入校验
------
1. 真正存放field级别错误信息的对象是LinkedHashMap

2. 该LinkedHashMap的key是String类型的,value是ArrayList类型的

3. 对于Action级别的错误信息,实际上是放置在ArrayList中的

4 拦截器:Interceptor

——————
  A  a=new   B()  
  可以这样理解:  
  A   a代表a可以实现A的所有功能,或者说的正式一点,a实现了接口A  
  java是单一继承的,如果没有接口,类之间只有树状结构,例如  
  车-四轮车-轿车  
      -二轮车-摩托车  
  此时,轿车和摩托车的关系只能追溯到"车"了  
  如果没有接口,对于"机动车"这个概念,只能定义为类,是"车"的子类,  
  轿车和摩托车因为java的单一继承,是无法再继承到"机动车"类下的  
  这就是有根树的结构的缺陷  
  对于接口,我觉得可以看作是"功能",类看作是"物体"  
  A   a=new   B()  
  表示a是B类的"物体",但是又有A的"功能"


  接口:是提供一些方法和属性的定义,而不去具体的去实现它,  
  接口在JAVA中是替代C++中的多继承。  
   
  要面向接口编程,不要面向实现编程!!!!  
或者是:要面向抽象编程,不要面向具体编程。

要面向接口编程,不要面向实现编程!!!!  
   
  我来解释一下  
   
  针对抽象编程(同样的意思)  
  假设你的程序中有   苹果,葡萄,香蕉  
  那么你要new   苹果().dosomething();这显然麻烦,如果你定义一个接口   水果;  
  那么你可以  
  水果   x=new   苹果()  
  水果.dosomething;  
  x=new   葡萄();  
  x.dosomethting();....  
   
  如果你要加入一个   桃子,那么任何代码都不用变,只要改变new   桃子()就可以了  
  这就是针对抽象编程  
  它使得程序容易维护  

————————————
1. 访问OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性:  名称 作用 例子
parameters 包含当前HTTP请求参数的Map #parameters.id[0]作用相当于request.getParameter("id")
request    包含当前HttpServletRequest的属性(attribute)的Map #request.userName相当于request.getAttribute("userName")
session    包含当前HttpSession的属性(attribute)的Map #session.userName相当于session.getAttribute("userName")
application 包含当前应用的ServletContext的属性(attribute)的Map #application.userName相当于application.getAttribute("userName")
attr 用于按request > session > application顺序访问其属性(attribute) #attr.userName相当于按顺序在以上三个范围(scope)内读取

userName属性,直到找到为止

#号
1.用于直接取根目录中的对象,在struts2中就是contextMap中的request,session ,attr等等对象
2.用于过滤或者反射 <s:if test="#xxx"> ,xxxx.{#this }
3用于构造map #{"xxx":"ddd"}
4根据自己经验,还用来表示一些由<s:set>之类定义的临时页面对象。

%号
%{exp}用来计算exp值代入到页面中,相当于<%=exp%>


今天在做Ant\Xdoclet辅助生成hibernate实体映射文件时,eclipse出现了极其让我郁闷的错误,"The word is not correctly spelled" 我讨教

过一些朋友以后,还是没有得到答案,我就纳闷了。然后我继续将代码执行下去,居然执行没有问题,还真的生成了实体映射文件,这下问题的根

源就找到了!是eclipse的问题,打开eclipse,我忽略掉了错误提示,这才没有出现"The word is not correctly spelled"的错误提示。
操作步骤:在eclipse下的Window--Preference输入spell,然后把第一个复选框“Enable spell checking“给去掉就可以了

————————————
实例就是你new出来的类的对象引用。比如 String str = "hello";
str就是String的一个实例。
实例就是某个类的一个对象。
类只是一个抽象的东西,对象才是实在的东东。所以叫实例
实例域就是指定义类时的最外层的那两个大括号那个范围。

————————————
==用来比较对象引用。  
  equals比较对象存储内容

==,比较两个String的指针是否指向同一个内存地址  
  equals(),比较String的面值

 

----------------------------
datetime 显示为'YYYY-MM-DD HH:MM:SS'格式,范围为'1000-01-01 00:00:00'到'9999-12-31 23:59:59'
date      显示为'YYYY-MM-DD'格式,范围为'1001-01-01'到'9999-12-31'
timestamp 范围从'1970-01-01 00:00:01'UTC 到'2038-01-09 03:14:07'UTC


--------------------------------
在我以前的了解中,String是一个final Class, StringBuffer不是。所以对于 String a = "yacht" ,String b = "yacht1" String c = a + b

; 存在一个对象拷贝构造和解析的消耗问题;对于一个StringBuffer来说,StringBuffer sb = new StringBuffer();sb.append("yacht") ;

sb.append("yacht1"); 因为StringBuffer是一个可以实例化的类,而且它的内建机制是维护了一个capacity大小的字符数组,所以它的append操

作不存在对象的消耗问题,所以我觉得如果存在String连接这种事情,StringBuffer来做会好很多。
       但事情并不是这么简单,看下面代码

       String a = "yacht1" + "yacht2" + "yacht3" + "yacht4";

       StringBuffer sb = new StringBuffer(); sb.append("yacht1") ; sb.append("yacht2"); sb.append("yacht3") ; sb.append

("yacht4");  String a = sb.toString();

       如果按照我先前说的看法,红色的效率肯定比蓝色的低,但经过测试不是这样,为什么?这里,我们需要理解程序过程的两个时期,一个

是编译时,一个是运行时,在编译时,编译器会对你的程序做出优化,所以红色的String a会被优化成yacht1yacht2yacht3yacht4,而蓝色的

StringBuffer只会在运行时才处理。所以效率是不一样的。

       如果代码是这样的:

       String a ; for(int i = 0; i< 100000;i++){ a += String.valueOf(i) ;}

       StringBuffer sb = new StringBuffer(); for(int i = 0; i< 100000;i++){ sb.append(i) ;} String a = sb.toString();

       如果是这种情况的话,红色的效率就大大不如蓝色,区别在哪里,就在于运行时和编译时的优化问题上!


--------------------------------

 

 

 

 

 

 


 

 

原创粉丝点击