struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4. <constant name="struts.ui.theme" value="simple"></constant>
  5.  
  6. <package name="rx" extends="struts-default" namespace="/*">
  7. <action name="*_*" class="{1}Action" method="{2}">
  8. <result name="success">${successResultValue}</result>
  9. <result name="redirect" type="redirectAction" >${redirectResultValue}</result>
  10. </action>
  11. </package>
  12. </struts>

BaseAction.java

  1. package com.yl.action;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6.  
  7. import org.apache.struts2.ServletActionContext;
  8.  
  9. import com.opensymphony.xwork2.ActionSupport;
  10.  
  11. public class BaseAction extends ActionSupport {
  12.  
  13. /**
  14. * 序列化ID
  15. */
  16. private static final long serialVersionUID = 1L;
  17.  
  18. protected String successResultValue;
  19.  
  20. public String getSuccessResultValue() {
  21. return successResultValue;
  22. }
  23.  
  24. public void setSuccessResultValue(String successResultValue) {
  25. this.successResultValue = successResultValue;
  26. }
  27.  
  28. protected String redirectResultValue;
  29.  
  30. public String getRedirectResultValue() {
  31. return redirectResultValue;
  32. }
  33.  
  34. public void setRedirectResultValue(String redirectResultValue) {
  35. this.redirectResultValue = redirectResultValue;
  36. }
  37.  
  38. protected String chainResultValue;
  39.  
  40. public String getChainResultValue() {
  41. return chainResultValue;
  42. }
  43.  
  44. public void setChainResultValue(String chainResultValue) {
  45. this.chainResultValue = chainResultValue;
  46. }
  47.  
  48. /**
  49. * 返回request对象
  50. *
  51. * @return
  52. */
  53. protected HttpServletRequest getRequest() {
  54. return ServletActionContext.getRequest();
  55. }
  56.  
  57. /**
  58. * 返回response对象
  59. *
  60. * @return
  61. */
  62. protected HttpServletResponse getResponse() {
  63. return ServletActionContext.getResponse();
  64. }
  65.  
  66. /**
  67. * 返回session对象
  68. *
  69. * @return
  70. */
  71. protected HttpSession getSession() {
  72. return getRequest().getSession();
  73. }
  74. }

