一、说明

类型转换、输入验证(前台和后台)(validate()、validateXXX()、xml)

标签、上传下载、i18n(国际化)、ognl(#reqeust.name)

注解方式、log4j(日志记录)、jfreechart(java图形报表)、highcharts(jquery插件)

二、上传下载

1、上传

在struts2框架中,上传不需要添加额外的jar,上传默认使用Apache的上传组件fileupload;我们可以直接使用

需要注意的是,上传组件只是帮我们把上传的文件流转为文件数据的封装,上传保存的逻辑代码还是需要我们自己来完成;

(1)demo1.jsp

<form action="demo1Action!upload.action" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="myFile"/><br/>
<input type="submit" value="上 传"/>
</form>

(2)Demo1Action

@Setter
@Getter
public class Demo1Action extends ActionSupport { private File myFile; //上传的文件
//myFile是表单name元素,后面的FileName和ContentType是固定的
private String myFileFileName; //上传文件名
private String myFileContentType; //上传文件类型 public String upload(){ try {
System.out.println("文件名:"+myFileFileName+",文件类型:"+myFileContentType); //上传路径
String path = ServletActionContext.getServletContext().getRealPath("/upload/");
path += myFileFileName;
//上传(流操作)
//输出流
FileOutputStream fos = new FileOutputStream(path);
//输入流
FileInputStream fis = new FileInputStream(myFile); //读取和写入
byte[] b = new byte[1024];
int len = 0;
while((len=fis.read(b)) != -1){
fos.write(b, 0, len);
} //关闭
fis.close();
fos.flush();
fos.close();
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
} return INPUT;
} }

(3)struts.xml

<action name="demo1Action" class="com.yujun.maven.action.Demo1Action">

 </action>

2、下载

https://struts.apache.org/core-developers/stream-result.html

(1)demo2.jsp

<h4>
<a href="demo2Action!download.action?fileName=第二章-选择器.pptx">第二章-选择器.pptx</a>
</h4>
<h4>
<a href="demo2Action!download.action?fileName=jquery-3.2.1.min.js">jquery-3.2.1.min.js</a>
</h4>

(2)Demo2Action

@Setter
@Getter
public class Demo2Action extends ActionSupport { private String fileName; //文件名
private InputStream is; //输入流 public String download(){ try {
System.out.println("文件名:"+fileName);
//下载路径
String path = ServletActionContext.getServletContext().getRealPath("/download/");
path += fileName; //文件输入流
is = new FileInputStream(path); //文件名进行下载时乱码处理
fileName = new String(fileName.getBytes(),"iso-8859-1");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
} return INPUT;
}
}

(3)struts.xml

<action name="demo2Action" class="com.yujun.maven.action.Demo2Action">
<!-- type=stream 表示流 -->
<result name="success" type="stream">
<!-- 设置下载的类型:这里表示流类型 -->
<param name="contentType">application/octet-stream</param>
<!-- action类中的输入流成员变量名 -->
<param name="inputName">is</param>
<!-- 下载时的文件名,${fileName}表示引用action类中的成员变量 -->
<param name="contentDisposition">attachment;filename=${fileName}</param>
<!-- 下载时的缓冲大小 -->
<param name="bufferSize">1024</param>
</result>
</action>

三、i18n国际化

internationalization  的首字母i和尾字母n,18中间字符数

国际化:根据不同的语言和地区显示不同界面内容

标识符:

zh  中文

CN  中国

en   英文

US  美国

1、新增语言资源文件

src目录/xxx_zh_CN.properties   中文

src目录/xxx_en_US.properties   英文

xxx是自定义的

2、struts.xml

<!-- 开启国际化配置,lang是国际化资源文件的前缀,如果有多个资源文件,可以使用逗号分隔开 -->

<constant name="struts.custom.i18n.resources" value="lang"/>

3、页面

<%@ taglib prefix="s" uri="/struts-tags" %>
<form>
<s:property value="%{getText('uname')}"/>:<input/><br/><br/>
<s:property value="%{getText('pname')}"/>:<input/><br/><br/>
<input type="submit" value="<s:property value="%{getText('btnLogin')}"/>"/>
</form>

4、动态切换语言

(1)页面

<h4>
<a href="langAction?request_locale=zh_CN">中文</a>
<a href="langAction?request_locale=en_US">英文</a>
</h4>

(2)LangActoin

public class LangAction extends ActionSupport {

    @Override
public String execute() throws Exception {
return SUCCESS;
}
}

(3)struts.xml

<action name="langAction" class="com.yujun.maven.action.LangAction">
<result>/login.jsp</result>
</action>

