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.ui.theme" value="simple"></constant> <package name="rx" extends="struts-default" namespace="/*">
<action name="*_*" class="{1}Action" method="{2}">
<result name="success">${successResultValue}</result>
<result name="redirect" type="redirectAction" >${redirectResultValue}</result>
</action>
</package>
</struts>

BaseAction.java

package com.yl.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class BaseAction extends ActionSupport { /**
* 序列化ID
*/
private static final long serialVersionUID = 1L; protected String successResultValue; public String getSuccessResultValue() {
return successResultValue;
} public void setSuccessResultValue(String successResultValue) {
this.successResultValue = successResultValue;
} protected String redirectResultValue; public String getRedirectResultValue() {
return redirectResultValue;
} public void setRedirectResultValue(String redirectResultValue) {
this.redirectResultValue = redirectResultValue;
} protected String chainResultValue; public String getChainResultValue() {
return chainResultValue;
} public void setChainResultValue(String chainResultValue) {
this.chainResultValue = chainResultValue;
} /**
* 返回request对象
*
* @return
*/
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
} /**
* 返回response对象
*
* @return
*/
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
} /**
* 返回session对象
*
* @return
*/
protected HttpSession getSession() {
return getRequest().getSession();
}
}

一个样例action:

package com.yl.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.annotation.Resource; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.yl.biz.ImgBiz;
import com.yl.config.SysConfig;
import com.yl.cons.UploadResult;
import com.yl.entity.Img;
import com.yl.util.CipherUtil; @Component("imgAction")
@Scope("prototype")
/**
* 图片Action
*/
public class ImgAction extends BaseAction { /**
* 序列化ID
*/
private static final long serialVersionUID = 1L; /**
* 路径
*/
private File upload; public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} /**
* 图片实体
*/
private Img img; public Img getImg() {
return img;
} public void setImg(Img img) {
this.img = img;
} /**
* 注入
*/
@SuppressWarnings("unused")
@Resource(name = "imgBiz")
private ImgBiz imgBiz; /**
* 图片上传
*
* @return
*/
@SuppressWarnings("deprecation")
public String uploadFile() {
@SuppressWarnings("unused")
String paths = getRequest().getRealPath("/");
// 推断路径是否为空
if (this.upload == null || !this.upload.isFile()
|| !this.upload.canRead()) {
this.addActionError(UploadResult.NULLFILE.getTitle());
return SUCCESS;
}
try {
FileInputStream fis = new FileInputStream(this.upload);
// 获得图片大小
int fileSize = fis.available();
// 推断图片大小
if (fileSize > SysConfig.MAX_FILE_SIZE) {
this.addActionError(UploadResult.TooLargeFile.getTitle());
} else {
// 保存路径
String path = SysConfig.PUBLIC_PATH;
// 获取图片名字
String fileName = this.upload.getName();
// 推断是否有点
if (!fileName.contains(".")) {
this.addActionError(UploadResult.NOTIMG.getTitle());
}
// 截取文件类型
fileName = fileName.substring(fileName.lastIndexOf("."));
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss aa");
// 从新给图片命名
fileName = CipherUtil.md5Encoding(sdf.format(new Date()))
+ fileName;
// 保存图片路径+图片名字
fileName = path + "\\" + fileName;
// New一个新的地址.
File file = new File(fileName);
// 输出图片到新的地址
FileOutputStream fos = new FileOutputStream(file);
int c;
byte b[] = new byte[4 * 1024];
while ((c = fis.read(b)) != -1) {
fos.write(b, 0, c);
}
// 关闭相关操作
fos.flush();
fis.close();
// 保存成功信息
this.addActionError(UploadResult.SUCCESS.getTitle());
}
} catch (FileNotFoundException e) {
// 保存失败信息
this.addActionError(UploadResult.NULLFILE.getTitle());
e.printStackTrace();
} catch (IOException e) {
// 保存失败信息
this.addActionError(UploadResult.UPLOADFAIL.getTitle());
e.printStackTrace();
}
// 设置返回页面
setSuccessResultValue("/index.jsp");
return SUCCESS; } }

常量:

package com.yl.config;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.InitializingBean; /**
* 图片系统类
*
*/
public class SysConfig implements InitializingBean { public static Boolean IS_DEBUG = true; /**
* 上传图片大小控制
*/
public static int MAX_FILE_SIZE = 1024 * 1024 * 100; /**
* 上传图片路径控制
*/ @SuppressWarnings("deprecation")
public static String PUBLIC_PATH = ServletActionContext.getRequest().getRealPath("/") + "upload"; private SysConfig() {
} public static boolean isDebug() {
return IS_DEBUG != null && IS_DEBUG;
} public void setIS_DEBUG(Boolean iS_DEBUG) {
IS_DEBUG = iS_DEBUG;
} public void afterPropertiesSet() throws Exception {
}
}

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

package com.yl.cons;

/**
* 上传返回标志
*
*/
public enum UploadResult {
SUCCESS((short) 0, "成功"), NULLFILE((short) 1, "找不到文件"), NOTIMG((short) 2,
"非图片文件"), UPLOADFAIL((short) 3, "上传失败"), TooLargeFile((short) 4,
"文件过大");
/**
* 错误类型
*/
private short type; /**
* 错误名称
*/
private String title; private UploadResult(short type, String title) {
this.type = type;
this.title = title;
} public short getType() {
return type;
} public String getTitle() {
return title;
} }

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. ubuntu卸载vmware

    下面方法摘抄网上方法,经过证实好使 lsmod——显示已载入系统的模块 ps aux : 显示其他用户启动的进程(a)   查看系统中属于自己的进程(x)         启动这个进程的用户和它启动的 ...

  2. Irrlicht学习之光照的研究

    Irrlicht学习之光照的研究 最近研究一下Irrlicht的光照.发现Irrlicht的光照还是比较简单的,相比低于它的OpenGL和Direct3D,设置光源以及设置光照的参数更加人性化(可能是 ...

  3. ZooKeeper原理及配置

    ooKeeper实验版本:3.4.6 ZooKeeper下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ zookeeper是一个高可用性,高性 ...

  4. [置顶] 浏览器模式和标准对于javascript的影响

    今天在编写代码的时候遇到了一个莫名其妙的问题,请看下面 <html> <head> <title> Test </title> <!--<m ...

  5. Python笔记之基本的语法

    1 变量和赋值 Python是动态类型语言,不须要预先声明变量的类型.变量的类型在赋值的那一刻被初始化. Python变量名是大写和小写敏感的,即"cAsE"与"CaSe ...

  6. pthread_detach(pthread_self())

    pthread_detach(pthread_self()) 将状态改为unjoinable状态,确保资源的释放.其实简单的说就是在线程函数头加上 pthread_detach(pthread_sel ...

  7. ASP.NET aspx页面中 写C#脚本; ASP.NET 指令(<%@%>);

    1 <h2>Welcome</h2> <ul> <% for (int i = 0; i <= Convert.ToInt32(ViewData[&qu ...

  8. jsp生命周期和工作原理

    jsp的工作原理jsp是一种Servlet,但是与HttpServlet的工作方式不太一样.httpservlet是先由源代码编译为class文件后部署到服务器下的,先编译后部署.而jsp则是先部署后 ...

  9. Linux下Nginx+tomcat应用系统性能优化

    软件环境及服务器配置如下: Linux rh6.3,Tomcat7.0.29,Nginx1.2.7 mysql5.1,jdk1.6.0 mysql5.1 memcached 1.4.15 Xeno 2 ...

  10. LVS--什么是LVS?

    1.什么是LVS? 首先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和基于内容请求分发技术.调度器具有很 ...