SSH学习--struts的action中BaseAction的作用

通常struts中的action一般都是继承ActionSupport的

public void loginCheck() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			out.println(userLogic.loginCheck(userDTO.getUserLoginName(), userDTO.getUserPwd()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}

而发现有些项目中的则是继承BaseAction的

public void loginCheck() throws Exception {
		PrintWriter out = getWriter();
		try {
			accountLogic.getAllAccount();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.close();
		}

	}

ActionSupport是struts2自带的类, BaseAction是自己的一个封装
这是BaseAction中的内容:
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	PrintWriter getWriter() throws IOException {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		return response.getWriter();
	}
	
	HttpSession getSession(){		
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getSession();	
	}

}

可以看出BaseAction1也是继承自ActionSupport的,只是里面 增加一些公用的属性和方法,例如获取httpRequest,又比如获取用户信息的方法,完全是自己封装的。

这样做避免每个action甚至每个方法都要写这些,增加了代码的复用性。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值