Struts2笔记

来源:互联网 发布:阿尔卑斯牧场物语java 编辑:程序博客网 时间:2024/06/05 07:09

Struts2笔记

入门

核心点1. 拦截器intercepto2. Action3. ognl表达式与valueStack基本原理1. 当通过浏览器发送一个请求2. 会被StrutsPrepareAndExecuteFilter拦截3. 会调用struts2框架默认的拦截器(interceptor)完成部分功能4. 在执行Action中操作5. 根据Action中的方法的执行结果来选择来跳转页面Result视图

Struts2配置文件加载顺序

  1. 第一个加载的是default.properties文件,主要声明了struts2框架的常量
  2. 加载的是一批配置文件
    • Struts-default.xml声明了interceptor result bean
    • Struts-plugin.xml用于插件的配置声明
    • Struts.xml用于我们自己工程使用struts2框架的配置
  3. 加载自定义的struts.properties,用于定制常量
  4. 自定义配置提供
  5. 加载web.xml
  6. bean相关配置

struts.xml文件配置

主要有四个标签1. package有四个属性- name属性,就是一个包的名字没啥用- namespace 决定框架会处理来自哪个路径提交的请求- extends 指定继承自哪个包,一般是struts-default,如果用到json传输数据,会是json-default- abstruct 没啥用2. action- name属性 与namespace联合使用来确定一个action的访问路径- class属性 用于指定相应本次请求的当前action类- method属性 用于指定本次请求当前的action类中的哪个方法执行3. result 可以配置一组result通过不同的返回值来实现不同的跳转- name属性 与method的方法的返回值进行匹配,来确定跳转路径- type 属性常见类型 如下- dispatcher 默认值,就是转发,返回页面- chain 转发到一个Action- redirect 重定向到一个URL- redirectAction 重定向到一个Action- strean 向浏览器发送一个流,通常用于文件下载,- json 需要先继承json-default用于异步请求4. constant 用于声明常量,常见的配置如下
    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <!-- 把它设置为开发模式,发布时要设置为false -->    <constant name="struts.devMode" value="true" />    <!-- 设置在class被修改时是否热加载,发布时要设置为false -->    <constant name="struts.convention.classes.reload" value="true"/>    <!-- 自动动态方法的调用,使用这个设置后可以这样调用:action!method -->    <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <!-- 指定jsp文件所在的目录地址 -->    <constant name="struts.convention.result.path" value="/WEB-INF/content/" />    <!-- 使用struts-default默认的转换器,如果是rest的使用:rest-default,rest需要rest的jar插件 -->    <constant name="struts.convention.default.parent.package" value="struts-default"/>    <!-- 用于配置包名后缀。默认为action、actions、struts-->    <constant name="struts.convention.package.locators" value="actions" />    <!-- 用于配置类名后缀,默认为Action,设置后,Struts2只会去找这种后缀名的类做映射 -->    <constant name="struts.convention.action.suffix" value="Action"/>    <!-- 设置即使没有@Action注释,依然创建Action映射。默认值是false。因为Convention-Plugin是约定优于配置的风格,        可以不通过注解根据预先的定义就能访问相应Action中的方法 -->    <constant name="struts.convention.action.mapAllMatches" value="true"/>    <!-- 自定义jsp文件命名的分隔符 -->    <constant name="struts.convention.action.name.separator" value="-" />    <!-- 国际化资源文件名称 -->    <constant name="struts.custom.i18n.resources" value="i18n" />    <!-- 是否自动加载国际化资源文件  -->    <constant name="struts.i18n.reload" value="true" />    <!-- 浏览器是否缓存静态内容 -->    <constant name="struts.serve.static.browserCache" value="false" />     <!-- 上传文件大小限制设置 -->    <constant name="struts.multipart.maxSize" value="-1" />    <!-- 主题,将值设置为simple,即不使用UI模板。这将不会生成额外的html标签 -->    <constant name="struts.ui.theme" value="simple" />    <!-- 编码格式 -->    <constant name="struts.i18n.encoding" value="UTF-8" /></struts>

Action类

可以通过继承ActionSupport来创建Action类可以通过通配符来简化action访问方式
<action name = "book_*" class="BookAction" method={1}</action>

Action类中获取页面请求参数

有两种方式,分别是模型驱动和属性驱动1. 模型驱动- action类只要实现ModelDriven接口,从页面中获取到的参数就会自动赋值给对应的实体2. 属性驱动- 直接在action类中提供与请求参数匹配的set方法,也能直接获取到参数分析:首先框架中有一个类实现了一个拦截器,然后被拦截到,对值栈中分装的参数进行分析,用反射的方式尝试调用参数对应的set方法,如果成功,也就实现了赋值.

获取servletAPI

两种方式- 通过ServletActionContext静态方法获取- 获取request ServletActionContext.getRequest()- 获取response ServletActionContext.getRequest()- 获取servletContext ServletActionContext.getRequest()- 通过实现注入的方法实现- 获取request 实现ServletRequestAware接口可以获取.- 获取response 实现ServletResponseAware接口可以获取- 获取servletContext 实现ServletContextAware接口可以获取.
public class BaseAction implements ServletRequestAware, ServletResponseAware,          StrutsStatics {      protected HttpServletRequest request;      protected HttpServletResponse response;      public void setServletRequest(HttpServletRequest request) {          this.request = request;      }      public void setServletResponse(HttpServletResponse response) {          this.response = response;      }  }  

OGNL表达式与ValueStack

  • 获取ValueStack
//通过ActionContext来获取值栈ValueStack valueStack = ActionContext.getContext().getValueStack();
  • 向ValueStack存储数据
//第一种方法,直接压栈valueStack.push("itcast");//第二种方法,将参数分装到了一个hashMap中valueStack.set("username","tom");
  • 利用OGNL表达式从值栈中获取数据
    • 要使用OGNL表达式,就先要在jsp文件的页首声明
      <%@taglib prefix="s" uri="/struts-tags"%>
    • 如果是push方法存入的数据
      <!-- 根据在值栈中的位置获取 --><s:property value = "[0].top"/>
    • 如果是用set方法存入的数据
      <!-- 根据对应键查找值 --><s:property value = "name"/>

interceptor拦截器

关于拦截器的详细介绍:Struts2拦截器
我们使用拦截器可以在action执行前后进行处理工作,之前的工作可以模拟过滤器的效果,之后可以模拟监听器的效果所有的Interceptor都要实现一个接口com.opensymphony.xwork2.interceptor.Interceptor
接口有三个方法,分别是
  • init()用于创建
  • destory()用于销毁
  • intercept(ActiongInvocation invocation)用于做处理
只需要在应用程序struts.xml文件中通过<include file="struts-default.xml" />将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用<interceptor-ref name="xx" />引用拦截器或拦截器栈(interceptor stack)

Struts2的注解开发

要使用Struts2的注解,我们必须引入一个jar包pom坐标如下
        <dependency>            <groupId>org.apache.struts</groupId>            <artifactId>struts2-convention-plugin</artifactId>            <version>${struts2.version}</version>        </dependency>
常用注解- @Namespace 来代替<package namespace="">- @ParentPackage来代替<package extends="">- @Action 来描述关于<Action>的配置- value用于描述Action的name属性- result用来描述结果类型的配置<result>- type用来描述返回结果的类型相当于type属性- location属性用于替代以前写在Action标签中的返回路径- @Actions的作用是通过多个映射来访问同一个action- @InterceptorRef用于指定拦截器
原创粉丝点击