zoukankan      html  css  js  c++  java
  • Struts2的文件上传及错误国际化显示

    一、准备工作

    1. 运行环境:
      1. jdk6.0 | MyEclipse10 | win7       
    2. struts2架包的准备和上传文件夹的创建:
      1.  upload文件夹创建在WEB-INF/upload  

    3. struts2的配置文件的准备struts.xml: 
      1.   
         1 <?xml version="1.0" encoding="UTF-8" ?>
         2 <!DOCTYPE struts PUBLIC
         3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
         4     "http://struts.apache.org/dtds/struts-2.3.dtd">
         5 
         6 <struts>
         7     <!-- 配置开发者模式 -->
         8     <constant name="struts.devMode" value="true" />    
        24 </struts>

        备注:struts.xml必须在src下 

    4. web.xml的配置
      1.  1 <?xml version="1.0" encoding="UTF-8"?>
         2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
         3 
         4     <display-name>Struts Blank</display-name>
         5 
         6     <filter>
         7         <filter-name>struts2</filter-name>
         8         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         9     </filter>
        10 
        11     <filter-mapping>
        12         <filter-name>struts2</filter-name>
        13         <url-pattern>/*</url-pattern>
        14     </filter-mapping>
        15 
        16     <welcome-file-list>
        17         <welcome-file>index.html</welcome-file>
        18     </welcome-file-list>
        19 
        20 </web-app>

    二、代码的编写

    1. 上传action类的编写:
        1.  1 package cn.xu.demo;
           2 
           3 import java.io.File;
           4 import org.apache.commons.io.FileUtils;
           5 import org.apache.struts2.ServletActionContext;
           6 
           7 import com.opensymphony.xwork2.Action;
           8 import com.opensymphony.xwork2.ActionSupport;
           9 
          10 public class UploadAction extends ActionSupport {
          11     private File upload;
          12     private String uploadFileName;
          13     private String uploadContentType;
          14     
          15     public String upload()throws Exception{
          16         String root = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
          17         File destFile = new File(root,uploadFileName);
          18         FileUtils.copyFile(upload, destFile);
          19         
          20         return "success";
          21     }
          22 
          23     public File getUpload() {
          24         return upload;
          25     }
          26 
          27     public void setUpload(File upload) {
          28         this.upload = upload;
          29     }
          30 
          31     public String getUploadFileName() {
          32         return uploadFileName;
          33     }
          34 
          35     public void setUploadFileName(String uploadFileName) {
          36         this.uploadFileName = uploadFileName;
          37     }
          38 
          39     public String getUploadContentType() {
          40         return uploadContentType;
          41     }
          42 
          43     public void setUploadContentType(String uploadContentType) {
          44         this.uploadContentType = uploadContentType;
          45     }
          46     
          47     
          48     
          49 
          50 }
        
    2. struts.xml的配置:
      1.  1 <?xml version="1.0" encoding="UTF-8" ?>
         2 <!DOCTYPE struts PUBLIC
         3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
         4     "http://struts.apache.org/dtds/struts-2.3.dtd">
         5 
         6 <struts>
         7     <!-- 配置开发者模式 -->
         8     <constant name="struts.devMode" value="true" />    
         9     <constant name="struts.custom.i18n.resources" value="message"></constant>
        10     
        11     <package name="p1" extends="struts-default" namespace="/">
        12         <!-- 对上传文件的大小进行设置 -->
        13          <action name="upload" class="cn.xu.demo.UploadAction" method="upload">
        14             <result name="success">/success.jsp</result>
        15             <interceptor-ref name="defaultStack">
        16                     <param name="fileUpload.maximumSize">2018</param>
        17             </interceptor-ref>
        18             <result name="input">/errors.jsp</result>
        19         </action>
        20     </package>
        21 </struts>
          
    3. 页面的编写
      1. success.jsp的编写:
        1.  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
           2     pageEncoding="UTF-8"%>
           3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
           4 <html>
           5 <head>
           6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           7 <title>Insert title here</title>
           8 </head>
           9 <body>
          10     <h1>删除成功</h1>
          11 </body>
          12 </html>
      2. errors.jsp的编写:
        1.  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
           2     pageEncoding="UTF-8"%>
           3 <%@taglib prefix="s" uri="/struts-tags" %>
           4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
           5 <html>
           6   <head>
           7     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           8     <title>Insert title here</title>
           9   </head>
          10   <body>
          11   <h1>错误页面</h1>
          12    <s:actionerror/>
          13    <s:fielderror/>
          14   </body>
          15 </html>
           
      3. upload.jsp的编写
        1.  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
           2     pageEncoding="UTF-8"%>
           3 <%@taglib prefix="s" uri="/struts-tags" %>
           4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
           5 <html>
           6   <head>
           7     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
           8     <title>Insert title here</title>
           9   </head>
          10   
          11   <body>
          12     <s:form action="upload" namespace="/" method="post" enctype="multipart/form-data">
          13         <s:file name="upload"></s:file>
          14         <s:submit value="'保存'"/>
          15     </s:form>
          16     <hr/>
          17   </body>
          18 </html>                 
    4. 配置文件message.properties的编写:
        1. 备注:该文件也在src下              

    三、运行结果

    1. 成功结果:
    2. 错误结果:
      1. 错误类型一
        1. 备注:因为我配置局部上传大小的定义和国际化的配置我上传的文件大小为23kb局部限制为2K

      2. 错误类型二 
        1. 备注:因为全局的上传文件大小为2M而我上传的文件的大小为14M

    四、项目结构图

    五、分析

    1. UploadAction类的的分析
      1. 上传的属性应该怎么写
        1. 打开你的struts的架包中的struts2-core-2.3.15.3.jar -->><interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
        2. 在myeclipse中按Ctrl+shit+t 打开搜索该类的对话框,如果是第一次则需要关联源码,打开该类后你在上面可以找到有关该类的

               说明了并附上截图

                   

              3.查看源码的解释

               

             4.推荐我的写法

                 

          2.对Upload上传方法的解释 

            

      2.struts.xml配置文件的分析

        1.

        2.开启国际化你可以在struts2-core-2.3.15.3.jar-->>org.apache.strut2-->>default.proterties中找到国际化常量

        3.对上传文件其他的限制可以参考FileUplaodInterceptor类中setXxx的方法进行设置     

      3.message.propreties配置文件的分析

        1.message.properties中的key是怎么写的?

          当你不开启国际化时:当程序出现你所限制的错误时,他就会往input视图跳,这时你就把显示的英文错误复制下来

          并在struts2-core-2.3.15.3.jar-->>org.apache.struts2-->>struts-messages.properties中就写行了

        2.

        3.

         4.备注:上传文件的大小<局部设置的文件大小<全局的上传文件的大小

      4.页面的分析

      1.upload.jsp页面分析

          1.

       2.errors.jsp 页面分析

          1.

      

      

       

        

  • 相关阅读:
    java IO流详解
    java设计模式之单例模式(几种写法及比较)
    JS定时刷新页面及跳转页面
    遍历map的四种方法
    String 中去掉空格
    TSP问题_遗传算法(STL大量使用)
    无向图的深度优先生成树和广度优先生成树
    Prim算法求最小生成树
    哈夫曼编码_静态库
    中序线索化二叉树
  • 原文地址:https://www.cnblogs.com/geyaofendou/p/7811978.html
Copyright © 2011-2022 走看看