1. 程式人生 > >Struts2的開發應用

Struts2的開發應用

一、實驗目的:

理解MVC設計模式的基本概念和Java Web開發的兩種模式Model1和Model2,以及Struts開發工作流程和基本應用。

 

二、實驗環境:

win10+myeclipse10+tomcat7.0+Struts 2 Core Libraries

三、實驗內容:

(一) Struts2的檔案上傳

新建一個Java Web工程,在工程目錄下匯入 Struts 2 Core Libraries

1. 檔案上傳頁面,其中包含兩個表單域:檔案標題和檔案瀏覽域

    • 程式功能:上傳頁面,包含兩個表單域。
    • 新建upload.html
      檔案
    • upload.html程式原始碼:
<!DOCTYPE html>

<html>

  <head>

    <title>簡單的檔案上傳</title>

    <meta http-equiv="Content-Type" content="text/html;charset=GBK">

  </head>



  <body>

    <form action="upload.action" method="post" enctype="multipart/form-data">

       檔案標題:<input type="text" name="title"><br>

       選擇檔案:<input type="file" name="upload"><br>

       <input value="上傳" type="submit">

    </form>

  </body>

</html>

2.處理上傳請求的Action類

新建UploadAction.java檔案,包名為lee

UploadAction.java原始碼:

package lee;



import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;



import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;



import com.opensymphony.xwork2.ActionSupport;



public class UploadAction extends ActionSupport {

   private String title;

   private File upload;

   private String uploadContentType;

   private String uploadFileName;

  

   //接受依賴注入的屬性

   private String savePath;



   public String getTitle() {

      return title;

   }



   public void setTitle(String title) {

      this.title = title;

   }



   public File getUpload() {

      return upload;

   }



   public void setUpload(File upload) {

      this.upload = upload;

   }



   public String getUploadContentType() {

      return uploadContentType;

   }



   public void setUploadContentType(String uploadContentType) {

      this.uploadContentType = uploadContentType;

   }



   public String getUploadFileName() {

      return uploadFileName;

   }



   public void setUploadFileName(String uploadFileName) {

      this.uploadFileName = uploadFileName;

   }

  

   public String getSavePath() throws Exception{

      return ServletActionContext.getRequest().getRealPath(savePath);

   }

  

   public void setSavePath(String savePath) {

      this.savePath = savePath;

   }

  

   @Override

   public String execute() throws Exception {

      System.out.println("開始上傳單個檔案--------------");

      System.out.println(getSavePath());

      System.out.println("========="+getUploadFileName());

      System.out.println("========="+getUploadContentType());

      System.out.println("========="+getUpload());

      //以伺服器的檔案儲存地址和原檔名建立上傳檔案輸出流

      FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());

      FileInputStream fis=new FileInputStream(getUpload());

      byte[] buffer=new byte[1024];

      int len=0;

      while((len=fis.read(buffer))>0)

      {

         fos.write(buffer, 0, len);

      }

      return SUCCESS;

   }

}
  1. 配置檔案上傳的Action

在src目錄下的struts.xml檔案進行配置,struts.xml原始碼如下:

   

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

   <constant name="struts.custom.i18n.resources" value="globalMessages"/>

   <constant name="struts.i18n.encoding" value="GBK"/>

   <package name="lee" extends="struts-default">

      <action name="upload" class="lee.UploadAction">

         <param name="savePath">/upload</param>

         <result>/succ.jsp</result>

      </action>

   </package>

</struts>

web.xml原始碼:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

   xmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <filter>

  <filter-name>struts2</filter-name>

  <filter-class>

      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

  </filter-class>

  </filter>

  <filter-mapping>

  <filter-name>struts2</filter-name>

  <url-pattern>*.action</url-pattern>

  </filter-mapping>



</web-app>

新建一個succ.jsp頁面,原始碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>成功頁面</title>

  </head>



  <body>

    檔案上傳成功!

  </body>

</html>

注意:需要在tomcat的安裝目錄下找到webapps資料夾下的你新建的Java Web工程的資料夾,在該工程目錄下新建一個資料夾,命名為upload,(檔名需要和之前的配置檔案相符)。否則可能上傳檔案失敗。

執行結果: