Struts2 应用详解

一、数据校验

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

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

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

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

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

把 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> 
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 的值来满足上传的需求。
三、多文件上传

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

同上。

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>
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 指定了拦截器,则默认的拦截器就不起作用了。
欢迎关注我的公众号:敲代码的老贾,回复“领取”赠送《Java面试》资料,阿里,腾讯,字节,美团,饿了么等大厂

作者:用户bPc1FjG原文地址:https://segmentfault.com/a/1190000042357223

%s 个评论

要回复文章请先登录注册