J2EE进阶(二)从零开始之Struts2

来源:互联网 发布:床 品牌 知乎 编辑:程序博客网 时间:2024/05/17 03:14

J2EE进阶()从零开始之Struts2

   以前自己总是听说什么SSH框架,不明觉厉。现在自己要重整旗鼓,开始系统性的学习SSH框架了。首先开始Struts2的学习。其实自己之前参与过Struts2项目的开发。有关Struts2的运行原理,详见博客《Struts2进阶()运行原理》一文。

      1.由于Web应用是基于请求/响应架构的应用,所以不管哪个MVC Web框架,都需要在web.xml中配置该框架的核心ServletFilter,这样才可以让该框架介入Web应用中。

      2.数据校验可分为客户端校验和服务器端校验两种、客户端校验和服务器端校验都是必不可少的,二者分别完成不同的过滤。客户端校验进行基本校验,如检验非空字段是否为空,数字格式是否正确等。客户端校验主要用来过滤用户的误操作。客户端校验的作用是:过滤、拒绝正常用户误操作输入提交到服务器处理,降低服务器端负担。服务器端校验也必不可少,是整个应用阻止非法数据的最后防线。服务器端校验防止非法数据进入程序,导致程序异常、底层数据库异常。服务器端校验是保证程序有效运行及数据完整的手段。

      3.客户端校验的主要作用是防止正常浏览者的误输入,仅能对输入进行初步过滤;对于恶意用户的恶意行为,客户端检验将无能为力。因此,客户端检验决不可代替服务器端校验。当然,客户端校验也决不可少,因为Web应用大部分浏览者都是正常的浏览者,他们的输入可能包含了大量的误输入,客户端校验把这些误输入阻止在客户端,从而降低了服务器的负载。

应用

文件上传

      文件上传在项目中经常会用到,下面就来说说struts2中怎么上传文件的:

      1.引入相应的jar包(commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar)

      2.把form的enctype设置为"multipart/form-data",如下所示:

      <form action="<%=basePath%>upload/upload.action" method="post" name="form" enctype="multipart/form-data">  

           文件1:<input type="file" name="upload"/><br/>  

          <input type="submit" value="上传" />  

     </form>  

      3.在action类中添加如下代码中注释的几个属性。

public class HelloWorldAction {      private File upload;//得到上传的文件     private String uploadContentType;//得到上传文件的扩展名     private String uploadFileName;//得到上传文件的名称       public File getUpload() {          return upload;      }      public void setUpload(File upload) {          this.upload = upload;      }      public String getUploadContentType() {          return uploadContentType;      }      public void setUploadContentType(String uploadContentType) {          this.uploadContentType = uploadContentType;      }      public String getUploadFileName() {          return uploadFileName;      }      public void setUploadFileName(String uploadFileName) {          this.uploadFileName = uploadFileName;      }      public String upload() throws IOException {          String realpath = ServletActionContext.getServletContext().getRealPath("/upload");          if(upload != null) {              File savefile = new File(realpath,uploadFileName);              if(!savefile.getParentFile().exists()) {                  savefile.getParentFile().mkdirs();              }              FileUtils.copyFile(upload, savefile);              ActionContext.getContext().put("msg", "文件上传成功!");          }          return "success";      }  }

      注意,如果在上传的过程中文件的大小超过了struts2默认的文件大小的话,就会上传失败,这时候,可以根据具体的情况设置struts.multipart.maxSize的值来满足上传的需求。

多文件上传

     在实际的项目中,有时候可能会要求上传多个文件的情况,下面就来说说上传多个文件的情况。

      1.同上。

      2.form如下所示:

<form action="<%=basePath%>upload/upload" method="post" name="form" enctype="multipart/form-data">      文件1:<input type="file" name="upload"/><br/>      文件2:<input type="file" name="upload"/><br/>      文件3:<input type="file" name="upload"/><br/>      <input type="submit" value="上传" />  </form>

      3.action中添加的几个属性都是数组形式的。

public class HelloWorldAction {      private File[] upload;//得到上传的文件      private String[] uploadContentType;//得到上传文件的扩展名      private String[] uploadFileName;//得到上传文件的名称        public File[] getUpload() {          return upload;      }      public void setUpload(File[] upload) {          this.upload = upload;      }      public String[] getUploadContentType() {          return uploadContentType;      }      public void setUploadContentType(String[] uploadContentType) {          this.uploadContentType = uploadContentType;      }      public String[] getUploadFileName() {          return uploadFileName;      }      public void setUploadFileName(String[] uploadFileName) {          this.uploadFileName = uploadFileName;      }      public String upload() throws IOException {          String realpath = ServletActionContext.getServletContext().getRealPath("/upload");          if(upload != null) {              for(int i=0; i<upload.length; i++) {                  File savefile = new File(realpath,uploadFileName[i]);                  if(!savefile.getParentFile().exists()) {                      savefile.getParentFile().mkdirs();                  }                  FileUtils.copyFile(upload[i], savefile);              }              ActionContext.getContext().put("msg", "文件上传成功!");          }          return "success";      }  }  

自定义拦截器

     自定义拦截器要实现com.opensymphony.xwork2.interceptor.Interceptor接口。下面是一个自定义拦截器的例子:

public class PermissionInterceptor implements Interceptor {      public void destroy() {        }      public void init() {      }      public String intercept(ActionInvocation invocation) throws Exception {          Object user = ActionContext.getContext().getSession().get("user");          if(user != null) {              return invocation.invoke();          } else {              ActionContext.getContext().put("message", "你没有执行权限!");          }          return "success";      }  }  

      接下来,就要在配置文件中注册拦截器,具体的做法是:

<interceptors>      <interceptor name="permission" class="zhchljr.interceptor.PermissionInterceptor"></interceptor>  </interceptors> 

      为action指定拦截器,具体的做法是:

<action name="interceptor" class="zhchljr.action.HelloWorldAction" method="interceptor">      <interceptor-ref name="permission"></interceptor-ref>  </action>

      但是这样做了以后,就会出现一个问题,struts2中为一个action指定拦截器后,默认的defaultStack中的拦截器就不起作用了,也就是说struts2的众多核心功能都使用不了了(struts2的许多核心功能都是通过拦截器实现的),为了解决这个问题,引入拦截器栈,先使用系统默认的拦截器,然后再来使用自定义的拦截器,具体的做法是:

<interceptors>      <interceptor name="permission" class="zhchljr.interceptor.PermissionInterceptor"></interceptor>      <interceptor-stack name="permissionStack">          <interceptor-ref name="defaultStack"></interceptor-ref>          <interceptor-ref name="permission"></interceptor-ref>      </interceptor-stack>  </interceptors>  

      如果希望包下的所有action都使用自定义的拦截器,可以把拦截器设置为默认拦截器,具体的实现方式是:

     <default-interceptor-ref name="permissionStack"></default-interceptor-ref>  

     注意:每个包中只能有一个默认的拦截器;一旦为包中的某个action指定了拦截器,则默认的拦截器就不起作用了。

参考文献

      1.http://blog.csdn.net/dandanzmc/article/details/27355565

     2.《轻量级JavaEE企业应用实战(第三版) Struts2+Spring3+Hibernate整合开发》

美文美图

 


1 1
原创粉丝点击