struts2

来源:互联网 发布:巨人网络传真 编辑:程序博客网 时间:2024/06/04 18:38

Struts2实现文件上传

  • 文件上传提交方式必须post,enctype属性一定为:multipart/form-data
<FORM id=form1 name=form1        action="${pageContext.request.contextPath }/PhoneAction_add"        enctype="multipart/form-data" method=post>        <td>头像 :</td>        <td>                <input type="file" name="phone" value="文件上传">        </td>
  • Action里面生成File字段 name相对应JSP页面提交name 生成get/set方法 调用File.renameTo(File file)实现上传提交
public class PhoneAction extends ActionSupport{    private File phone;    public File getPhone() {        return phone;    }    public void setPhone(File phone) {        this.phone = phone;    }    public String add() throws Exception{        phone.renameTo(new File("D:/upload/wb.jpg"));        return "toList";    }}

struts2配置Struts.xml

  • package

    • name属性:可用来定义包名(程序如需扩展,可继承);
    • namespace属性:可用于解决程序url请求重复问题 如:
      • namespace=”” 为该属性的默认值,所以我们请求URL地址为:http://localhost:8080/项目名称/PhoneAction_add
      • namespace=”/hhh”时,再访问该Action时,其URL应为:http://localhost:8080/项目名称/hhh/PhoneAction_add
    • extends属性:可用来继承其他包的属性(可以多继承).

  • global-results(全局结果集)

    • result(子元素):根据返回值跳转相对应的页面
      • name属性:用来匹配返回的字符串

  • global-exception-mappings(全局异常捕获)

    • exception-mapping

      • result属性:用来设置你捕获异常返回的字符串
      • exception属性:用来设置你需要捕获的异常类型

    • action

    • name属性:设置请求的Action地址(可以用通配符 http://localhost:8080/项目名称/PhoneAction_* )
    • class属性:设置请求的类(包名+类名)
    • method属性::设置请求的方法名(可支持通配符: http://localhost:8080/项目名称/PhoneAction_* method={1})

    • result (子元素):根据返回值跳转相对应的页面(找不到在全局结果集里面找)

      • name属性:用来匹配返回的字符串
      • type属性:设置跳转页面的方式(取值如下:默认为dispatcher)

            chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息.    dispatcher:用来转向页面,通常处理JSP.    freemaker:处理FreeMarker模板.    httpheader:控制特殊HTTP行为的结果类型.    redirect:重定向到一个URL,被跳转的页面中丢失传递的信息,如request.    redirectAction:重定向到一个Action,跳转的页面中丢失传递的信息.    stream:向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据.    velocity:处理Velocity模板.    xslt:处理XML/XLST模板.    plainText:显示原始文件内容,例如文件源代码.
<?xml version="1.0" encoding="UTF-8"?>   <!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>    <package name="crm" namespace="/" extends="struts-default">        <global-results>            <result name="error">/error.jsp</result>        </global-results>        <global-exception-mappings>            <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>        </global-exception-mappings>        <action name="PhoneAction_*" class="xxx.PhoneAction" method="{1}">            <result name="list" type="redirect">/xxx.jsp</result>        </action>    </package></struts>

struts2配置web.xml

  • 配置struts2核心Filter
    <filter>        <filter-name>struts</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>
原创粉丝点击