<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Struts2 要點筆記(四)

    Posted on 2010-05-17 00:14 Gavin.lee 閱讀(244) 評論(0)  編輯  收藏 所屬分類: SSH2 --Struts2
    十九、文件上傳三部曲

    第一步、jar文件的準備

    commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar

    第二步、把form表的enctype設置為:multipart/form-data

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>

     
    <head>

        
    <title>My JSP 'employeeAdd.jsp' starting page</title>

        
    <meta http-equiv="pragma" content="no-cache">

        
    <meta http-equiv="cache-control" content="no-cache">

        
    <meta http-equiv="expires" content="0">

     
    </head>

     
    <body>

        
    <form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">

            文件:
    <input type="file" name="image">

            
    <input type="submit" value="上傳"/>

        
    </form>

     
    </body>

    </html>


    第三步、在
    Action類中添加以下屬性

    注意:

    可以設置Struts2的常量struts.multipart.maxSize來設置上傳文件大小

    可以得到上傳文件類型

    web上傳文件大小注意不要太大,一般的視頻網站上傳大文件是通過通訊軟件上傳的,即socket通訊

    多文件上傳:

    package cn.itcast.action;

    import java.io.File;

    import org.apache.commons.io.FileUtils;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionContext;

    public class HelloWorldAction {

        
    private File[] image;

        
    private String[] imageFileName;

        
    public File[] getImage() {

           
    return image;

        }


        
    public void setImage(File[] image) {

           
    this.image = image;

        }


        
    public String[] getImageFileName() {

           
    return imageFileName;

        }


        
    public void setImageFileName(String[] imageFileName) {

           
    this.imageFileName = imageFileName;

        }


        
    public String addUI(){

           
    return "success";

        }


        
    public String execute() throws Exception{

           String realpath 
    = ServletActionContext.getServletContext().getRealPath("/images");

           System.out.println(realpath);

           
    if(image!=null){

               File savedir 
    = new File(realpath);

               
    if(!savedir.exists()) savedir.mkdirs();

               
    for(int i = 0 ; i<image.length ; i++){           

                  File savefile 
    = new File(savedir, imageFileName[i]);

                  FileUtils.copyFile(image[i], savefile);

               }


               ActionContext.getContext().put(
    "message""上傳成功");

           }


           
    return "success";

        }


    }




    二十、自定義攔截器

    注意:

    1.自定義攔截器的部署時候需要定義攔截器棧,在該棧中需要引入系統默認的攔截器,如果直接應用則會導致系統所有的攔截器對該action都會失效

    2.所以要注意攔截器的應用范圍:action/package

    3.每個包只能指定一個默認的攔截器

    <default-interceptor-ref name=”permissionStack” />

    4.一個action可以定義多個攔截器

    <interceptor-ref name=”interceptor1” />

    <interceptor-ref name=”interceptor2” />

    攔截器:

    package cn.itcast.interceptor;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;

    public class PermissionInterceptor implements Interceptor {

        
    public void destroy() {

        }


        
    public void init() {

        }


        
    public String intercept(ActionInvocation invocation) throws Exception {

      Object user 
    = ActionContext.getContext().getSession().get("user");

      
    if(user!=nullreturn invocation.invoke();  
      
    //如果user不為null,代表用戶已經登錄,允許執行action中的方法

      ActionContext.getContext().put(
    "message""你沒有權限執行該操作");

      
    return "success";
        }

    }


    部署:

    <package name="employee" namespace="/control/employee" extends="struts-default">

           
    <interceptors>

               
    <interceptor name="permission" class="cn.itcast.interceptor.PermissionInterceptor"/>

               
    <interceptor-stack name="permissionStack">

                  
    <interceptor-ref name="defaultStack"/>

                  
    <interceptor-ref name="permission" />

               
    </interceptor-stack>

           
    </interceptors>

           
    <global-results>

               
    <result name="success">/WEB-INF/page/message.jsp</result>

           
    </global-results>

           
    <action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}">

               
    <interceptor-ref name="permissionStack" />

           
    </action>

        
    </package>

     

    主站蜘蛛池模板: 亚洲国产天堂在线观看| 日韩免费视频在线观看| 久久午夜无码免费| 国产免费阿v精品视频网址| 在线观看黄片免费入口不卡| 久久国产美女免费观看精品| 国产在线观看无码免费视频| a毛片在线免费观看| 免费毛片a线观看| 99re免费在线视频| 99久久国产热无码精品免费 | 亚洲福利视频网址| 亚洲精品在线免费看| 亚洲乱码卡三乱码新区| 中文字幕亚洲综合久久综合| 亚洲第一街区偷拍街拍| 亚洲av无码专区在线观看亚| 羞羞漫画页面免费入口欢迎你| 色多多www视频在线观看免费| 99精品视频免费| 中文字幕免费观看| 日韩av无码成人无码免费| 国产资源免费观看| 亚洲一级黄色视频| 亚洲AV成人片色在线观看高潮| 亚洲视频小说图片| 亚洲精品自偷自拍无码| 四虎影视久久久免费| 国产色爽免费无码视频| 国产精品视频免费| 国产真实伦在线视频免费观看| 狠狠综合久久综合88亚洲| 无码专区—VA亚洲V天堂| 77777午夜亚洲| 一级毛片免费视频网站| 无码日韩精品一区二区三区免费| 欧美男同gv免费网站观看| 一本色道久久88亚洲综合| 亚洲av一综合av一区| 久久亚洲精品国产亚洲老地址| 免费无码国产V片在线观看|