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

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

          Struts2 要點筆記(四)

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

          第一步、jar文件的準備

          commons-fileupload-1.2.1.jarcommons-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>

           

          主站蜘蛛池模板: 南岸区| 龙游县| 长海县| 扎赉特旗| 陵川县| 都兰县| 皋兰县| 阿拉善左旗| 通辽市| 诸城市| 延安市| 明溪县| 广宁县| 崇左市| 福贡县| 措勤县| 赤水市| 志丹县| 自治县| 谢通门县| 灵武市| 镇康县| 塔城市| 白银市| 石嘴山市| 巴青县| 芦溪县| 威海市| 承德县| 大荔县| 龙泉市| 汉沽区| 南漳县| 河曲县| 安化县| 峨边| 泰和县| 宁明县| 榕江县| 山阴县| 亳州市|