<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 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    Struts2 要點筆記(四)

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

    第一步、jar文件的準備

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

    第二步、把form表的enctype設(shè)置為: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類中添加以下屬性

    注意:

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

    可以得到上傳文件類型

    web上傳文件大小注意不要太大,一般的視頻網(wǎng)站上傳大文件是通過通訊軟件上傳的,即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.自定義攔截器的部署時候需要定義攔截器棧,在該棧中需要引入系統(tǒng)默認的攔截器,如果直接應用則會導致系統(tǒng)所有的攔截器對該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,代表用戶已經(jīng)登錄,允許執(zhí)行action中的方法

      ActionContext.getContext().put(
    "message""你沒有權(quán)限執(zhí)行該操作");

      
    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>

     

    主站蜘蛛池模板: 精品国产_亚洲人成在线高清| 国拍在线精品视频免费观看| 亚洲精品高清在线| 色窝窝亚洲AV网在线观看| 亚洲中文字幕无码专区| 美女视频免费看一区二区| 免费v片视频在线观看视频| 99亚洲男女激情在线观看| 国产大片91精品免费看3| 污污视频免费观看网站| 亚洲午夜精品久久久久久浪潮 | 本免费AV无码专区一区| 亚洲中文字幕无码爆乳av中文 | 美女视频免费看一区二区| 一级毛片直播亚洲| 亚洲视频在线免费| 久久精品国产亚洲av成人| 91免费在线播放| 亚洲中文字幕无码av| 国产v片免费播放| 亚洲精品白色在线发布| 日本永久免费a∨在线视频| 国产精品xxxx国产喷水亚洲国产精品无码久久一区| 美女免费视频一区二区| 在线A亚洲老鸭窝天堂| 久久青草免费91线频观看站街| 亚洲精品成人片在线观看| 国产免费黄色无码视频| 久久夜色精品国产噜噜亚洲AV| 成人免费一级毛片在线播放视频| 亚洲av无码专区青青草原| 最新国产AV无码专区亚洲| 三年片在线观看免费大全电影| 国产偷国产偷亚洲高清日韩| 成人国产精品免费视频| 亚洲一区在线观看视频| 免费在线观看的黄色网址| 久久久久久影院久久久久免费精品国产小说 | 亚洲av最新在线观看网址| 国产亚洲人成无码网在线观看| 青青视频观看免费99|