Struts2 应用详解 struts2视频教程
yuyutoo 2024-12-19 17:36 9 浏览 0 评论
一、数据校验
由于 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 指定了拦截器,则默认的拦截器就不起作用了。
相关推荐
- 网络规划建设原来也可以这么简单!
-
废话少说,直接上干货。天气炎热,请各位看官老爷静心阅读。整体思路下图是关于网络建设的所有相关领域,接下来我为大家逐一讲解。网络分层...
- 网络规划设计师笔记-第 1 章 计算机网络原理
-
计算机网络原理1.1计算机网络概论(P1-10)...
- 别输在远见上,网工这样做职业规划,比啥都强
-
01职业中的规划,人生中的buff“职业规划“这个词,其实对很多年轻人,包括曾经年轻的我来说,都不屑一提。...
- 网络规划设计师学习中(个人自学笔记分享1),有一起学习的吗?
-
网络规划设计师,上午考试内容学习:第一章:计算机网络概述(上部分):如果你也在一起学习,那么我们来一起学习吧!坚持1年,争取明年一次性通过!...
- 在微服务中使用 ASP.NET Core 实现事件溯源和 CQRS
-
概述:事件溯源和命令查询责任分离(CQRS)已成为解决微服务设计的复杂性的强大架构模式。基本CQRS表示形式在本文中,我们将探讨ASP.NETCore如何使你能够将事件溯源和CQRS...
- 用 Nginx 部署 ASP.NET Core 应用程序
-
用Nginx部署ASP.NETCore应用程序步骤如下:在Linux中安装.NETCore运行时和Nginx:...
- Asp.net Core启动流程讲解(一)(asp.net core 入门)
-
asp.netcore默认项目包括项目根目录级的Startup.cs、Program.cs、appsettings.json(appsettings.Development.json)launch...
- 十天学会ASP之第五天(十天学会asp教程)
-
学习目的:学会数据库的基本操作1(写入记录)数据库的基本操作无非是:查询记录,写入记录,删除记录,修改记录。今天我们先学习写入记录。先建立一个表单:<formname="form1"met...
- ASP.NET Core 的 WebApplication 类
-
ASP.NETCore提供了3个主机类(Host)。这些类用于配置应用、管理生命周期和启动Web服务。...
- ASP.NET Core中的键控依赖注入(.net依赖注入原理)
-
大家好,我是深山踏红叶,今天我们来聊一聊ASP.NETCore中的FromKeyedServices,它是在.Net8中引入的。这一特性允许通过键(如字符串或枚举)来注册和检索依赖注入(D...
- Asp.net常用方法及request和response-a
-
asp.net教程asp.net常用方法:1、Request.UrlReferrer请求的来源,可以根据这个判断从百度搜的哪个关键词、防下载盗链、防图片盗链,可以伪造(比如迅雷)。(使用全局一般处理...
- asp.net常考面试题(aspnet题库)
-
asp.net常考面试题一,列举ASP.Net页面之间传递值的几种方式?1,使用QueryString,如:......?id=1;response.Redirect()......2,使用Sessi...
- 在Windows系统搭建.NET Core环境并创建运行ASP.NET网站
-
微软于6月27日在红帽DevNation峰会上正式发布了.NETCore1.0、ASP.NET1.0和EntityFrameworkCore1.0,其将全部支持Windows、OSX和...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)