Struts开发笔记二

Struts2

配置细节问题

  • struts.xml中默认配置

    <default-action-ref name="default"></default-action-ref>
    <default-class-ref class="com.example.struts2.DefaultAction"> </default-class-ref>
    
    <action name="default">
        <result name="success">/index.jsp</result>
    </action>
    
    • 通用界面配置 —如果某个action中没有对应的result,默认跳转到default.jsp中

      <global-results>
          <result>/default.jsp</result>
      </global-results>
      
  • 全局异常

    <global-exception-mappings>
        <exception-mapping result="error" exception="java.lang.Exception"
            name="ErrorAction"></exception-mapping>
    </global-exception-mappings>
    
  • 常用常量配置

    <constant name="struts.action.extension" value="action,do,"/> 
    <constant name="struts.devMode" value="true" />  提供详细报错页面,修改struts.xml后不需要重启服务器 (要求)
    <constant name="struts.serve.static.browserCache" value="false"/> false不缓存,true浏览器会缓存静态内容,产品环境设置true、开发环境设置false 
    
  • struts.xml模块分离

    <include file="struts-user.xml"></include>
    复制 创建struts-user的xml文件
    

文件上传

企业常用文件上传技术 : jspSmartUpload(主要应用 JSP model1 时代) 、
fileupload (Apache commons项目中一个组件)、
Servlet3.0 集成文件上传 Part类
文件上传 enctype=”multipart/form-data”
是 MIME协议定义多部分请求体 (消息体)

private File upload;
private String uploadContentType;
private String uploadFileName;

public void setUpload(File upload) {
    this.upload = upload;
}

public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
}

public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
}

@Override
public String execute() throws Exception {
    // 查询
    String realPath = ServletActionContext.getServletContext().getRealPath(
            "/upload");
    // 路径
    String targetPath = realPath + File.separator + uploadFileName;
    // 开始上传
    File targetFile = new File(targetPath);
    FileUtils.copyFile(upload, targetFile);

    return NONE;
}

多文件上传

private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;

// 查询
String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
for (int i = 0; i < uploadFileName.length; i++) {
    String targetPath = realPath + File.separator + uploadFileName[i];
    File targetFile = new File(targetPath);
    FileUtils.copyFile(upload[i], targetFile);
        }
ServletActionContext.getRequest().setAttribute("name", "上传成功");

案例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值