Struts2-拦截器-单个拦截器

自定义拦截器

1.创建一个继承AbstractInterceptor的类

package com.gyf.web.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor{ @Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor拦截前.....");
//放行
String returnStr = invocation.invoke();
System.out.println("MyInterceptor拦截后.....");
return returnStr;
} }

2.在Struts.xml文件中声明刚刚创建的拦截器

<package name="p1" extends="struts-default" namespace="/">
<!--声明拦截器-->
<interceptors>
<interceptor name="MyInterceptor" class="com.gyf.web.interceptor.MyInterceptor"/>
</interceptors>
</package>

3.创建一个action和一个jsp页面

package com.gyf.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class Demo1Action extends ActionSupport{
public String action1() {
return SUCCESS;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title> </head>
<body>
Action1.....
</body>
</html>

4.在struts.xml中声明一个action

<package name="p1" extends="struts-default" namespace="/">
<!--声明拦截器-->
<interceptors>
<interceptor name="MyInterceptor" class="com.gyf.web.interceptor.MyInterceptor"/>
</interceptors> <action name="action1" class="com.gyf.web.action.Demo1Action" method="action1">
<interceptor-ref name="MyInterceptor"/>
<result>/action1.jsp</result>
</action>
</package>

拦截器放行的是什么呢?

注意:如果在action中配置了拦截器,那么默认的拦截器就失效了

Struts2文件上传

Struts提供了内置标签用于文件上传<s:file>,我们成为文件选择域

文件上传的必要前提条件:表单必须是post方法,enctype类型必须为Multipart/form-data

//上传文件需要三个属性

private File photo;//struts会自动把数据转成文件对象
private String photoContentType;//文件的类型
private String photoFileName;//文件对的名称

文件上传类型的限制和中文错误显示

配置Struts.xml来限制文件上传类型

<package name="p1" extends="struts-default" namespace="/">
<action name="upload" class="com.gyf.web.action.UploadAction" method="upload">
<!-- 依赖注入 :
fileUpload拦截器调用allowedTypes方法
参数写MIME类型 <param name="fileUpload.allowedExtensions">png,jpg</param>
-->
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/png,image/jpeg</param>
</interceptor-ref>
<result name="input">/upload.jsp</result>
</action>
</package>

当文件上传类型出错是出现的提示并不是中文,现在把错误信息提示改成中文

自己写一个国际化资源包,把key的值改成中文即可

首先创建一个国际化资源包,把提示信息复制到自己创建的国际化资源包,提示信息在这里

更改国际化资源包的value

value中的 {0},{1},{2},{3}表示的是字段名,和文件名,临时文件,文件类型

struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed="{1}" - {3} \u6587\u4EF6\u7C7B\u578B\u4E0D\u88AB\u5141\u8BB8
struts.messages.error.file.extension.not.allowed=\u6587\u4EF6\u6269\u5C55\u540D\u4E0D\u4E0D\u6B63\u786E: {0} "{1}" "{2}" {3}

然后在struts.xml文件配置国际化

	<constant name="struts.custom.i18n.resoources" value="fileuploadmessage"></constant>

配置完成

多文件上传

多文件上传,在Action代码中,只需要把接收的文件字段设置为数组即可

jsp配置

	<s:form action="/upload2" enctype="multipart/form-data" method="post">
<s:textfield label="用户名" name="username"/>
<s:textfield label="密码" name="password"/>
<s:file label="照片" name="photo"/>
<s:file label="照片" name="photo"/>
<s:file label="照片" name="photo"/>
<s:submit value="上传"/>
</s:form>

action类

package com.gyf.web.action;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
private String username;
private String password;
private File[] photo;//struts会自动把数据转成文件对象
private String[] photoContentType;//文件的类型
private String[] photoFileName;//文件对的名称
public String upload() {
for(int i=0;i<photo.length;i++) {
File file = photo[i];
System.out.println("文件临时路径: "+file);
System.out.println("文件类型: "+photoContentType[i]);
System.out.println("文件名: "+photoFileName[i]);
}
return NONE;
} public void setPhoto(File[] photo) {
this.photo = photo;
} public void setPhotoContentType(String[] photoContentType) {
this.photoContentType = photoContentType;
} public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
} public void setUsername(String username) {
this.username = username;
} public void setPassword(String password) {
this.password = password;
} }

struts.xml配置

<action name="upload2" class="com.gyf.web.action.UploadAction2" method="upload2">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">png,jpg</param>
</interceptor-ref>
<result name="input">/upload2.jsp</result>
</action>

