资源描述
SSH学习笔记
王利江
7月
1. Struts1 4
1.1 TCP/IP合同组 4
1.2 术语和缩写词 4
1.3 遵循原则 4
2. Struts2 5
2.1 struts2长处 5
2.2开发struts2至少需要jar文献 5
2.3 struts2解决流程 6
2.4 struts2struts.xml配备文献 6
2.4.1 action配备项 6
2.4.2 struts2常量 8
2.4.3指定各种配备文献 9
2.5 接受祈求参数 9
2.5.1基本类型祈求参数 9
2.5.2复合类型祈求参数 9
2.5.3版本2.1.6中午参数乱码问题 10
2.6类型转换器 10
2.6.1局部类型转换器 10
2.6.2全局类型转换器 11
2.7访问添加属性 11
2.8文献上传 12
2.8.1单文献上传 12
2.8.2多文献上传 13
2.9自定义拦截器 15
2.10输入校验 16
2.10.1输入校验流程 16
2.10.2手工编码实现 16
2.10.3基于XML配备方式实现 18
2.11国际化 21
2.12OGNL表达式 23
2.13惯用标签 26
2.14防止表单重复提交 29
3. Spring 31
3.1实例化 31
3.1.1 spring容器实例化 31
3.1.2 bean实例化 31
3.1控制反转IOC 32
3.1.2个人令牌版 32
4. Hibernate 33
4.1安装配备 33
3.1.2个人令牌版 33
5. Ibatis 34
4.1正则表达式 34
3.1.2个人令牌版 34
6. Thread多线程 35
6.1创立线程两种方式 35
6.1.1继承Thread类 35
6.1.2实现Runnable接口 35
6.2互斥与同步通讯 36
6.3ThreadLoacl类 37
6.4各种线程访问共享对象和数据方式 38
6.5线程池 40
6.x练习题 40
7. AJAX 43
7.1使用dom方式开发ajax实例 43
7.1.1返回格式为Text文本 43
7.1.2返回格式为XML格式数据 45
7.2使用dom方式开发ajax实例 46
1. Struts1
1.1 TCP/IP合同组
TCP/IP是一组在Internet网络上不同计算机之间进行通信合同群,它是由TCP(传播控制合同)、IP(Internet合同)、UDP(顾客数据报合同)、HTTP(超文本传播合同)、FTP(文献传播线合同)、SMTP(简朴邮件传播合同)等一系列合同构成。它们从下往上可以分为四层构造:
l 物理层(以太网、令牌环、X.25等)
l 网络层(IP合同)
l 传播层(TCP合同、UDP合同)
l 应用层(HTTP合同、FTP合同、SMTP合同等)
从一台计算机发往另一台计算机数据都是按如下顺序发送和接受:
发送数据计算机先将数据传到应用层,由上往下传送,数据通过每一层时,所使用合同都给数据加上一种合同头,然后将加上合同头数据传到下一层,下一层所使用合同在给它加上一种合同头,继续向下传递,最有由物理层经硬件设备发送到网络上。接受数据计算机则相反,数据时由下往上传递,每通过一层都剥去相应合同头,然后继续向上传递。最后传递给顾客数据是剥去所有合同头数据,即原始数据。
假设你浏览器想从网路上运营WEB服务器上获取数据,它先将一种HTTP祈求发送到TCP层,加上一种TCP端口地址后发往IP层,IP层在给它加上一种IP地址,通过物理层将此祈求发往网络上指定计算机。
在接受数据一方,每层都剥去相应地址信息,然后决定下一步该做什么。物理层在接受到消息后,将数据发往IP层,IP层发现这是一种包,将剥去IP头数据发往TCP层,TCP层再将剥去TCP头数据发送给计算机上运营HTTP服务代理,HTTP代理依照这个祈求进行相应解决,然后再将成果按照相应途径返回给祈求者。
1.2 术语和缩写词
无。
1.3 遵循原则
本文档遵循宇新数据科技有限公司《需求分析阐明书编写规范》。
2. Struts2
2.1 struts2长处
和struts1 相比,struts2长处列举如下:
n 在软件设计上struts2没有像struts1那样Servlet API和Struts API有着紧密耦合,struts2应用可以不依赖于Servlet API和Struts API,属于无侵入式设计,而struts1属于侵入式设计。
n Struts2提供了拦截器,运用拦截器可以进行AOP编程,实现如权限拦截等功能。
n Struts2提供了类型转换器,可以把特殊祈求参数转换为需要类型。
n Struts2提供支持各种体现层技术,如:jsp、freeMarker、velocity等
n Struts2输入校验可以对指定办法进行校验
n 提供了全局范畴、包范畴和action范畴国际化资源文献管理实现
2.2开发struts2至少需要jar文献
n Struts 2.1.6 和struts2.1.8
l Struts2-core-2.x.x.jar:struts2框架核心类库。
l Xwork-2.x.x.jar:xwork类库,struts2在其基本上构建。
l Ognl.2.6. x.jar:对象图导航语言(Object Graph Navigation Language),struts2通过其读写对象属性。
l freeMarker-2.3.x.jar:Struts2UI标签模板使用freeMarker编写
l commons-logging-1.1.x.jar:ASF出品日记包,struts2框架用这个日记包来支持Log4j和JDK 1.4+日记记录
l commons-fileupload-1.2.1.jar:文献上传组件,2.1.6后来版本必要加入该组件
n Struts 2.3.4
l Struts2-core-2.x.x.jar:struts2框架核心类库。
l Xwork-2.x.x.jar:xwork类库,struts2在其基本上构建。
l Ognl.2.6. x.jar:对象图导航语言(Object Graph Navigation Language),struts2通过其读写对象属性。
l freeMarker-2.3.x.jar:Struts2UI标签模板使用freeMarker编写
l commons-logging-1.1.x.jar:ASF出品日记包,struts2框架用这个日记包来支持Log4j和JDK 1.4+日记记录
l commons-fileupload-1.2.1.jar:文献上传组件,2.1.6后来版本必要加入该组件
l javassist-3.11.0.GA.jar
l commons-lang3-3.1.jar
l commons-io-2.0.1.jar
2.3 struts2解决流程
StrutsPrepareAndExecuteFilter是struts2框架核心控制器,它负责拦截友<url-pattern>/*</url-pattern>指定所有顾客祈求,当顾客祈求到达时,该Filter会过滤顾客祈求。默认状况下,如果顾客祈求途径不带后缀或者后缀以.action结尾,这时祈求将被转入struts2框架进行解决,否则struts2框架将略过该祈求解决。当祈求转入struts2框架解决时会先通过一系列拦截器,然后再到action。与struts1不同,struts2对顾客每次祈求都会创立一种action,因此struts2中action是线程安全。
2.4 struts2struts.xml配备文献
2.4.1 action配备项
n action名称搜索顺序
URL:http://lserver/struts2/path1/ path2/ path3/helloworld.action
l 环节一:一方面寻找namespace为/path1/ path2/ path3package,如果不存在这个package则执行环节二,如果存在这个package则在这个package中寻找名字为helloworldaction,如果找不到action就会直接跑到默认namespace(默认命名空间为空字符串“”),如果默认命名空间package中还找不到,页面提示找不到action
l 环节二:寻找namespace为/path1/ path2package,如果不存在这个package则执行环节三,如果存在这个package则在这个package中寻找名字为helloworldaction,如果找不到action就会直接跑到默认namespace(默认命名空间为空字符串“”),如果默认命名空间package中还找不到,页面提示找不到action
l 环节三:寻找namespace为/path1package,如果不存在这个package则执行环节四,如果存在这个package则在这个package中寻找名字为helloworldaction,如果找不到action就会直接跑到默认namespace(默认命名空间为空字符串“”),如果默认命名空间package中还找不到,页面提示找不到action
l 环节四:寻找namespace为/package,如果存在这个package则在这个package中寻找名字为helloworldaction,当在package中找不到action或者不存在这个package时,都会去默认namespacepackage中寻找action,如果还找不到,页面提示找不到action
n action配备项默认值
<action name="addUI">
<result>/WEB-INF/page/employeeAdd.jsp</result>
</action>
l 如果没有为action指定class,默认是ActionSupport
l 如果没有为action指定method,默认执行actionexecute()办法
l 如果没有指定resultname属性,默认执行success
n actionresult惯用配备项
l dispactcher :默认值,祈求转发,类似于struts1中forward
l redirect:祈求重定向
<result type="redirect">/index.jsp?username=${username}</result>
l redirectAction:将祈求重定向到其他action
<!-- 祈求重定向,不能重定向到WEB-INF下jsp文献 ${username} 为action中属性值 -->
<result type="redirectAction">helloworld</result>
如果重定向到其她package中action,需要为redirectAction注入两个参数:其她package命名空间和action名称,如:
<result type="redirectAction">
<!-- 重定向到其他package中action -->
<param name="actionName">actionName</param>
<param name="namespace">/namespace</param>
</result>
l plaintext:显示原始文献内容
<result type="plainText">/index.jsp</result>
当原始文献内容包括特殊编码字符,如果需要正常显示原文献内容,需要为plainText注入两个参数:原始文献途径location和使用字符集charSet,如:
<result type="plainText">
<param name="location">/index.jsp</param>
<param name="charSet">UTF-8</param>
</result>
n 为action类属性注入参数
<action name="helloworld" class="com.vanceinfo.action.HelloWorldAction"
method="execute">
<param name="savepath">/inmages</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
n 动态办法调用
动态办法调用方式为:actionName!method.action,如:helloworld!addUI.action
n 使用通配符
<action name="list_*" class="com.vanceinfo.action.HelloWorldAction" method="{1}">
<param name="savepath">employee</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
{1}表达“list_*”中第一种*代表字符串。在访问时候使用list_addUI表达是访问action名称为listaddUI办法
2.4.2 struts2常量
n struts2常量加载顺序
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在各种文献中都配备了同一种常量,则后一种文献中配备常量值会覆盖前面文献中配备常量值。
n 惯用struts2常量
l 指定默认编码集,作用于HttpServletRequestsetCharacterEncoding办法和freeMarker、velocity输出编码:
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
l 指定struts2解决祈求后缀,该属性默认值为action,即所有匹配*.action祈求都由struts2负责解决,如果顾客需要指定各种祈求后缀,则在各种祈求后缀之间用英文逗号分隔。
<constant name="struts.action.extension" value="do" />
也可以设立同步解决各种action后缀,value值用“,”分割即可,如下:
<constant name="struts.action.extension" value="do,action"/>
l 设立浏览器与否缓存静态内容,默认值为true(生产环境使用),开发环境建议关闭。
<constant name="struts serve.static.browserCache " value="true"/>
l 当struts2配备文献修改后,系统与否自动加载该文献,默认值为false(生产环境使用),开发阶段建议打开。
<constant name="struts.configuration.xml.reload" value="false"/>
l 开发模式下使用,可以打开更详细错误信息。
<constant name="struts.devMode" value="false"/>
l 默认视图主题
<constant name="struts.ui.theme" value="simple"/>
l 与spring集成时,指定由spring负责action创立
<constant name="struts.objectFactory" value="spring"/>
备注:2.3.4版本有变化
l Struts2与否支持动态办法调用,默认值为true,如要关闭动态办法调用,可以设立为false
<constant name="struts.enable.DynamicMethodInvocation" value="flse"/>
l 上传文献大小限制
<constant name="struts.multipart.maxSize" value="1024"/>
2.4.3指定各种配备文献
<struts>
<include file="struts-user.xml" />
<include file="struts-base.xml" />
</struts>
2.5 接受祈求参数
2.5.1基本类型祈求参数
在action类中定义与祈求参数同名属性,struts2便能自动接受祈求参数并赋予给同名属性。祈求途径为:http://localhost:8080/struts2/control/employee/product.do?id=12345
public class ProductAction {
private Integer id;
public Integer getId() {
return id;
}
//Struts2通过反射技术调用与祈求参数同名属性setter办法来获取祈求参数值
public void setId(Integer id) {
this.id = id;
}
}
Form使用举例:
<form action="control/employee/product.do" method="post/get">
id:<input type="text" name = "id"><br/>
name:<input type="text" name = "name"><br/>
<input type="submit" value = "发送"><br/>
</form>
2.5.2复合类型祈求参数
祈求途径为:http://localhost:8080/struts2/control/employee/product.do?pd.id=12345
public class ProductAction {
private Product product;
public Product getProduct) {
return product;
}
public void setProduct (Product product) {
this. product = product;
}
}
Struts2一方面通过反射技术调用Product默认构造器创立product对象,然后再通过反射技术调用product中与祈求参数同名属性setter办法来获取祈求参数值。
Form使用举例:
<form action="control/employee/product.do" method="post">
id:<input type="text" name = "product.id"><br/>
name:<input type="text" name = "product.name"><br/>
<input type="submit" value = "发送"><br/>
</form>
2.5.3版本2.1.6中午参数乱码问题
2.6类型转换器
自定义类型转换器需要实现struts2类型转换器类DefaultTypeConverter,并重写该类convertValue办法:如下
public class DateTypeConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String,Object> context,Object value,
Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
try {
if (toType == Date.class) {
String[] params = (String[]) value;// request.getParameterValues();
System.out.println("StringToDate = " +params.length);
System.out.println("StringToDate = " +params[0]);
System.out.println("StringToDate = " +dateFormat.parse(params[0]));
return dateFormat.parse(params[0]);
} else if (toType == String.class) {
Date date = (Date) value;
System.out.println("DateToString = " +dateFormat.format(date));
return dateFormat.format(date);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
2.6.1局部类型转换器
将DateTypeConverter注册为局部类型转换器:
在action类所在包下放置ActionClassName-conversion.properties文献,ActionClassName是action类名,背面-conversion.properties是固定写法,如:文献名称为HelloWordAction-conversion.properties,在properties文献中内容为:
属性名称=类型转换器全类名
例如,对HelloWordActionprivate Date birthday;字段进行转换为:
birthday=com.spring.type.converter. DateTypeConverter
2.6.2全局类型转换器
将DateTypeConverter注册为全局类型转换器:
在WEB-INF/classes下放置xwork-conversion.properties文献,在properties文献中内容为:待转换类型=类型转换器全类名,即为:java.util.Date=com.spring.type.converter. DateTypeConverter
2.7访问添加属性
访问或添加request/session/application属性
n 方式一:使用struts2ActionContext类,使用如下:
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
ctx.getApplication().put("app","应用范畴");
ctx.getSession().put("ses","session范畴");
ctx.put("res","request范畴");
return "success";
}
JSP中使用如下:
<body>
${applicationScope.app}<br />
${sessionScope.ses}<br />
${requestScope.res}<br />
</body>
n 方式二:使用struts2servletActionContext类,获取HttpServletRequest、HttpSession、ServletContext、HttpServletResponse等对象引用,然后通过setAttribute办法设立属性值。
public String rsa() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext context = ServletActionContext.getServletContext();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
request.setAttribute("req","祈求范畴属性");
context.setAttribute("app","应用范畴属性");
session.setAttribute("ses","会话范畴范畴");
request.setAttribute("names",Arrays.asList("张三","李斯","王五"));
return "success";
}
JSP中使用如下:
//引入jstl标签库,需要导入standard.jar和jstl.jar文献
<%@ taglib uri="" prefix="c"%>
<body>
${applicationScope.app}<br />
${sessionScope.ses}<br />
${requestScope.res}<br />
<c:forEach items="${names}" var="name">
${name}<br/>
</c:forEach>
</body>
n 方式三:实现ServletRequestAware、ServletResponseAware、ServletContextAware接口,由struts2框架运营时注入。
public class HelloWorldAction implements ServletContextAware,
ServletRequestAware,ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private ServletContext context;
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void setServletContext(ServletContext context) {
this.context = context;
}
public String execute() throws Exception {
this.request.setAttribute("req","祈求范畴属性");
this.context.setAttribute("app","应用范畴属性");
HttpSession session = this.request.request.getSession();
session.setAttribute("ses","会话范畴范畴");
return "success";
}
}
2.8文献上传
2.8.1单文献上传
n 第一步:在WEB-INF/lib下加入commons-fileupload-1.2.2.jar、commons-io-2.0.1.jar
n 第二步:把form表enctype设立为“multipart/form-data”,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/control/fileupload/fileupload_execute.do" method="post">
<input type="file" name = "image">
<input type="submit" value ="上传">
</form>
n 第三步:在action类中添加属性名称
public class FileuploadAction {
private File image;//得到上传文献
private String savePath;//文献保存途径
//struts2默认命名规则:文献属性名称 + FileName/ContentType
private String imageFileName;//得到上传文献名称,
private String imageContentType;//得到上传文献类型
public String execute() throws Exception{
if(image == null)
{
return "fileupload";
}
String realPath = ServletActionContext.getServletContext().getRealPath(savePath);
File saveFile = new File(new File(realPath),imageFileName);
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs();
}
FileUtils.copyFile(image,saveFile);
ActionContext.getContext().put("message","文献上传成功");
return "success";
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
}
n 第四步:配备action属性
//设立上传文献大小为10M,struts2默以为2M
<constant name="struts.multipart.maxSize" value="10485760" />
//配备上传action类
<action name="fileupload_*" class="com.vanceinfo.action.FileuploadAction" method="{1}">
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/massage.jsp</result>
</action>
2.8.2多文献上传
n 第一步:在WEB-INF/lib下加入commons-fileupload-1.2.2.jar、commons-io-2.0.1.jar
n 第二步:把form表enctype设立为“multipart/form-data”,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/control/fileupload/fileupload_execute.do" method="post">
文献1:<input type="file" name = "image">
文献2:<input type="file" name = "image">
文献3:<input type="file" name = "image">
<input type="submit" value ="上传">
</form>
n 第三步:在action类中添加属性名称
public class FileuploadAction {
private File[] image;//得到上传文献
private String savePath;//文献保存途径
//struts2默认命名规则:文献属性名称 + FileName/ContentType
private String[] imageFileName;//得到上传文献名称,
private String[] imageContentType;//得到上传文献类型
public String execute() throws Exception {
if (image == null) {
return "fileupload";
}
String realPath = ServletActionContext.getServletContext().getRealPath(savePath);
System.out.pri
展开阅读全文