Struts2学习第三天——拦截器与文件传输

41 篇文章 1 订阅
3 篇文章 0 订阅
文档版本开发工具测试平台工程名字日期作者备注
V1.02016.06.15lutianfeinone


  • 内容摘要:
    1.国际化(了解)
    2.拦截器(Interceptor)—重点
    3.struts2文件上传与下载(次重点)
    4.ognl与valuestack

国际化

  • 国际化原理,什么是国际化 ?

    • 同一款软件 可以为不同用户,提供不同语言界面 —- 国际化软件
    • 需要一个语言资源包(很多properties文件,每个properties文件 针对一个国家或者语言 ,通过java程序根据来访者国家语言,自动读取不同properties文件 )
  • Java原生国家化操作

  • 资源包编写
    • properties文件命名 : 基本名称_语言(小写)_国家(大写).properties
    • 例如 :
      • messages_zh_CN.properties 中国中文
      • messages_en_US.properties 美国英文
  • ResourceBundle 根据不同Locale(地域信息),读取不同国家 properties文件
    ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.US);


struts2中国际化
  • struts2中对国际化进行了封装,我们只需要根据其提供的API进行访问就可以。
问题1:在struts2中国际化时properties文件的定义
  • 1.全局 : 需要通过一个常量来声明
    • 默认全局路径:
    • 第166行:struts.custom.i18n.resources=testmessages,testmessages2
    • 对于properties配置文件可以放置在任意位置
    • struts.xml配置如下:
      • 当message.properties在src下:<constant name="struts.custom.i18n.resources" value="message">
      • 当message.properties在cn.itcast.i18n.resource包下: <constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message">


  • 2.局部
    • 1.针对于action类
      • 位置:与action类在同一个包下.
      • 名称: ActionClassName.properties
      • 这个配置文件只对当前action有效。
    • 2.针对于package下所有action
      • 位置:在指定的包下
      • 名称:package.properties
    • 3.jsp页面临时使用某一个properties文件.
      • 在某个jsp页面中加入 : <s:i18n name="cn.itcast.action.package"></s:i18n>
问题2:Struts2中国际化操作在哪些位置使用
  • 1.action类中使用
  • 2.配置文件中使用<validation.xml>
  • 3.在jsp页面上使用


问题3:Struts2中操作国际化
  • 1.在action类中使用

    • 前提:action类要继承ActionSupport类。
    • getText(String name)就可以获取配置文件中对应名称的值。
  • eg :

// I18nDemo1Action.properties 中 msg=hello world

    public String execute() throws Exception {

        // 得到properties文件中信息.

        System.out.println(this.getText("msg"));
        return NONE;
    }

    //结果就是 hello world


  • 2.在validation.xml文件中使用
    • <message key="名称"/>
    • eg : I18nDemo2Action-validation.xml & xxx.properites
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message key="nameerror"></message>
        </field-validator>

    </field>

</validators>


//properties

//package_en_US.properties
nameerror=name required


//package_zh_CN.properties
nameerror=\u540D\u5B57\u5FC5\u987B\u5730

//package.properties
nameerror=name required
  • 3.在jsp页面上使用

    • <s:text name="名称"> :这里名称为对应的Action名称
    • 如果没有使用<s:i18n name="">来指定,会从全局配置文件中获取。
    • 如果要从某一个配置文件中获取,通过name属性来指定 包名.配置文件名称
  • eg : i18n.jsp


      <s:i18n name="cn.itcast.action.package">
          <s:text name="nameerror"/>
      </s:i18n>


    <s:text name="name" /> //全局包:cn.itcast.i18n.resource
    //由struts.xml中常量配置决定:
    //<constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message"></constant>


