<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>

     

    主站蜘蛛池模板: 免费无码又黄又爽又刺激| 免费无码又爽又刺激高潮的视频| 亚洲视频免费在线播放| 天天摸夜夜摸成人免费视频| 一级毛片免费播放视频| 亚洲高清日韩精品第一区| 国产91在线免费| 37pao成人国产永久免费视频| 亚洲中文无码卡通动漫野外| 亚洲开心婷婷中文字幕| 最近中文字幕mv免费高清电影| 国产va免费精品| 亚洲1区1区3区4区产品乱码芒果| 亚洲人成网站观看在线播放| 最近中文字幕电影大全免费版 | a级毛片无码免费真人| 成人一级免费视频| 亚洲AV无码一区二区三区在线| 亚洲&#228;v永久无码精品天堂久久| 日本高清免费观看| 在线精品自拍亚洲第一区| 久久精品国产亚洲av麻豆色欲 | 亚洲精品在线电影| 亚洲Av无码乱码在线znlu| 91网站免费观看| 黄网站免费在线观看| 色吊丝免费观看网站| 亚洲一级毛片免费观看| 亚洲精品狼友在线播放| 免费一级国产生活片| 在线视频免费观看爽爽爽| 美女视频黄a视频全免费网站色窝| 久久精品国产亚洲AV天海翼| 亚洲熟妇av一区| 亚洲Av无码专区国产乱码DVD| mm1313亚洲精品国产| 成人无遮挡毛片免费看| 亚洲成人在线免费观看| 最好免费观看高清在线| 一级做a爰片久久免费| 色窝窝亚洲AV网在线观看|