struts2.1.8

来源:互联网 发布:美工需要掌握什么 编辑:程序博客网 时间:2024/04/29 07:34

开发Struts2应用依赖的jar文件

struts2-core-2.x.x.jar :Struts 2框架的核心类库

xwork-core-2.x.x.jar :XWork类库,Struts 2在其上构建

ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性

freemarker-2.3.x.jar :Struts 2UI标签的模板使用FreeMarker编写

commons-logging-1.x.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4JJDK 1.4+的日志记录。

commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后必须加入此文件

 

Struts2应用的配置文件.

Struts2默认的配置文件为struts.xml ,该文件需要存放在WEB-INF/classes

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

</struts>

 

Struts2web中的启动配置

struts2中,struts框架是通过Filter启动的。他在web.xml中的配置如下:

<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>

struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件

 

Struts.xml配置中的包的介绍

<package name="itcast" namespace="/test" extends="struts-default">

        <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >

                <result name="success">/WEB-INF/page/hello.jsp</result>

        </action>

 </package>

配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,如果其他包要继承该包,必须通过该属性进行引用。

 

包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下Action的路径的一部分,如访问上面例子的Action,访问路径为:/test/helloworld.action

namespace属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串)。

 

通常每个包都应该继承struts-default包, 因为Struts2很多核心的功能都是拦截器来实现。struts-default定义了这些拦截器和Result类型。

当包继承了struts-default才能使用struts2提供的核心功能。

struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。

struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。

 

包还可以通过abstract=“true”定义为抽象包,抽象包中不能包含action

 

访问struts2actionURL路径由两部份组成

包的命名空间+action的名称,例如访问本例子HelloWorldActionURL路径为:/test/helloworld,另外我们也可以加上.action后缀访问此Action

可以使用EL表达式访问Action中的属性。

 

Action名称的搜索顺序

 

Action配置中的各项默认值

1>如果没有为action指定class,默认是ActionSupport

2>如果没有为action指定method,默认执行action中的execute() 方法。

3>如果没有指定resultname属性,默认值为success

 

Actionresult的各种转发类型

常用的类型有: dispatcher(默认值) redirect redirectAction plainText

result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。如下:

<result type="redirect">/view.jsp?id=${id}</result>

下面是redirectAction 结果类型的例子,如果重定向的action中同一个包下:

<result type="redirectAction">helloworld</result>

如果重定向的action在别的命名空间下:

<result type="redirectAction">

        <param name="actionName">helloworld</param>

        <param name="namespace">/test</param>

</result>

 

redirect   
   
重定向到一个URL ,被跳转的页面中丢失传递的信息,如
request  
    org.apache.struts2.dispatcher.ServletRedirectResult   
  
redirectAction   
   
重定向到一个Action ,跳转的页面中丢失传递的信息,如
request     
    org.apache.struts2.dispatcher.ServletActionRedirectResult   


redirect-action     
   
重定向到一个Action ,跳转的页面中丢失传递的信息,如request     
   org.apache.struts2.dispatcher.ServletActionRedirectResult

注:redirectredirect-action区别

一、使用redirect需要后缀名 使用redirect-action不需要后缀名
二、type="redirect"的值可以转到其它命名空间下的action,redirect-action只能转到同一命名空下的 action,因此它可以省略.action的后缀直接写action的名称。

 

多个Action共享一个视图--全局result配置

<package ....>

        <global-results>

                <result name="message">/message.jsp</result>

        </global-results>

</package>

 

Action的属性注入值

Struts2Action中的属性提供了依赖注入功能。属性必须提供setter方法。

public class HelloWorldAction{

        private String savePath;

        public String getSavePath() {

                return savePath;

        }

        public void setSavePath(String savePath) {

                this.savePath = savePath;

        }

       ......

}

<package name="itcast" namespace="/test" extends="struts-default">

        <action name="helloworld" class="cn.itcast.action.HelloWorldAction" >

                <param name="savePath">/images</param>

                <result name="success">/WEB-INF/page/hello.jsp</result>

        </action>

</package>

获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象

方法一,通过ServletActionContext.类直接获取:

                HttpServletRequest request = ServletActionContext.getRequest();

                HttpServletResponse response = ServletActionContext.getResponse();

                HttpSession session = request.getSession();

                ServletContext servletContext = ServletActionContext.getServletContext();

方法二,实现指定接口,struts框架运行时注入:

public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{

private HttpServletRequest request; private ServletContext servletContext; private HttpServletResponse response;

        public void setServletRequest(HttpServletRequest req) {

                this.request=req;

        }

        public void setServletResponse(HttpServletResponse res) {

                this.response=res;

        }

        public void setServletContext(ServletContext ser) {

                this.servletContext=ser;

        }

        Struts2文件上传           

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar

 

第二步:把form表的enctype设置为:“multipart/form-data“ method=”post”

 

第三步:在Action类中添加以下属性, 属性红色部分对应于表单中文件字段的名称(name=” uploadImages”).提供GET/SET方法。

 

public class HelloWorldAction{

  private File[]  uploadImages;//得到上传的文件

  private String[]  uploadImagesContentType;//得到文件的类型

  private String[]  uploadImagesFileName;//得到文件的名称

  //这里略省了属性的getter/setter方法

public String upload() throws Exception{

String realpath = ServletActionContext.getServletContext().getRealPath("/images");

      File file = new File(realpath);

      if(!file.exists()){

 file.mkdirs();

      }

      for(int i=0 ;i<uploadImages.length; i++){

File uploadImage = uploadImages[i];

        FileUtils.copyFile(uploadImage, new File(file, uploadImagesFileName[i]));

}

     return "success";

  }}

}