* Struts2中国际化配置文件中使用动态文本*
  • 1.action中动态文本使用

    • I18nDemo1Action.properties 中 : msg=hello world {0}
    • I18nDemo1Action 输出: this.getText(“msg”,new String[]{“tom”})
  • eg :

    public String execute() throws Exception {

        // 得到properties文件中信息.

        //System.out.println(this.getText("msg"));

        //动态文本
        System.out.println(this.getText("msg", new String[]{"tom"}));

        return NONE;
    }

    //结果就是 hello world tom


  • 2.jsp页面上怎样使用
    • 在i18n.jsp 中加入 :
        <s:i18n name="cn.itcast.action.I18nDemo1Action">
        <s:text name="msg">
        <s:param>张三</s:param>
        </s:text>
        </s:i18n>
* 结果就是 hello world  张三.


拦截器(interceptor)

  • struts2拦截器使用的是AOP思想
  • AOP的底层实现就是动态代理

  • 拦截器采用 责任链 模式

    • 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。
    • 责任链每一个节点,都可以继续调用下一个节点,也可以阻止流程继续执行
  • struts2中在struts-default.xml文件中声明了所有的拦截器。

  • 而struts2框架默认使用的是defaultStack这个拦截器栈。
  • 在这个拦截器栈中使用了18个拦截器
  • 简单说,struts2框架在默认情况下,加载了18个拦截器
Struts2中使用拦截器
  • 使用拦截器可以做什么?
    • 可以通过使用拦截器进行控制action的访问。例如,权限操作。
拦截器使用方法
  • 1.创建一个Interceptor 可以自定义一个类实现com.opensymphony.xwork2.interceptor.Interceptor在这个接口中有三个方法
    • init
    • destory
    • intercept 真正拦截的方法。
    • 在intercept方法中如果要向下继续执行,通过其参数ActionInvocation调用它的invoke()方法就可以。
public class _MyInterceptor implements Interceptor {
    public void destroy() {
    }

    public void init() {
        System.out.println("my interceptor init");
    }

    public String doIntercept(ActionInvocation ai) throws Exception {

        System.out.println("my interceptor 拦截。。。。。");
        //return ai.invoke(); // 放行
        return Action.LOGIN;  //"login"
    }

}


  • 2.声明一个Interceptor 在struts-default.xml文件中
    • eg:
    <interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
    </interceptor>


  • 3.在action中指定使用哪些拦截器
<interceptors>
    <interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
    </interceptor>
</interceptors>
  • 注意 : 只要显示声明使用了一个拦截器。那么默认的拦截器就不在加载。
    • 如果需要继续使用默认拦截器只需要在struts.xml中设计自己的拦截器栈。
<interceptors>

    <interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
    </interceptor>

    <interceptor name="bookInterceptor" class="cn.itcast.intercept.BookInterceptor">
        <param name="includeMethods">add,update,delete</param>
    </interceptor>

    <interceptor-stack name="myStack">
        <interceptor-ref name="bookInterceptor"></interceptor-ref>
        <interceptor-ref name="defaultStack" />
    </interceptor-stack>
</interceptors>


分析拦截器原理
  • 源代码执行流程:

  • 1.在StrutsPrepareAndExecuteFilter中查找

    • doFilter方法内有一句话 execute.executeAction (request, response, mapping) 执行Action操作.
  • 2.在executeAction执行过程中会访问Dispatcher类中的serviceAction,在这个方法中会创建一个

    • ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, method, extraContext, true, false); 这就是我们的Action的代理对象
  • 3.查看ActionInvocation,查看其实现类 DefaultActionInvocation.

    • 在其invoke方法中
    if (interceptors.hasNext()) {//判断是否有下一个拦截器.
    final InterceptorMapping interceptor = interceptors.next(); //得到一个拦截器
    String interceptorMsg = "interceptor: " + interceptor.getName();
    UtilTimerStack.push(interceptorMsg);
    try {
    resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); 
    //调用得到的拦截器的拦截方法.将本类对象传递到了拦截器中。
    }
    finally {
    UtilTimerStack.pop(interceptorMsg);
    }
    } 
  • 通过源代码分析,发现在DefaultActionInvocation中就是通过递归完成所有的拦截调用操作.