一个样例action:

  1. package com.yl.action;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10.  
  11. import javax.annotation.Resource;
  12.  
  13. import org.springframework.context.annotation.Scope;
  14. import org.springframework.stereotype.Component;
  15.  
  16. import com.yl.biz.ImgBiz;
  17. import com.yl.config.SysConfig;
  18. import com.yl.cons.UploadResult;
  19. import com.yl.entity.Img;
  20. import com.yl.util.CipherUtil;
  21.  
  22. @Component("imgAction")
  23. @Scope("prototype")
  24. /**
  25. * 图片Action
  26. */
  27. public class ImgAction extends BaseAction {
  28.  
  29. /**
  30. * 序列化ID
  31. */
  32. private static final long serialVersionUID = 1L;
  33.  
  34. /**
  35. * 路径
  36. */
  37. private File upload;
  38.  
  39. public File getUpload() {
  40. return upload;
  41. }
  42.  
  43. public void setUpload(File upload) {
  44. this.upload = upload;
  45. }
  46.  
  47. /**
  48. * 图片实体
  49. */
  50. private Img img;
  51.  
  52. public Img getImg() {
  53. return img;
  54. }
  55.  
  56. public void setImg(Img img) {
  57. this.img = img;
  58. }
  59.  
  60. /**
  61. * 注入
  62. */
  63. @SuppressWarnings("unused")
  64. @Resource(name = "imgBiz")
  65. private ImgBiz imgBiz;
  66.  
  67. /**
  68. * 图片上传
  69. *
  70. * @return
  71. */
  72. @SuppressWarnings("deprecation")
  73. public String uploadFile() {
  74. @SuppressWarnings("unused")
  75. String paths = getRequest().getRealPath("/");
  76. // 推断路径是否为空
  77. if (this.upload == null || !this.upload.isFile()
  78. || !this.upload.canRead()) {
  79. this.addActionError(UploadResult.NULLFILE.getTitle());
  80. return SUCCESS;
  81. }
  82. try {
  83. FileInputStream fis = new FileInputStream(this.upload);
  84. // 获得图片大小
  85. int fileSize = fis.available();
  86. // 推断图片大小
  87. if (fileSize > SysConfig.MAX_FILE_SIZE) {
  88. this.addActionError(UploadResult.TooLargeFile.getTitle());
  89. } else {
  90. // 保存路径
  91. String path = SysConfig.PUBLIC_PATH;
  92. // 获取图片名字
  93. String fileName = this.upload.getName();
  94. // 推断是否有点
  95. if (!fileName.contains(".")) {
  96. this.addActionError(UploadResult.NOTIMG.getTitle());
  97. }
  98. // 截取文件类型
  99. fileName = fileName.substring(fileName.lastIndexOf("."));
  100. SimpleDateFormat sdf = new SimpleDateFormat(
  101. "yyyy-MM-dd HH:mm:ss aa");
  102. // 从新给图片命名
  103. fileName = CipherUtil.md5Encoding(sdf.format(new Date()))
  104. + fileName;
  105. // 保存图片路径+图片名字
  106. fileName = path + "\\" + fileName;
  107. // New一个新的地址.
  108. File file = new File(fileName);
  109. // 输出图片到新的地址
  110. FileOutputStream fos = new FileOutputStream(file);
  111. int c;
  112. byte b[] = new byte[4 * 1024];
  113. while ((c = fis.read(b)) != -1) {
  114. fos.write(b, 0, c);
  115. }
  116. // 关闭相关操作
  117. fos.flush();
  118. fis.close();
  119. // 保存成功信息
  120. this.addActionError(UploadResult.SUCCESS.getTitle());
  121. }
  122. } catch (FileNotFoundException e) {
  123. // 保存失败信息
  124. this.addActionError(UploadResult.NULLFILE.getTitle());
  125. e.printStackTrace();
  126. } catch (IOException e) {
  127. // 保存失败信息
  128. this.addActionError(UploadResult.UPLOADFAIL.getTitle());
  129. e.printStackTrace();
  130. }
  131. // 设置返回页面
  132. setSuccessResultValue("/index.jsp");
  133. return SUCCESS;
  134.  
  135. }
  136.  
  137. }

常量:

  1. package com.yl.config;
  2.  
  3. import org.apache.struts2.ServletActionContext;
  4. import org.springframework.beans.factory.InitializingBean;
  5.  
  6. /**
  7. * 图片系统类
  8. *
  9. */
  10. public class SysConfig implements InitializingBean {
  11.  
  12. public static Boolean IS_DEBUG = true;
  13.  
  14. /**
  15. * 上传图片大小控制
  16. */
  17. public static int MAX_FILE_SIZE = 1024 * 1024 * 100;
  18.  
  19. /**
  20. * 上传图片路径控制
  21. */
  22.  
  23. @SuppressWarnings("deprecation")
  24. public static String PUBLIC_PATH = ServletActionContext.getRequest().getRealPath("/") + "upload";
  25.  
  26. private SysConfig() {
  27. }
  28.  
  29. public static boolean isDebug() {
  30. return IS_DEBUG != null && IS_DEBUG;
  31. }
  32.  
  33. public void setIS_DEBUG(Boolean iS_DEBUG) {
  34. IS_DEBUG = iS_DEBUG;
  35. }
  36.  
  37. public void afterPropertiesSet() throws Exception {
  38. }
  39. }

一个错误的结果enum封装,使用见action:

  1. package com.yl.cons;
  2.  
  3. /**
  4. * 上传返回标志
  5. *
  6. */
  7. public enum UploadResult {
  8. SUCCESS((short) 0, "成功"), NULLFILE((short) 1, "找不到文件"), NOTIMG((short) 2,
  9. "非图片文件"), UPLOADFAIL((short) 3, "上传失败"), TooLargeFile((short) 4,
  10. "文件过大");
  11. /**
  12. * 错误类型
  13. */
  14. private short type;
  15.  
  16. /**
  17. * 错误名称
  18. */
  19. private String title;
  20.  
  21. private UploadResult(short type, String title) {
  22. this.type = type;
  23. this.title = title;
  24. }
  25.  
  26. public short getType() {
  27. return type;
  28. }
  29.  
  30. public String getTitle() {
  31. return title;
  32. }
  33.  
  34. }

