下载Struts2框架
http://struts.apache.org/download.cgi Struts struts2目录结构 apps 该文件夹包含了基于struts2 的示例应用,这些示例应用对于学习者是非常有用的 docs 该文件夹下包含了struts2 相关文档,包括struts2 快速入门、struts2的文档以及API文档等 lib 该文件夹下包含了Struts2框架和核心类库,以及struts2第三方插件类库 src 该文件夹下包含了Struts2框架的全部源代码配置jar包到当前应用程序下
导入 asm-3.3.jar 操作java字节码的类库 asm-commons-3.3.jar 提供了基于事件的表现形式 asm-tree-3.3.jar 提供了基于对象的表现形式 commons-fileupload-1.3.2.jar 文件上传 commons-io-2.2.jar io包 commons-lang3-3.2.jar 封装了java lang基本到操作 freemarker-2.3.22.jar FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具 javassist-3.11.0.GA.jar 分析、编辑和创建Java字节码的类库。 log4j-api-2.3.jar 输出log log4j-core-2.3.jar 输出log ognl-3.0.19.jar 是支持ognl表达式 struts2-core-2.3.32.jar struts2的核心jar包,不可缺少的 xwork-core-2.3.32.jar xwork的核心包,由于Struts2是由xwork的延伸 有些类依然关联着 xwork的类配置web.xml 根据版本配置–直接copy
<?xml version="1.0" encoding="UTF-8"?> <web-app id="starter" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- Filters --> <!-- START SNIPPET: filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Welcome file lists --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>配置struts.xml
写一个Action类
public class HelloWorldAction extends ActionSupport { @Override public String execute() { System.out.println("被访问了。。。"); return "success"; } }dtd关联
搜索xml,XML catalog
C:\学习\JavaEE第三个月讲课资料\day01\资料\struts-2.3.32\src\core\src\main\resources\2.3.dtd
src下配置struts.xml
普通配置 <!-- 包名随便定义 actio result 指定对应结果的界面 --> <!-- 名称空间跟路径有关系 默认为/ --> <!-- method指定特定方法名称 --> <!-- action name 指定地址栏访问路径 --> <!-- action class 要访问的action的全路径名称 --> <package name="haha" extends="struts-default"> <action name="hello" class="com.example.struts2.HelloWorldAction"> <result name="success"> /struts2_helloworld.jsp </result> </action> </package>第一种 直接写类
public class FirstAction { public String execute() { System.out.println("我是第一种类型的Action"); return "success"; } }第二种 写类继承自Action
public class SecondAction implements Action{ @Override public String execute() { System.out.println("我是第二种类型的Action"); return "success"; } }第三种 写类继承自ActionSupport 重点掌握
public class ThirdAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("我是第三中类型的Action"); return SUCCESS; } }ActionContext
获取Action的环境,可以通过actionContext获取请求参数,session等信息
实现 ServletRequestAware ServletContextAware ServletResponseAware 接口获取Servlet中请求 响应 和 ServletContext信息
ServletActionContext 重点掌握
HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ServletContext servletContext = ServletActionContext.getServletContext(); PageContext pageContext = ServletActionContext.getPageContext();通配符配置
增删改查 <action name="user_*" class="com.example.action.SecondAction" method="{1}">