strut2

三.在struts2中实现(以图片上传为例)
1.FileUpload.jsp代码清单如下:
Java代码

   1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
   2. <%@ taglib prefix="s" uri="/struts-tags" %>  
   3. <html>  
   4.   <head>  
   5.     <title>The FileUplaodDemo In Struts2</title>  
   6.   </head>  
   7.     
   8.   <body>  
   9.     <s:form action="fileUpload.action" method="POST" enctype="multipart/form-data">  
  10.         <s:file name="myFile" label="MyFile" ></s:file>  
  11.         <s:textfield name="caption" label="Caption"></s:textfield>  
  12.         <s:submit label="提交"></s:submit>  
  13.     </s:form>  
  14.   </body>  
  15. </html> 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
  <title>The FileUplaodDemo In Struts2</title>
  </head>
 
  <body>
    <s:form action="fileUpload.action" method="POST" enctype="multipart/form-data">
    <s:file name="myFile" label="MyFile" ></s:file>
    <s:textfield name="caption" label="Caption"></s:textfield>
    <s:submit label="提交"></s:submit>
    </s:form>
  </body>
</html>



2.ShowUpload.jsp的功能清单如下:
Java代码

   1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
   2. <%@ taglib prefix="s" uri="/struts-tags" %>  
   3. <html>  
   4.   <head>  
   5.     <title>ShowUpload</title>  
   6.   </head>  
   7.     
   8.   <body>  
   9.     <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >   
  10.         <img src ='UploadImages/<s:property value ="imageFileName"/> '/>  
  11.         <br />   
  12.         <s:property value ="caption"/>   
  13.     </div >   
  14.   </body>  
  15. </html> 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>ShowUpload</title>
  </head>
 
  <body>
    <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
        <img src ='UploadImages/<s:property value ="imageFileName"/> '/>
        <br />
        <s:property value ="caption"/>
    </div >
  </body>
</html>



3.FileUploadAction.java的代码清单如下 :
Java代码

   1. package com.chris;  
   2.  
   3. import java.io.*;  
   4. import java.util.Date;  
   5.  
   6. import org.apache.struts2.ServletActionContext;  
   7.  
   8.  
   9. import com.opensymphony.xwork2.ActionSupport;  
  10.  
  11. public class FileUploadAction extends ActionSupport{  
  12.  
  13.      private static final long serialVersionUID = 572146812454l ;  
  14.      private static final int BUFFER_SIZE = 16 * 1024 ;  
  15.       
  16.      //注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定  
  17.      //所以同时要提供myFileContentType,myFileFileName的set方法  
  18.        
  19.      private File myFile;   //上传文件  
  20.      private String contentType;//上传文件类型  
  21.      private String fileName;   //上传文件名  
  22.      private String imageFileName;  
  23.      private String caption;//文件说明,与页面属性绑定  
  24.       
  25.      public void setMyFileContentType(String contentType)  {  
  26.          System.out.println("contentType : " + contentType);  
  27.          this .contentType = contentType;  
  28.     }   
  29.       
  30.      public void setMyFileFileName(String fileName)  {  
  31.          System.out.println("FileName : " + fileName);  
  32.          this .fileName = fileName;  
  33.     }   
  34.           
  35.      public void setMyFile(File myFile)  {  
  36.          this .myFile = myFile;  
  37.     }   
  38.       
  39.      public String getImageFileName()  {  
  40.          return imageFileName;  
  41.     }   
  42.       
  43.      public String getCaption()  {  
  44.          return caption;  
  45.     }   
  46.    
  47.       public void setCaption(String caption)  {  
  48.          this .caption = caption;  
  49.     }   
  50.       
  51.      private static void copy(File src, File dst)  {  
  52.          try  {  
  53.             InputStream in = null ;  
  54.             OutputStream out = null ;  
  55.              try  {                  
  56.                 in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);  
  57.                 out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);  
  58.                  byte [] buffer = new byte [BUFFER_SIZE];  
  59.                  while (in.read(buffer) > 0 )  {  
  60.                     out.write(buffer);  
  61.                 }   
  62.              } finally  {  
  63.                  if ( null != in)  {  
  64.                     in.close();  
  65.                 }   
  66.                   if ( null != out)  {  
  67.                     out.close();  
  68.                 }   
  69.             }   
  70.          } catch (Exception e)  {  
  71.             e.printStackTrace();  
  72.         }   
  73.     }   
  74.       
  75.      private static String getExtention(String fileName)  {  
  76.          int pos = fileName.lastIndexOf(".");  
  77.          return fileName.substring(pos);  
  78.     }   
  79.    
  80.     @Override 
  81.      public String execute()      {          
  82.         imageFileName = new Date().getTime() + getExtention(fileName);  
  83.         File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages" ) + "/" + imageFileName);  
  84.         copy(myFile, imageFile);  
  85.          return SUCCESS;  
  86.     }  
  87. } 

