什么是框架,框架有什么用?
什么是struts2框架,它有什么用?
javaweb
开发中使用的。XWork—它是webwork核心,提供了很多核心功能:
类似于struts2框架的产品 :
Strust2 核心功能
index.jsp——>HelloAction———>hello.jsp struts2流程
Struts2的下载和安装
struts2的目录结构:
war
后缀表示web压缩文件1.导入jar包
注意:在struts2开发,一般情况下最少导入的jar包,去apps
下的struts2-blank
示例程序中copy。将war
后缀改为rar
后解压。
2.创建index.jsp,hello.jsp页面
你好,Struts2
3.对struts2框架进行配置
/*
, 但是struts2 默认处理.action结尾请求,分发到相应Action类 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /*
* 2.创建一个struts.xml配置文件 ,这个是struts2框架配置文件。 * 目的:是为了struts2框架流程可以执行。 * 名称:struts.xml * 位置:src下(classes下)
public class HelloAction { public String say(){ System.out.println("hello world"); return "good"; // 结果页面命名 }}
struts2 的Action类似以前编写的Servlet程序,可以处理用户提交请求,但是Struts2的Action可以POJO对象
5.在struts.xml文件中配置HelloAction
/hello.jsp
第一次使用struts2
struts.xml
1.创建一个Filter—-StrutsFilter
struts cn.itcast.filter.StrutsFilter struts /*
// 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.流程分析
struts-default.xml
定义2.关于手动配置struts.xml文件中提示操作
3.关联struts2源文件
4.使用插件 struts2-config-browser-plugin-2.3.15.1
StrutsPrepareAndExecuteFilter
init_DefaultProperties(); // [1] ---------- org/apache/struts2/default.properties init_TraditionalXmlConfigurations(); // [2] --- struts-default.xml,struts-plugin.xml,struts.xmlinit_LegacyStrutsProperties(); // [3] --- 自定义struts.properties init_CustomConfigurationProviders(); // [5] ----- 自定义配置提供init_FilterInitParameters() ; // [6] ----- web.xml init_AliasStandardObjects() ; // [7] ---- Bean加载
1.default.properties文件
2.struts-default.xml
struts.xml
3.自定义的struts.properties
4.web.xml
在开发中,后加载文件中的配置会将先加载文件中的配置覆盖。
1.
作用:是用于声明一个包。用于管理action。
2
用于声明一个action
execute
函数。3.
用于确定返回结果类型
关于action配置其它细节
1.关于默认值问题
namespace的默认值是
name的默认值是 “success” 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设置认定对应的类。
default.properties 它声明了struts中的常量。
问题:人为设置常量,可以在哪些位置设置 ?
StrutsPrepareAndExecuteFilter的初始化参数
来配置的. struts.action.extension do,,
常用常量(struts.xml)
struts.action.extension=action,,
: 这个常量用于指定strus2框架默认拦截的后缀名.
: 相当于request.setCharacterEncoding(“UTF-8”); 解决post请求乱码
: false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false
: 提供详细报错页面,修改struts.xml后不需要重启服务器 (要求)struts.xml文件的分离:
导入其它的配置文件。有三种方式
1.创建一个POJO类.
在struts2框架底层是通过反射来操作
struts.xml
获得 完整Action类名 2.创建一个类,实现Action接口 com.opensymphony.xwork2.Action
3.创建一个类,继承自ActionSupport类
。com.opensymphony.xwork2.ActionSupport
1.通过设置method的值,来确定访问action类中的哪一个方法.
2.使用通配符来简化配置
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.不建议使用过多的通配符,阅读不方便。
的method属性
!方法名
指定调用Action哪个方法struts.xml没有指定method属性,但product!add.action
就会执行ProductAction的add方法
eg:在struts.xml文件中
* 访问时路径: http://localhost/struts2_day01_2/book!add * 就访问到了BookAction类中的add方法。* 对于`book!add` 这就是动态方法调用。* 注意:struts2框架支持动态方法调用,是因为在`default.properties`配置文件中设置了动态方法调用为**true**. * 第108行 `struts.enable.DynamicMethodInvocation = true`
在struts2中获取servlet api有三种方式
1.通过ActionContext来获取
ActionContext context=ActionContext.getContext();
:返回ActionContext实例对象2.注入方式获取(这种方式是真正的获取到了servlet api)
1.要求action类必须实现指定接口。
2.重定接口中的方法。
public void setServletRequest(HttpServletRequest request) { this.request = request; }
if (action instanceof ServletRequestAware) { //判断action是否实现了ServletRequestAware接口 HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request对象. ((ServletRequestAware) action).setServletRequest(request);//将request对象通过action中重写的方法注入。 }
Action处理请求后, 返回字符串(逻辑视图名), Struts2 根据逻辑视图名,决定响应哪个结果,需要在struts.xml 提供
元素定义结果页面
标签属性
必会: chain dispatcher redirect redirectAction stream
dispatcher
:它代表的是请求转发,也是默认值。它一般用于从action跳转到页面。该结果类型有一个 location 参数, 它是一个默认参数 chain
:它也相当于请求转发。它一般情况下用于从一个action跳转到另一个action。redirect
:它代表的是重定向 它一般用于从action跳转到页面redirectAction
: 它代表的是重定向 它一般用于从action跳转另一个action。 stream
:代表的是服务器端返回的是一个流,一般用于下载。了解: freemarker velocity
/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
//login.jsp ${requestScope["login.message"] }
//success.jsp ${username}
联系客服