关于interceptor与Filter区别
  • 1、拦截器是基于java反射机制的,而过滤器是基于函数回调的。
  • 2、过滤器依赖于servlet容器,而拦截器不依赖于servlet容器。
  • 3、拦截器只能对Action请求起作用,而过滤器则可以对几乎所有请求起作用。
  • 4、拦截器可以访问Action上下文、值栈里的对象,而过滤器不能。
  • 5、在Action的生命周期中,拦截器可以多次调用,而过滤器只能在容器初始化时被调用一次。
  • interceptor 可以在其他环境中使用,而Filter只能在Web环境中使用。


拦截器案例
  • 1.login.jsp –> LoginAction –> book.jsp
  • 登录成功,将用户存储到session。

  • login.jsp

  <body>
    <s:fielderror/>
    <s:actionerror/>

    <form action="${pageContext.request.contextPath }/login" method="post">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>

        <input type="submit" value="登录">
    </form>
  </body>


  • LoginAction.java
public class LoginAction extends ActionSupport implements ModelDriven<User>{

    private User user = new User();

    public User getModel(){
        return user;
    }
     @Override
    public String execute() throws Exception {

         System.out.println("name: "+user.getUsername());
         System.out.println("password: "+user.getPassword());

        if("miaolu".equals(user.getUsername()) && "123".equals(user.getPassword())){
            ServletActionContext.getRequest().getSession().setAttribute("user", user);
            return SUCCESS;
        }
        else{
            this.addActionError("用户名或密码错误");
            return INPUT;
        }
    }
}


  • book.jsp
  <body>
      <a href="${pageContext.request.contextPath}/book_add">book add</a><br>
      <a href="${pageContext.request.contextPath}/book_update">book update</a><br>
      <a href="${pageContext.request.contextPath}/book_delete">book delete</a><br>
      <a href="${pageContext.request.contextPath}/book_search">book search</a><br>
  </body>


  • 2.在book.jsp中提供crud链接。
  • 每一个连接访问一个BookAction中一个方法。
  • 要求:对于BookAction中的add,update,delete方法要求用户必须登录后才可以访问。search无要求。

  • BookAction.java

public class BookAction extends ActionSupport {


    public String add() throws Exception{
        System.out.println("book action add");

        return null;
    }

    public String update() throws Exception{
        System.out.println("book action update");

        return null;
    }

    public String delete() throws Exception {

        System.out.println("book action delete");
        return null;
    }

    public String search() throws Exception {

        System.out.println("book action search");
        return null;
    }

}


  • 怎样解决只控制action中某些方法的拦截?

    • 1.创建类不在实现Interceptor接口,而是继承其下的一个类.MethodFilterInterceptor
    • 不用在重写intercept方法,而是重写 doIntercept方法。
  • BookInterceptor.java

public class BookInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

        User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");


        if(user == null){
            BookAction action = (BookAction) invocation.getAction();

            action.addActionError("权限不足,请先登录");

            return Action.LOGIN;
        }

        return invocation.invoke();
    }
}


  • 2.在struts.xml文件中声明
<struts>
    <package name="default" namespace="/" extends="struts-default">

        <interceptors>

            <interceptor name="bookInterceptor" class="test.intercept.BookInterceptor">
                <param name="includeMethods">add,update,delete</param>
                <param name="excludeMethods">search</param>
            </interceptor>

            <interceptor-stack name="myStack">
                <interceptor-ref name="bookInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>

        </interceptors>

        <global-results>
            <result name = "login">/login.jsp</result>
        </global-results>

        <action name ="login" class="test.action.LoginAction">
            <result name="input">/login.jsp</result>
            <result>/book.jsp</result>
        </action>


        <action name="book_*" class="test.action.BookAction" method="{1}">
            <interceptor-ref name="myStack"/>
        </action>
    </package>




Struts2中文件上传与下载