package com.chris;

import java.io.*;
import java.util.Date;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

private static final long serialVersionUID = 572146812454l ;
     private static final int BUFFER_SIZE = 16 * 1024 ;
   
     //注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
     //所以同时要提供myFileContentType,myFileFileName的set方法
    
     private File myFile; //上传文件
     private String contentType;//上传文件类型
     private String fileName; //上传文件名
     private String imageFileName;
     private String caption;//文件说明,与页面属性绑定
   
     public void setMyFileContentType(String contentType)  {
    System.out.println("contentType : " + contentType);
         this .contentType = contentType;
    }
   
     public void setMyFileFileName(String fileName)  {
    System.out.println("FileName : " + fileName);
         this .fileName = fileName;
    }
       
     public void setMyFile(File myFile)  {
         this .myFile = myFile;
    }
   
     public String getImageFileName()  {
         return imageFileName;
    }
   
     public String getCaption()  {
         return caption;
    }

      public void setCaption(String caption)  {
         this .caption = caption;
    }
   
     private static void copy(File src, File dst)  {
         try  {
            InputStream in = null ;
            OutputStream out = null ;
             try  {               
                in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
                out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                 byte [] buffer = new byte [BUFFER_SIZE];
                 while (in.read(buffer) > 0 )  {
                    out.write(buffer);
                }
             } finally  {
                 if ( null != in)  {
                    in.close();
                }
                  if ( null != out)  {
                    out.close();
                }
            }
         } catch (Exception e)  {
            e.printStackTrace();
        }
    }
   
     private static String getExtention(String fileName)  {
         int pos = fileName.lastIndexOf(".");
         return fileName.substring(pos);
    }

    @Override
     public String execute()      {       
        imageFileName = new Date().getTime() + getExtention(fileName);
        File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages" ) + "/" + imageFileName);
        copy(myFile, imageFile);
         return SUCCESS;
    }
}


注:此时仅为方便实现Action所以继承ActionSupport,并Overrider execute()方法
  在struts2中任何一个POJO都可以作为Action

4.struts.xml清单如下:
Java代码

   1. <?xml version="1.0" encoding="UTF-8" ?>  
   2. <!DOCTYPE struts PUBLIC  
   3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
   4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
   5. <struts>  
   6.     <package name="example" namespace="/" extends="struts-default">  
   7.         <action name="fileUpload" class="com.chris.FileUploadAction">  
   8.         <interceptor-ref name="fileUploadStack"/>  
   9.         <result>/ShowUpload.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts> 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="fileUpload" class="com.chris.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<result>/ShowUpload.jsp</result>
</action>
</package>
</struts>


5.web.xml清单如下:
Java代码

   1. <?xml version="1.0" encoding="UTF-8"?>  
   2. <web-app version="2.4"   
   3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
   4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
   5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
   6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
   7.     <filter >   
   8.         <filter-name > struts-cleanup </filter-name >   
   9.         <filter-class >   
  10.             org.apache.struts2.dispatcher.ActionContextCleanUp  
  11.         </filter-class >   
  12.     </filter >   
  13.      <filter-mapping >   
  14.         <filter-name > struts-cleanup </filter-name >   
  15.         <url-pattern > /* </url-pattern >   
  16.     </filter-mapping >  
  17.       
  18.     <filter>  
  19.         <filter-name>struts2</filter-name>  
  20.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  21.     </filter>  
  22.     <filter-mapping>  
  23.         <filter-name>struts2</filter-name>  
  24.         <url-pattern>/*</url-pattern>  
  25.     </filter-mapping>  
  26.   <welcome-file-list>  
  27.     <welcome-file>Index.jsp</welcome-file>  
  28.   </welcome-file-list>  
  29.     
  30. </web-app> 

你可能感兴趣的:(java,apache,jsp,xml,struts)