struts2快速入门
- index.jsp——>HelloServlet——–>hello.jsp web开发流程.
index.jsp——>HelloAction———>hello.jsp struts2流程
Struts2的下载和安装
struts2的目录结构:
- apps: 该文件夹包含了基于struts2 的示例应用,这些示例应用对于学习者是非常有用的;例子程序
war 后缀表示web压缩文件
- docs : 该文件夹下包含了struts2 相关文档,包括struts2 快速入门、struts2的文档以及API文档等
- lib : 该文件夹下包含了Struts2框架和核心类库,以及struts2第三方插件类库
- 开发时没必要将lib目录下jar文件全部复制到项目中
- src : 该文件夹下包含了Struts2框架的全部源代码
- core 它是struts2的源代码
- xwork-core struts2底层使用了xwork,xwork的源代码
1.导入jar包
- 下载struts2的jar包 struts-2.3.15.1-all 版本.
- Struts运行必要jar包
- struts2-core-2.3.1.1.jar:Struts 2框架的核心类库
- xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基于xwork
- ognl-3.0.3.jar:对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
- freemarker-2.3.18.jar:Struts 2的UI标签的模板使用FreeMarker编写
- commons-logging-1.1.x.jar:ASF出品的日志包,Struts 2框架使用这个日志,包来支持Log4J和JDK 1.4+的日志记录。
- commons-fileupload-1.2.2.jar: 文件上传组件,2.1.6版本后需要加入此文件
- commons-io-2.0.1.jar:传文件依赖的jar包
- commons-lang-2.5.jar:对java.lang包的增强
- 开发中为了方便导入,可以使用app/struts2-blank.war 携带jar包
注意:在struts2开发,一般情况下最少导入的jar包,去apps 下的struts2-blank 示例程序中copy。将war 后缀改为rar 后解压。
2.创建index.jsp,hello.jsp页面
- 在index.jsp (发起请求页面)
- hello.jsp (结果页面)
- 结果页面显示 struts2框架访问成功
3.对struts2框架进行配置
- 1.web.xml文件中配置前端控制器(核心控制器)—–就是一个Filter
- 目的:是为了让struts2框架可以运行。
- 过滤器配置
/* , 但是struts2 默认处理.action结尾请求,分发到相应Action类
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
* 2.创建一个struts.xml配置文件 ,这个是struts2框架配置文件。
* 目的:是为了struts2框架流程可以执行。
* 名称:struts.xml
* 位置:src下(classes下)
- 4.创建一个HelloAction类
- 要求,在HelloAction类中创建一个返回值是String类型的方法,注意,无参数。
public class HelloAction {
public String say(){
System.out.println("hello world");
return "good"; // 结果页面命名
}
}
/hello.jsp
Struts2 处理流程
模仿struts2流程完成入门程序
struts
cn.itcast.filter.StrutsFilter
struts
/*
- 3.在StrutsFilter中完成拦截操作,并访问Action中的方法,跳转到hello.jsp页面操作.
// 2.1 得到请求资源路径
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String path = uri.substring(contextPath.length() + 1);
// System.out.println(path); // hello
// 2.2 使用path去struts.xml文件中查找某一个这个标签
SAXReader reader = new SAXReader();
// 得到struts.xml文件的document对象。
Document document = reader.read(new File(this.getClass()
.getResource("/struts.xml").getPath()));
Element actionElement = (Element) document
.selectSingleNode("//action[@name='" + path + "']"); // 查找这样的标签
if (actionElement != null) {
// 得到标签上的class属性以及method属性
String className = actionElement.attributeValue("class"); // 得到了action类的名称
String methodName = actionElement.attributeValue("method");// 得到action类中的方法名称。
// 2.3通过反射,得到Class对象,得到Method对象
Class actionClass = Class.forName(className);
Method method = actionClass.getDeclaredMethod(methodName);
// 2.4 让method执行.
String returnValue = (String) method.invoke(actionClass
.newInstance()); // 是让action类中的方法执行,并获取方法的返回值。
// 2.5
// 使用returnValue去action下查找其子元素result的name属性值,与returnValue做对比。
Element resultElement = actionElement.element("result");
String nameValue = resultElement.attributeValue("name");
if (returnValue.equals(nameValue)) {
// 2.6得到了要跳转的路径。
String skipPath = resultElement.getText();
// System.out.println(skipPath);
request.getRequestDispatcher(skipPath).forward(request,
response);
return;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
struts2的流程分析以及工具配置
struts2配置(重点)
1.struts2配置文件加载顺序
- struts2框架要能执行,必须先加载
StrutsPrepareAndExecuteFilter
- 在StrutsPrepareAndExecuteFilter的init方法中对Dispatcher进行了初始化.
- 在Dispatcher类中定义的init方法内就描述了struts2配置文件加载的顺序
init_DefaultProperties(); // [1] ---------- org/apache/struts2/default.properties
init_TraditionalXmlConfigurations(); // [2] --- struts-default.xml,struts-plugin.xml,struts.xml
init_LegacyStrutsProperties(); // [3] --- 自定义struts.properties
init_CustomConfigurationProviders(); // [5] ----- 自定义配置提供
init_FilterInitParameters() ; // [6] ----- web.xml
init_AliasStandardObjects() ; // [7] ---- Bean加载
2.关于Action的配置
/hello.jsp
* 此时输入: http://localhost/struts2_day01_2/a/b/c/hello 也访问到了action。
* 原因:struts2中的action被访问时,它会首先查找
* 1.namespace="/a/b/c" action的name=hello 没有.
* 2.namespace="/a/b action的name=hello 没有
* 3.namespace="/a" action的name=hello 没有
* 4.namespace="/" action的name=hello 查找到了.
* 如果最后也查找不到,会报404错误.
* 3.默认的action。
* 作用:处理其它action处理不了的路径。
* ``
* 当访问的路径,其它的action处理不了时,就会执行name指定的名称的action。
* 4.action的默认处理类
* 在action配置时,如果class不写。默认情况下是 com.opensymphony.xwork2.ActionSupport
* ``
* 如上设置,在当前包下,默认处理action请求的处理类就为class指定的类。即:当``中class省略时,按照default-class-ref中的class设置认定对应的类。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
关于常量配置
struts.action.extension
do,,
常用常量(struts.xml)
struts.action.extension=action,, : 这个常量用于指定strus2框架默认拦截的后缀名.
: 相当于request.setCharacterEncoding(“UTF-8”); 解决post请求乱码
: false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false
: 提供详细报错页面,修改struts.xml后不需要重启服务器 (要求)
struts.xml文件的分离:
- 目的:就是为了阅读方便。可以让一个模块一个配置文件,在struts.xml文件中通过
导入其它的配置文件。
Action
关于Action类的创建方式介绍
关于action的访问方式
book add
book update
book delete
book search
* 当访问book add时,这时的路径是 Book_add,那么对于struts.xml文件中.
* `*_*`代表匹配两个字符串
* {1} 匹配UserAction 用于执行class
* {2} 匹配login用于指定method执行方法 和结果页面
* 第一个星就是 Book
* 第二个星就是 add
* 对于{1}Action---->BookAction
* 对于method={2}--->method=add
* 使用通配符来配置注意事项:
* 1.必须定义一个统一的命名规范。
* 2.不建议使用过多的通配符,阅读不方便。
- 3.动态方法调用 (了解)
- 通过url动态指定调用Action哪个方法而无需配置
的method属性
- 通过
!方法名 指定调用Action哪个方法
* 访问时路径: http://localhost/struts2_day01_2/book!add
* 就访问到了BookAction类中的add方法。
* 对于`book!add` 这就是动态方法调用。
* 注意:struts2框架支持动态方法调用,是因为在`default.properties`配置文件中设置了动态方法调用为**true**.
* 第108行 `struts.enable.DynamicMethodInvocation = true`
在struts2框架中获取servlet API
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
- 扩展:分析其实现:
- 是使用struts2中的一个interceptor完成的.
if (action instanceof ServletRequestAware) { //判断action是否实现了ServletRequestAware接口
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request对象.
((ServletRequestAware) action).setServletRequest(request);//将request对象通过action中重写的方法注入。
}
- 3.通过ServletActionContext获取.
- 该方案可避免Action类实现XxxAware接口,但Action依然与Servlet API直接耦合
- 开发中优先使用ActionContext 这样可以避免耦合
- 在ServletActionContext中方法都是static。
- ServletActionContext.getRequest() : 获得request对象 (session)
- ServletActionContext.getResponse() : 获得response 对象
- ServletActionContext.getServletContext() : 获得ServletContext对象
- 静态方法没有线程问题,ThreadLocal
Result结果类型
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
/demo6/result.jsp
/demo6/result.jsp
作业
struts.xml
/login.jsp
/success.jsp
/login.jsp
/success.jsp
/login1.jsp
/success.jsp
/login2.jsp
/success.jsp
/login3.jsp
/success.jsp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
//login.jsp
${requestScope["login.message"] }
//success.jsp
${username}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
|