文件上传
  • 浏览器端:

    • 1.method=post
    • 2.<input type="file" name="xx">
    • 3.encType=”multipart/form-data”;
  • 服务器端:

    • commons-fileupload组件
      • 1.DiskFileItemFactory
      • 2.ServletFileUpload
      • 3.FileItem
  • struts2中文件上传

    • 默认情况下struts2框架使用的就是commons-fileupload组件.
    • struts2它使用了一个interceptor帮助我们完成文件上传操作。
    • <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
  • 在action中怎样处理文件上传?

    • 页面上组件:<input type="file" name="upload">
  • 在action中要有三个属性:

    • private File upload; 如果是上传单个文件, upload属性的类型就是 java.io.File, 它代表被上传的文件,要与jsp 页面上的 file 标签的名字相同。
    • private String uploadContentType;表示上传文件的类型,格式:file组件的名称+ContentType
    • private String uploadFileName; 表示上传文件的文件名,格式:file组件的名称+FileName
  • 在execute方法中使用commons-io包下的FileUtils完成文件复制.

    • FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));
@Override
    public String execute() throws Exception {
            System.out.println("上传文件的类型:" + uploadContentType);
            System.out.println("上传文件的名称:" + uploadFileName);

            // 完成文件上传.
            FileUtils.copyFile(upload, new File("d:/upload", uploadFileName);
        return null;
    }


  • struts.xml 中的配置
<action name="upload" class="cn.itcast.action.UploadAction">
</action>


关于struts2中文件上传细节
  • 1.关于控制文件上传大小
  • 在default.properties文件中定义了文件上传大小
    • struts.multipart.maxSize=2097152 上传文件默认的总大小 2MB
  • 在struts.xml`中通过修改如下参数,进行修改

    • <constant name="struts.multipart.maxSize" value="20971520"></constant>
  • 2.在struts2中默认使用的是commons-fileupload进行文件上传。

    • struts.multipart.parser=cos : 上传组件(备用)
    • struts.multipart.parser=pell : 上传组件(备用)
    • struts.multipart.parser=jakarta : 默认上传方式
    • 如果使用pell,cos进行文件上传,必须导入其jar包.
  • 3.如果出现问题,需要配置input视图,在页面上可以通过<s:actionerror>展示错误信息.

  • 在页面上展示的英文信息的国际化操作

  • struts-messages.properties 文件里预定义 上传错误信息,通过覆盖对应key 显示中文信息
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
  • 上述信息在自己新建的properties文件修改为
struts.messages.error.uploading=上传错误: {0}
struts.messages.error.file.too.large=上传文件太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} "{1}" "{2}" {3}
  • {0}:<input type=“file” name=“uploadImage”>中name属性的值
  • {1}:上传文件的真实名称
  • {2}:上传文件保存到临时目录的名称
  • {3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)


  • 4.关于多文件上传时的每个上传文件大小控制以及上传文件类型控制
    • 1.多文件上传
      • 服务器端 : 只需要将action属性声明成List集合或数组就可以。
public class UploadAction extends ActionSupport {

    // 在action类中需要声明三个属性
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;

    @Override
    public String execute() throws Exception {

        for (int i = 0; i < upload.size(); i++) {


            System.out.println("上传文件的类型:" + uploadContentType.get(i));
            System.out.println("上传文件的名称:" + uploadFileName.get(i));

            // 完成文件上传.
            FileUtils.copyFile(upload.get(i), new File("d:/upload", uploadFileName.get(i)));
        }
        return null;
    }

}


  • 2.怎样控制每一个上传文件的大小以及上传文件的类型?
    • 在fileupload拦截器中,通过其属性进行控制.
      • maximumSize—每一个上传文件大小
      • allowedTypes–允许上传文件的mimeType类型.
      • allowedExtensions–允许上传文件的后缀名.
    • 在自定义拦截器配置中加入下代码:
        <interceptor-ref name="defaultStack">
        <param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
        </interceptor-ref>
  • eg:
        <action name="upload" class="cn.itcast.action.UploadAction">
            <result name="input">/upload.jsp</result>
            <interceptor-ref name="defaultStack">
                <param name="maximumSize">2097152</param>
                <param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
            </interceptor-ref>
        </action>


文件下载
  • 文件下载方式:
    • 1.超连接
    • 2.服务器编码,通过流向客户端写回。
  • 过程:

    • 1.通过response设置 response.setContentType(String mimetype);
    • 2.通过response设置 response.setHeader(“Content-disposition;filename=xxx”);
    • 3.通过response获取流,将要下载的信息写出。
  • struts2中文件下载

    • 通过<result type="stream">完成。
    • <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>在StreamResult类中有三个属性:
      • protected String contentType = "text/plain"; : 用于设置下载文件的mimeType类型
        • protected String contentDisposition = "inline"; : 用于设置进行下载操作以及下载文件的名称
        • protected InputStream inputStream; : 用于读取要下载的文件。
  • eg:

<action name="download" class="cn.itcast.action.DownloadAction">
    <result type="stream">
        <param name="contentType">${contentType}</param> <!-- 调用当前action中的getContentType()方法 -->
        <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
        <param name="inputStream">${inputStream}</param><!-- 调用当前action中的getInputStream()方法 -->
    </result>
</action>
  • 在action类中定义一个方法
public InputStream getInputStream() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:/upload/" + filename);
return fis;
}


  • 问题1: <a href="${pageContext.request.contextPath}/download?filename=捕获.png">捕获.png</a>下载报错
    • 原因:超连接是get请求,并且下载的文件是中文名称,乱码。
    public InputStream getInputStream() throws FileNotFoundException,
            UnsupportedEncodingException {

        filename = new String(filename.getBytes("iso8859-1"), "utf-8"); // 解决中文名称乱码.

        FileInputStream fis = new FileInputStream("d:/upload/" + filename);
        return fis;
    }


  • 问题2:下载捕获文件时,文件名称是a.txt

    • 下载文件后缀名应该是png,而我们在配置文件中规定却是txt?
  • struts.xml 修订:

<result type="stream">
<param name="contentType">${contentType}</param> <!-- 调用当前action中的getContentType()方法 -->
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
<param name="inputStream">${inputStream}</param><!-- 调用当前action中的getInputStream()方法 -->
</result>
// 设置下载文件mimeType类型
    public String getContentType() {

        String mimeType = ServletActionContext.getServletContext().getMimeType(
                filename);
        return mimeType;
    }



// 获取下载文件名称
public String getDownloadFileName() throws UnsupportedEncodingException {

    return DownloadUtils.getDownloadFileName(ServletActionContext
            .getRequest().getHeader("user-agent"), filename);

}
  • 在struts2中进行下载时,如果使用<result type="stream">它有缺陷
    • 例如:下载点击后,取消下载,服务器端会产生异常。
    • 在开发中,解决方案:可以下载一个struts2下载操作的插件,它解决了stream问题。




OGNL与ValueStack

OGNL表示式使用
  • OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目。 Struts2框架使用OGNL作为默认的表达式语言。
    • xwork 提供 OGNL表达式
    • ognl-3.0.5.jar
  • OGNL 是一种比EL 强大很多倍的语言
OGNL 提供五大类功能
  • 1、支持对象方法调用,如xxx.doSomeSpecial();
  • 2、支持类静态的方法调用和值访问
  • 3、访问OGNL上下文(OGNL context)和ActionContext; (重点 操作ValueStack值栈 )
  • 4、支持赋值操作和表达式串联
  • 5、操作集合对象。

  • 使用OGNL访问 对象方法 和 静态方法

    • OGNL 在jsp 结合 struts2 标签库 使用 , <s:property value="ognl表达式" /> 执行 ognl表达式
    • 调用 实例方法 : 对象.方法() —-> <s:property value="'hello,world'.length()"/>
    • 调用 静态方法 : @[类全名(包括包路径)]@[方法名] —> <s:property value="@java.lang.String@format('您好,%s','小明')"/>
    • 使用 静态方法调用 必须 设置 struts.ognl.allowStaticMethodAccess=true
  • eg : 对象调方法

    public static void main(String[] args) throws OgnlException {
        // ognl可以通过对象调用方法.

        // System.out.println("aaa".length());

        // 使用ognl来完成上面操作.

        // 1.创建一个ognl上下文。
        OgnlContext context = new OgnlContext();

        Object obj1 = Ognl.getValue("'aaa'.length()", context.getRoot());

        System.out.println(obj1);

    }


  • 静态方法
    public static void main(String[] args) throws OgnlException {
        // ognl可以通过对象调用方法.

        System.out.println(Math.max(10, 20));
        System.out.println(Math.PI);
        // 使用ognl来完成上面操作.

        // 1.创建一个ognl上下文。
        OgnlContext context = new OgnlContext();

        Object obj1 = Ognl.getValue("@java.lang.Math@max(10,20)", context.getRoot());

        System.out.println(obj1);

        Object obj2= Ognl.getValue("@java.lang.Math@PI", context.getRoot());

        System.out.println(obj2);

    }


  • 访问OGNL上下文(OGNL context)和ActionContext
    public static void main(String[] args) throws OgnlException {

        // 创建一个ognl上下文
        OgnlContext context = new OgnlContext(); // 本质上就是一个Map集合.

        Person p = new Person();
        p.setName("张三");

        Dog dog = new Dog();
        dog.setName("lucy");

        p.setDog(dog); //张三有条狗叫lucy

        context.setRoot(p);

        Dog dog1 = new Dog();
        dog1.setName("豆豆");

        Person pp=new Person();
        pp.setName("james");
        dog1.setP(pp);

        context.put("dog", dog1);

        context.put("name", "tom");

        // 使用 ognl来获取根中数据 获取根中数据,不需要加#
        Object name1 = Ognl.getValue("name", context, context.getRoot());

        System.out.println(name1);

        // 使用ognl来获取非根中的数据 获取非根中数据,需要使用#

        Object name2 = Ognl.getValue("#name", context, context.getRoot());

        System.out.println(name2);

        //获取出张三的的狗的名称
        //张三是root中故直接指向根张三
        Object name3 = Ognl.getValue("dog.name", context, context.getRoot());

        System.out.println(name3);

        //豆豆的主人名称
        Object name4=Ognl.getValue("#dog.p.name", context, context.getRoot());

        System.out.println(name4);


    }
}


//运行结果:
    //张三
    //tom
    //lucy
    //james


* OGNL上下文(OGNL context)对象 值栈 ValueStack *

  • 什么是值栈 ValueStack ?

    • ValueStack 是 struts2 提供一个接口,实现类 OgnlValueStack —- 值栈对象 (OGNL是从值栈中获取数据的 )
    • 每个Action实例都有一个ValueStack对象 (一个请求 对应 一个ValueStack对象 )
    • 在其中保存当前Action 对象和其他相关对象 (值栈中 是有Action 引用的 )
    • Struts 框架把 ValueStack 对象保存在名为 “struts.valueStack” 的请求属性中,request中 (值栈对象 是 request一个属性)
  • 值栈的内部结构 ?

    • 值栈由两部分组成
      • ObjectStack: Struts 把动作和相关对象压入 ObjectStack 中–List
      • ContextMap: Struts 把各种各样的映射关系(一些 Map 类型的对象) 压入 ContextMap 中,Struts 会把下面这些映射压入 ContextMap 中
        • parameters: 该 Map 中包含当前请求的请求参数
        • request: 该 Map 中包含当前 request 对象中的所有属性
        • session: 该 Map 中包含当前 session 对象中的所有属性
        • application:该 Map 中包含当前 application 对象中的所有属性
        • attr: 该 Map 按如下顺序来检索某个属性: request, session, application
  • ValueStack中 存在root属性 (CompoundRoot) 、 context 属性OgnlContext

    • CompoundRoot 就是ArrayList
    • OgnlContext 就是 Map
  • context 对应Map 引入 root对象

    • context中还存在 request、 session、application、 attr、 parameters 对象引用
    • OGNL表达式,访问root中数据时 不需要 #, 访问 request、 session、application、 attr、 parameters 对象数据 必须写 #
    • 操作值栈 默认指 操作 root 元素
  • 值栈对象的创建 ,ValueStack 和 ActionContext 是什么关系 ?

    • 值栈对象 是请求时 创建的
    • doFilter中 prepare.createActionContext(request, response);
      • 创建ActionContext 对象过程中,创建 值栈对象ValueStack
      • ActionContext对象 对 ValueStack对象 有引用的 (在程序中 通过 ActionContext 获得 值栈对象 )
    • Dispatcher类 serviceAction 方法中 将值栈对象保存到 request范围
      • request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
  • 获得值栈对象的两种方法

    • ValueStack valueStack = (ValueStack) ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
    • ValueStack valueStack2 = ActionContext.getContext().getValueStack();
  • 向值栈保存数据 (主要针对 root)的两种方式

    • 将数据保存root的索引0位置,放置到第一个元素
      • ArrayList add(0,element);
      • valueStack.push(“itcast”);
    • 在值栈创建参数map, 将数据保存到map中
      • valueStack.set(“company”, “传智播客”);
    • 在jsp中 通过 <s:debug /> 查看值栈的内容
  • 在JSP中获取值栈的数据

    • 访问root中数据 不需要#
    • 访问 其它对象数据 加 #
  • 通过下标获取root中对象

    • <s:property value="[0].top"/> //取值栈顶对象
  • 直接在root中查找对象属性 (自上而下自动查找)

    • valueStack:<s:property value="username"/>
  • 在OgnlContext中获取数据

request:<s:property value="#request.username"/>
session:<s:property value="#session.username"/>
application:<s:property value="#application.username"/>
attr:<s:property value="#attr.username"/>
parameters:<s:property value="#parameters.cid[0]"/>
Struts2属性文件详解 struts.configuration 该属性指定加载Struts 2配置文件的配置文件管理器.该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现自己的配置管理器,开发者则可以实现一个实现Configuration接口的类,该类可以自己加载Struts 2配置文件. struts.locale 指定Web应用的默认Locale. struts.i18n.encoding 指定Web应用的默认编码集.该属性对于处理中文请求参数非常有用,对于获取中文请求参数值,应该将该属性值设置为GBK或者GB2312; 提示: 当设置该参数为GBK时,相当于调用HttpServletRequest的setCharacterEncoding方法. struts.objectFactory 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring. struts.objectFactory.spring.autoWire 指定Spring框架的自动装配模式, 该属性的默认值是name, 即默认根据Bean的name属性自动装配. struts.objectFactory.spring.useClassCache 该属性指定整合Spring框架时,是否缓存Bean实例,该属性只允许使用true和false两个属性值,它的默认值是true.通常不建议修改该属性值. struts.objectTypeDeterminer 该属性指定Struts 2的类型检测机制, 通常支持tiger和notiger两个属性值. struts.multipart.parser 该属性指定处理multipart/form-data的MIME类型(文件上传)请求的框架,该属性支持cos,pell和jakarta等属 性值, 即分别对应使用cos的文件上传框架,pell上传及common-fileupload文件上传框架.该属性的默认值为jakarta. 注意: 如果需要使用cos或者pell的文件上传方式,则应该将对应的JAR文件复制到Web应用中.例如,使用cos上传方式,则需要自己下载cos框架的JAR文件,并将该文件放在WEB-INF/lib路径下. struts.multipart.saveDir 该属性指定上传文件的临时保存路径,该属性的默认值是javax.servlet.context.tempdir. struts.multipart.maxSize 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数. struts.custom.properties 该属性指定Struts 2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖struts.properties文件中指定的属性.如果需要加载多个自定义属性文件,多个自定义属性文件的文件名以英文逗号(,)隔开. struts.mapper.class 指定将HTTP请求映射到指定Action的映射器,Struts 2提供了默认的映射器: org.apache.struts2.dispatcher.mapper.DefaultActionMapper.默认映射器根据请求的前缀与 Action的name属性完成映射. struts.action.extension 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理.如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开. struts.serve.static 该属性设置是否通过JAR文件提供静态内容服务,该属性只支持true和false属性值,该属性的默认属性值是true. struts.serve.static.browserCache 该属性设置浏览器是否缓存静态内容.当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false. struts.enable.DynamicMethodInvocation 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true.如果需要关闭动态方法调用,则可设置该属性为false. struts.enable.SlashesInActionNames 该属性设置Struts 2是否允许在Action名中使用斜线,该属性的默认值是false.如果开发者希望允许在Action名中使用斜线,则可设置该属性为true. struts.tag.altSyntax 该属性指定是否允许在Struts 2标签中使用表达式语法,因为通常都需要在标签中使用表达式语法,故此属性应该设置为true,该属性的默认值是true. struts.devMode 该属性设置Struts 2应用是否使用开发模式.如果设置该属性为true,则可以在应用出错时显示更多、更友好的出错提示.该属性只接受true和flase两个值,该属性的 默认值是false.通常,应用在开发阶段,将该属性设置为true,当进入产品发布阶段后,则该属性设置为false. struts.i18n.reload 该属性设置是否每次HTTP请求到达时,系统都重新加载资源文件(允许国际化文件重载).该属性默认值是false.在开发阶段将该属性设置为true会更有利于开发,但在产品发布阶段应将该属性设置为false. 提示: 开发阶段将该属性设置了true,将可以在每次请求时都重新加载国际化资源文件,从而可以让开发者看到实时开发效果;产品发布阶段应该将该属性设置为false,是为了提供响应性能,每次请求都需要重新加载资源文件会大大降低应用的性能. struts.ui.theme 该属性指定视图标签默认的视图主题,该属性的默认值是xhtml. struts.ui.templateDir 该属性指定视图主题所需要模板文件的位置, 该属性的默认值是template,即默认加载template路径下的模板文件. struts.ui.templateSuffix 该属性指定模板文件的后缀,该属性的默认属性值是ftl.该属性还允许使用ftl、vm或jsp,分别对应FreeMarker、Velocity和JSP模板. struts.configuration.xml.reload 该属性设置当struts.xml文件改变后,系统是否自动重新加载该文件.该属性的默认值是false. struts.velocity.configfile 该属性指定Velocity框架所需的velocity.properties文件的位置.该属性的默认值为velocity.properties. struts.velocity.contexts 该属性指定Velocity框架的Context位置,如果该框架有多个Context,则多个Context之间以英文逗号(,)隔开. struts.velocity.toolboxlocation 该属性指定Velocity框架的toolbox的位置. struts.url.http.port 该属性指定Web应用所在的监听端口.该属性通常没有太大的用户,只是当Struts 2需要生成URL时(例如Url标签),该属性才提供Web应用的默认端口. struts.url.https.port 该属性类似于struts.url.http.port属性的作用,区别是该属性指定的是Web应用的加密服务端口. struts.url.includeParams 该属性指定Struts 2生成URL时是否包含请求参数.该属性接受none、get和all三个属性值,分别对应于不包含、仅包含GET类型请求参数和包含全部请求参数. struts.custom.i18n.resources 该属性指定Struts 2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开. struts.diSPAtcher.parametersWorkaround 对于某些Java EE服务器,不支持HttpServlet Request调用getParameterMap()方法,此时可以设置该属性值为true来解决该问题.该属性的默认值是false.对于 WebLogic、Orion和OC4J服务器,通常应该设置该属性为true. struts.freemarker.manager.classname 该属性指定Struts 2使用的FreeMarker管理器.该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是Struts 2内建的FreeMarker管理器. struts.freemarker.wrapper.altMap 该属性只支持true和false两个属性值,默认值是true.通常无需修改该属性值. struts.xslt.nocache 该属性指定XSLT Result是否使用样式表缓存.当应用处于开发阶段时,该属性通常被设置为true;当应用处于产品使用阶段时,该属性通常被设置为false. struts.configuration.files 该属性指定Struts 2框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开.该属性的默认值为struts- default.xml,struts-plugin.xml,struts.xml,看到该属性值,所以应该明白为什么Struts 2框架默认加载struts.xml文件了.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值