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,(文件名需要和之前的配置文件相符)。否则可能上传文件失败。

运行结果:

猜你喜欢

转载自blog.csdn.net/xu_benjamin/article/details/83755864