struts2-剩余

 

一、说明

类型转换、输入验证(前台和后台)(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>
原文地址:https://www.cnblogs.com/faded8679/p/10758420.html