Struts笔记4的更多相关文章

  1. struts笔记

    Struts视频笔记: Struts是一个开源的web框架,框架提高了程序的规范的同时也约束了程序员的自由 为什么会有struts: 因为我们队mvc理解的不同,可能造成不同公司写程序的时候,规范不统 ...

  2. Struts 笔记 内部资料 请勿转载 谢谢合作

    Struts 概述 随着MVC 模式的广泛使用,催生了MVC 框架的产生.在所有的MVC 框架中,出现最早,应用最广的就是Struts 框架. Struts 的起源 Struts 是Apache 软件 ...

  3. Struts笔记5

    文件下载 1.写action类 package com.gyf.web.action; import java.io.File; import java.io.FileInputStream; imp ...

  4. Struts笔记3

    struts标签 form表单标签 Action:请求地址.直接写动作名称,不用写contextPath <s:form action="/user/register.action&q ...

  5. Struts笔记2

    Struts2-配置文件result元素 作用:为动作指定结果视图 name属性:逻辑视图的名称,对应着动作方法的返回值.默认值是success type属性:结果类型,指的就是用什么方式转到定义的页 ...

  6. struts笔记1

    框架:所谓框架,就是把一些繁琐的重复性代码封装起来,使程序员在编码中把更多的精力放到业务需求的分析和理解上面 SHH:strust spring hibernate; SSM:springmvc sp ...

  7. Struts笔记二:栈值的内存区域及标签和拦截器

    值栈和ognl表达式 1.只要是一个MVC框架,必须解决数据的存和取的问题 2.struts2利用值栈来存数据,所以值栈是一个存储数据的内存结构 1.  ValueStack是一个接口,在struts ...

  8. Struts笔记一

    Struts 概念: 是一个MVC框架: Servlet的缺点 1.在web.xml中文件中需要配置很多行代码,维护起来很不方便呢,不利于团队合作. 2.一个servlet的入口只有一个doPost或 ...

  9. Struts中的OGNL和EL表达式笔记

    Struts中的OGNL和EL表达式笔记 OGNL(Object-Graph Navigation Language),可以方便的操作对象属性的表达式语言. 1.#符号的用途 一般有三种方式: 1.1 ...

随机推荐

  1. css+vue实现流程图

    主要用css+flex布局实现样式部分,vue实现组件逻辑.首先看下效果吧: 当空间不够时还可以使用拖拽功能 接下来说明下实现思路 1.首先是实现单个节点样式,这个很简单不谈了,节点后都跟有一小段连接 ...

  2. [WEB安全]绕过URL跳转限制的思路

    0x00 简介 说起URL跳转漏洞,有些人可能会觉得,不就是单纯的跳转到某一个其他网页吗?有什么用??? 给大家一个链接,你们进去看一下就明白了: http://www.anquan.us/searc ...

  3. NAT反向转换基本配置详解

  4. SignalR要求_转自:https://www.cnblogs.com/humble/p/3855137.html

    SignalR 服务端组件可以被部署在诸多的服务器配置中,本节描述了它所支持的操作系统版本,.NET framework,IIS.以及其他组件 二.支持的服务器操作系统 SignalR服务端组件可以被 ...

  5. Oracle中查询走索引的情况

    1.对返回的行无任何限定条件,即没有where子句 2.未对数据表与任何索引主列相对应的行限定条件例如:在City-State-Zip列创建了三列复合索引,那么仅对State列限定条件不能使用这个索引 ...

  6. Linux服务器下日志截取

    我们经常需要去Linux服务器上查看服务运行日志,但是有时候日志文件很大看起来很不方便,这个时候我们需要对日志进行切割筛选出自己需要的日志,比如查看某段时间内的日志,命令如下:   sed -n '/ ...

  7. peomethues 参数设置 监控网站 /usr/local/prometheus-2.13.0.linux-amd64/prometheus --config.file=/usr/local/prometheus-2.13.0.linux-amd64/prometheus.yml --web.listen-address=:9999 --web.enable-lifecycle

    probe_http_status_code{instance="xxxx",job="web_status"} probe_http_status_code{ ...

  8. Chaos Engineering 混沌工程 Chaos Monkey vs Chaos xxx vs Chaos Blade

    Chaos Engineering的历史.原则以及实践https://www.infoq.cn/article/chaos-engineering-the-history-principles-and ...

  9. 网络通信技术中的中继器repeater

    1. repeater的作用 对信号进行再生和还原 2. repeater的优点 延长通讯距离 提高可靠性 增加节点的最大数目 各个网段可以使用不同的通讯速率 3. repeater的缺点 增加了延时 ...

  10. flutter 右滑返回上一页

    import 'package:flutter/material.dart'; import 'package:flutter_app/pages/SplashScreen.dart'; import ...