Struts2 随笔1

配置拦截器
<interceptors>
                <interceptor-stack name="mystack">
                    <!-- 默认的拦截器 -->
                    <interceptor-ref name="struts-default"></interceptor-ref>
                    <!--查看每个运行action时间 -->
                    <interceptor-ref name="timer"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
            <!-- 修改默认拦截器(要在action之前) -->
            <default-interceptor-ref name="mystack"></default-interceptor-ref>

 

在struts.xml文件中,有时候打一个 < 却没有提示,原因是因为没有连接互联网,访问不到"http://struts.apache.org/dtds/struts-2.3.dtd"这个路径
解决方法:1、连接到互联网,工具会自动下载使用
          2、在MyEclipse中配置一下
配置步骤如下:
Window---Preferences--MyEclipse Enterprise …. ---XML-----XML Catalog --
然后点击Add按钮,添加文件。Location 添加的是struts-2.3.dtd文件的路径,Key Type选择URL,KEY中的内容为http://struts.apache.org/dtds/struts-2.3.dtd,选择OK.
注意:在配置完成之后,struts.xml文件中可能会有一个红叉,按一下回车键,然后在保存一下就可以了

 

<!--公共的跳转 -->
    <global-results>
         <result name="logins" type="redirectAction">
            <param name="actionName">login</param>
              <!-- login?code=1001 -->
            <param name="code">1001</param>
          </result>
    </global-results>

 

文件下载
<action name="download" class="com.peng.action.UserAction" method="download">
                <result type="stream">
            //文件类型 <param name="contentType">${fileType}</param>
            //文件大小 <param name="contentLength">${fileLength}</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="contentCharSet">UTF-8</param>
            //指向getInputstream <param name="inputName">inputstream</param> </result> </action> //action   public String download() { return SUCCESS; } public InputStream getInputstream() throws FileNotFoundException { fileName = "zhangqingfeng.doc"; File file = new File("E:/Download/",fileName); fileType = map.get(file.getName().substring(file.getName().trim().lastIndexOf("."))); fileLength = file.length(); return new FileInputStream(file); }

 

//文件上传
<action name="upload" class="com.peng.action.UserAction" method="upload">
                <result type="redirectAction">
                    <param name="actionName">home</param>
                </result>
            </action>

//action
public String upload() throws Exception{
        System.out.println("Desc: " + desc);
        //获取文件名称为临时文件的名称
        System.out.println("Pic Name: " + pic.getName());
        System.out.println("Pic length :" + pic.length());
        //获取文件真正的文件名
        System.out.println("File Name:" + picFileName);
        System.out.println("文件类型:" + picContentType);
        
        InputStream input = new FileInputStream(pic);
        OutputStream out = new FileOutputStream(new File("E:/Download/",UUID.randomUUID().toString()+picFileName.substring(picFileName.lastIndexOf("."))));
        byte[] buffer =  new byte[1024]; 
        int len = -1;
        
        while ((len=input.read(buffer))!=-1) {
            out.write(buffer,0,len);
        }
        
        out.flush();
        out.close();
        input.close();
        return SUCCESS;
    }

 

posted on 2014-07-07 14:20  jiandan123  阅读(239)  评论(0编辑  收藏  举报

导航