struts2-剩余的更多相关文章

  1. Struts2拦截器之FileUploadInterceptor

    一.它能做什么? 借助于这个拦截器我们可以实现文件的上传和下载功能. 理论部分: struts2的文件上传下载功能也要依赖于Apache commons-fileupload和Apache commo ...

  2. Struts2(十四)拦截器实现权限管理

    一.认识拦截器 拦截器也是一个类 拦截器可以在Action被调用之前和之后执行代码 框架很多核心功能是拦截器实现的 拦截器的特点: 拦截器自由组合,增强了灵活性.扩展性.有利于系统解耦 拦截器可以拦截 ...

  3. 转载:struts2和spring的结合原理(精品)

    转载网址:http://blog.sina.com.cn/s/blog_683278bc0101236z.html Ioc将所有的对象之间的关系转移到了xml配置文件中来. 在TopLogic中声明存 ...

  4. 浅谈Struts2(四)

    一.Struts2的拦截器(Intercept) 作用:把多个Action中的共有代码,提取至拦截器,从而减少Action中的冗余代码. 1.Action拦截器 a.编写interceptor类 pu ...

  5. struts2之拦截器

    1. 为什么需要拦截器 早期MVC框架将一些通用操作写死在核心控制器中,致使框架灵活性不足.可扩展性降低, Struts 2将核心功能放到多个拦截器中实现,拦截器可自由选择和组合,增强了灵活性,有利于 ...

  6. 第四章 Struts2深入

    4.1 Struts2架构    1.ActionMapper:        提供请求和Action之间的映射.根据请求查找是否存在对于的action,如有,翻译描述action映射的ActionM ...

  7. 自定义简单的struts2的s标签

    一:自定标签前需要了解的知识: BodyTagSupport类的方法: 编写标签对应的实现类时,需要重载BodyTagSupport类几个方法:doStartTag(), setBodyContent ...

  8. Struts2+Hibernate实现图书管理系统

    效果图 部分代码 Books.java package entity; import java.util.Date; public class Books { //书籍编号 private Strin ...

  9. Struts2 学习笔记(概述)

    Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...

  10. struts2 基础5 OGNL、标签、四大域、默认拦截器说明

    OGNL表达式 OGNL:对象导抗图语言 OGNL表达式是一个上下文的概念,上下文Map结构 OGNL表达式需要使用#标注命名空间.访问上下文(Context)中的对象需要使用#符号标注命名空间,如# ...

随机推荐

  1. [2019.04.01]Linux 学习心得(2)-- tar 命令的理解

    这篇文章并不是发布最早的但是阅读量却每天都见长,很想知道各位大大是怎么找到这篇文章的.如果不忙,还请各位大大评论一下我看看,没准我可以为大家改进一下本文,提升一下质量. =============== ...

  2. [Windows Server]Windows Server turn off screen auto-lock to fit scheduled tasks(Error Code :0x4F7) / 关闭Windows Server的自动锁定来解决计划任务0x4F7错误

    1. 打开“运行”,输入“regedit” 并回车. 2. 找到以下注册表路径,将Attributes的值改为 2: (原为1 HKEY_LOCAL_MACHINE \SYSTEM \CurrentC ...

  3. Re.多项式求逆

    前言 emmm暂无 多项式求逆目的 顾名思义 就是求出一个多项式的摸xn时的逆 给定一个多项式F(x),请求出一个多项式G(x),满足F(x)∗G(x)≡1(modxn),系数对998244353取模 ...

  4. php对某个页面设置基础认证登录设置

    记录下. 对某个页面设置认证 代码最开始添加: <?php $user = 'test'; $password = '111111'; // 通过HTTP认证进行验证 if (!isset($_ ...

  5. Java集合的总结

    参考博客: http://www.jianshu.com/p/63e76826e852 http://www.cnblogs.com/LittleHann/p/3690187.html https:/ ...

  6. python之路day07-集合set的增删查、列表如何排重(效率最高的方法)、深浅copy

    集合set 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...

  7. 【JS】JavaScript 指定日期增加天数

    指定某个日期(字符串),增加n天后,输出日期字符串,格式:年-月-日: /** * [dateAddDays 从某个日期增加n天后的日期] * @param {[string]} dateStr [日 ...

  8. cannot update the cursor rep,since it is read-only

    操作DBF文件,开发机器读写都OK,但部署到服务器上后报:cannot update the cursor rep,since it is read-only 网上寻找解决方案英文答案比较多,也没有给 ...

  9. Tomcat系列(1)——Tomcat安装配置

    核心步骤 1. 安装JAVA(因为tomcat依赖于java) 配置:JAVA_HOME D:\Program Files (x86)\Java\jdk1.7.0 path  %JAVA_HOME%\ ...

  10. html常用标签的取值和赋值操作

    我们在html页面当中,面对各种各样的标签,经常需要处理取值和赋值的问题,下面,就把常见的一些html标签元素的取值和赋值操作进行总结整理,以后备用. 1.button:改变button按钮上面的值, ...