struts2 一个简洁的struts.xml

struts2 一个简洁的struts.xml

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

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;
	}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118278.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ASP.NET验证控件之RangeValidator「建议收藏」

    ASP.NET验证控件之RangeValidator「建议收藏」RangeValidator控件用于检测用户输入的值是否介于两个值之间。可以对不同类型的值进行比较,比如数字、日期以及字符。我们一般会用来验证输入的年龄或者考试的分数等。下面我们一块看看RangeValidator的属性:属性描述 BackColor 背景颜色 ControlToValidate

    2022年7月12日
    13
  • linux双网卡架设FTP,LINUX系统上架设FTP服务器[通俗易懂]

    linux双网卡架设FTP,LINUX系统上架设FTP服务器[通俗易懂]CentOS上搭建FTP服务器服务器软件:vsftpd简要说明:vsftpd是linux下的一款小巧轻快,安全易用的FTP服务器软件,是一款在各个LINUX发行版中最受推崇的FTP服务器软件。至于它的安装教程,网络上也是数不胜数,每个教程都有各自的优缺点,祥哥特意做了个总结,取别人之长处,尽量做到菜鸟级别的教程。当你看见祥哥的这篇文章,能更好的使用和运用VSFTPD。下面正题开始。安装vsftpd…

    2022年7月21日
    12
  • nginx配置ssl证书实现https访问_更换ssl证书

    nginx配置ssl证书实现https访问_更换ssl证书1,登录阿里云,工作台找SSL证书或者安全下找CA证书2,点击创建证书(或购买证书),创建好以后点击证书申请、3,设置配置以及域名信息,仅填写圈住内容,其他默认即可4,随后等待一会,查看状态,是否为 已签发5,为已签发时,点击下载选择下载类型6,下载后解压文件7,上传至服务器,存放位置,先找到nginx所在位置 “/nginx/conf/”找到该位置创建“cert”把刚才解压的两个文件存放至此。8,开始nginx配置内容`server { #SSL 访问端口号为 443 li

    2022年8月19日
    12
  • 1.如何实现MT4帐号同步交易?

    1.如何实现MT4帐号同步交易?使用本跟单EA,可以实现在同一台计算机上运行两个(或更多个)MetaTrader4自动复制交易。您使用其中一个MT4帐号手动交易或者使用EA自动交易,那么其它一个(或更多个)MT4,会立即复制此帐号中的订单。您可以运行多个喊单EA和多个跟单EA,可以实现一个帐号跟多个帐号,或者多个帐号跟一个帐号,又或者多个帐号跟多个帐号。用来喊单的MT4帐号不需要帐号必须拥交易权限,因此,可以使用MT4“投资者”密码登录。投资者密码,又称呼“只读密码、观摩密码”。…

    2022年5月30日
    38
  • nuxt「建议收藏」

    nuxt「建议收藏」Nuxt.js是一个基于Vue.js的通用应用框架。通过对客户端/服务端基础架构的抽象组织,Nuxt.js主要关注的是应用的 UI渲染。Nuxt.js预设了利用Vue.js

    2022年8月3日
    10
  • kali激活成功教程软件_kali渗透教程

    kali激活成功教程软件_kali渗透教程转载请注明出处:https://blog.csdn.net/l1028386804/article/details/84895163VeilEvasion简介VeilEvasion是一个可执行文件,它被用来生成Metasploit的payload,能绕过常见杀软。免责声明:本教程目的只是为了教育,我们不对这些东西会如何使用担任何风险,使用它的后果自负。Veil-Evasion被…

    2022年8月20日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号