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

    Struts2 要點(diǎn)筆記(四)

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

    第一步、jar文件的準(zhǔn)備

    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類(lèi)中添加以下屬性

    注意:

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

    可以得到上傳文件類(lèi)型

    web上傳文件大小注意不要太大,一般的視頻網(wǎng)站上傳大文件是通過(guò)通訊軟件上傳的,即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.自定義攔截器的部署時(shí)候需要定義攔截器棧,在該棧中需要引入系統(tǒng)默認(rèn)的攔截器,如果直接應(yīng)用則會(huì)導(dǎo)致系統(tǒng)所有的攔截器對(duì)該action都會(huì)失效

    2.所以要注意攔截器的應(yīng)用范圍:action/package

    3.每個(gè)包只能指定一個(gè)默認(rèn)的攔截器

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

    4.一個(gè)action可以定義多個(gè)攔截器

    <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""你沒(méi)有權(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>

     

    主站蜘蛛池模板: 亚洲日韩aⅴ在线视频| 免费大黄网站在线观| 免费看美女让人桶尿口| 免费观看日本污污ww网站一区| 国产亚洲精品精品国产亚洲综合| 亚洲国产精品高清久久久| 免费福利电影在线观看| 一本岛v免费不卡一二三区| 中国黄色免费网站| 国产免费牲交视频免费播放| 黄色a级免费网站| 亚洲欧美在线x视频| 两个人的视频www免费| av免费不卡国产观看| 67194国产精品免费观看| 中文字幕无码免费久久| 成人福利免费视频| www亚洲精品少妇裸乳一区二区 | 99热免费在线观看| 精品成人免费自拍视频| 国产卡二卡三卡四卡免费网址 | 亚洲视频网站在线观看| 亚洲精品蜜夜内射| 中文字幕免费观看全部电影| 日本成年免费网站| 国产亚洲精品精品国产亚洲综合| wwwxxx亚洲| 99免费在线视频| 全免费一级午夜毛片| 亚洲国产成人片在线观看无码 | 亚洲免费电影网站| 久久夜色精品国产噜噜亚洲a| 亚洲视频无码高清在线| 国产久爱免费精品视频| 在线观看视频免费完整版| 亚洲午夜福利在线观看| 亚洲爆乳无码专区www| 一区二区三区观看免费中文视频在线播放| 日日操夜夜操免费视频 | 四虎影院在线免费播放| 国产jizzjizz视频免费看|