写点什么

Struts2 应用详解

  • 2022 年 8 月 21 日
    北京
  • 本文字数:3136 字

    阅读完需:约 10 分钟

Struts2应用详解

一、数据校验

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

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

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

二、文件上传

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

  1. 引入相应的 jar 包(commons-fileupload-1.2.1.jarcommons-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>  
复制代码
  1. 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>
复制代码


  1. 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接口。下面是一个自定义拦截器的例子:

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

<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指定了拦截器,则默认的拦截器就不起作用了。

发布于: 19 小时前阅读数: 64
用户头像

No Silver Bullet 2021.07.09 加入

岂曰无衣 与子同袍

评论

发布
暂无评论
Struts2应用详解_应用_No Silver Bullet_InfoQ写作社区