一、数据校验
因为 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面试》材料,阿里,腾讯,字节,美团,饿了么等大厂