struts2 一个简洁的struts.xml的更多相关文章

  1. Struts2系列笔记(2)---Struts.XML

    Struts2.xml 本篇博客主要讲Struts2.xml中package下的标签和标签属性,主要分以下四个部分说明: (1)action的配置基本属性 (2)同一个Action类中不同方法满足不同 ...

  2. struts2简单入门-配置文件-struts.xml

    struts.xml 作用:配置struts中的action,result,package,全局action,result,等等. 基本文件格式: <?xml version="1.0 ...

  3. struts2核心配置之struts.xml

    struts.xml -常量配置 -包配置 -包含配置 一.常量配置 struts2常量的配置通常采用三种方式: 1.在struts.xml中使用<constant>元素配置常量 < ...

  4. struts2 从一个action跳转到另一个action的struts.xml文件的配置

    解释: 想要用<result>跳转到另一个action,原来的配置代码是: <action name="insertDept" class="strut ...

  5. struts2:struts.xml配置文件详解

    1. 几个重要的元素 1.1 package元素 package元素用来配置包.在Struts2框架中,包是一个独立的单位,通过name属性来唯一标识包.还可以通过extends属性让一个包继承另一个 ...

  6. Struts2初学 struts.xml详解 一

    一.简介    Struts 2是一个MVC框架,以WebWork设计思想为核心,吸收了Struts 1的部分优点 二.详解    首先让我们看一下一个简单的struts.xml文件的结构  < ...

  7. Struts.xml讲解

    解决在断网环境下,配置文件无提示的问题我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/d ...

  8. Struts2的一个问题: 找不到struts.xml的路径问题

    一. 最近在学习Struts2的一些知识,在使用Struts2搭建框架的时候,部署到服务器上的时候出现上面的问题: 三月 19, 2016 1:43:24 下午 org.apache.tomcat.u ...

  9. struts2学习笔记--struts.xml配置文件详解

    这一节主要讲解struts2里面的struts.xml的常用标签及作用: 解决乱码问题 <constant name="struts.i18n.encoding" value ...

随机推荐

  1. C#构架之基础学习----动态添加窗体和 控件

    仿照窗体应用程序编写: 任务一:生成一个Form类的窗体对象frm using System.Windows.Forms;         //using指令使用Form对象创建所需的命名空间 //如 ...

  2. ASP.NET MVC 5 学习教程:数据迁移之添加字段

    原文 ASP.NET MVC 5 学习教程:数据迁移之添加字段 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符 ...

  3. lucene 索引查看工具

    luke 是 lucene 索引查看工具,基于 swing 开发的,是 lucene.solr.nutch 开发过程中不可或缺的工具.在测试搜索过程,进程出现搜不到东西或者搜到的东西不是想要的结果时, ...

  4. Computer Transformation(规律,大数打表)

    Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  5. 【转】CentOS6.5 增加一个SFTP上传的用户

    原文链接地址:http://www.msits.com/archives/4477.html #创建sftp组groupadd sftp#创建一个用户zjhpuseradd -g sftp -s /b ...

  6. Python之路day4

    坚持就是胜利.今天零下14度,从教室出来的路上真的很冷很冷,希望这个冬天自己不会白过,春暖花开的时候一定要给世界一个更好的自己. 原本以为day3的作业自己做得挺好的,没想到只得了B+.必须要加油了, ...

  7. 32位程序在64位系统上获取系统安装时间(要使用KEY_WOW64_64KEY标记)

    众所周知,取系统的安装时间可取注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion的子项InstallDate,此值是个 ...

  8. InputStream中read()与read(byte[] b)

    原文:InputStream中read()与read(byte[] b) read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK ...

  9. Git命令非主流札记

    使用git做开发的版本管理也有一年半之多了,但是始终都是常用的branch commit status diff push等一些再常用不过的命令,最近闲下来,打算学习一下高端用法,所以就静下心来好好读 ...

  10. oracle 11gR2 在VM中安装步骤

    oacle的安装 一.在oracle官网可以免费下载oracle的软件和安装文档,如果是在虚拟机中的linux系统里安装,可以用FileZilla Client把软件发送到系统中. linux_11g ...