strusts2对于action请求的编码优化
来源:互联网 发布:filesaver.js保存文件 编辑:程序博客网 时间:2024/06/05 14:50
大家对struts2的使用都很了解,每个请求对应一个action这样就会增多很多的配置,所有的action类都是继承了ActionSupport,而大部分人都会写一个自己的BaseAction,处理一些文件上传下载request,response、分页的处理。在这里分享一个处理action请求的小技巧。
代码
/** * <p> * Title: Admin Action基类 * * @author huicheng yang * @version 1.0 */public class BaseAdmin extends ActionSupport {/** * */private static final long serialVersionUID = -7728309960568975048L;private static final Log logger = LogFactory.getLog(BaseAdmin.class);private String remoteAddress;private String refererUrl;private AdminCookie adminCookie;private String curPath;private String curAction;private HttpServletRequest request;private HttpServletResponse response;private String c = "index";/** * 页码 */private int curPage = 1;@SuppressWarnings("unchecked")protected String executeMethod(String method) {try {Class[] c = null;Method m = this.getClass().getMethod(method, c);Object[] o = null;String result = (String) m.invoke(this, o);return result;} catch (Throwable e) {e.printStackTrace();logger.error("executeMethod", e);return ERROR;}}public String execute() {try {return this.executeMethod(this.getC());} catch (Throwable e) {logger.error("executeMethod", e);return ERROR;}}public String getRemoteAddress() {return remoteAddress;}public void setRemoteAddress(String remoteAddress) {this.remoteAddress = remoteAddress;}public HttpServletRequest getRequest(){if (request == null) {request = ServletActionContext.getRequest();}return request;}public HttpServletResponse getResponse(){if (response == null) {response = ServletActionContext.getResponse();}return response;}/** * @return the curPage */public int getCurPage() {return curPage;}/** * @param curPage * the curPage to set */public void setCurPage(int curPage) {this.curPage = curPage;}public String getRefererUrl() {return refererUrl;}public void setRefererUrl(String refererUrl) {this.refererUrl = refererUrl;}public String getC() {return c;}public void setC(String c) {this.c = c;}public String getCurPath() {return curPath;}public void setCurPath(String curPath) {this.curPath = curPath;}public String getCurAction() {return curAction;}public void setCurAction(String curAction) {this.curAction = curAction;}public AdminCookie getAdminCookie() {return adminCookie;}public void setAdminCookie(AdminCookie adminCookie) {this.adminCookie = adminCookie;}}在baseaction里声明一个c变量代表要提交的action的方法名
protected String executeMethod(String method) {try {Class[] c = null;Method m = this.getClass().getMethod(method, c);Object[] o = null;String result = (String) m.invoke(this, o);return result;} catch (Throwable e) {e.printStackTrace();logger.error("executeMethod", e);return ERROR;}}
通过代理调用的方法
public String execute() {try {return this.executeMethod(this.getC());} catch (Throwable e) {logger.error("executeMethod", e);return ERROR;}}在execute方法中使用executeMethod(),传入需要调用的方法名。
我们的其他的action类都继承baseaction这样在前台的调用的时候我们不用在strusts配文件配置多个action方法,localhost:8080/[项目名]/User.action?c=login只需要传入一个参数代表调用方法名即可 execute()方法会根据我们的传入的参数名称调用相应的方法。
如果项目用的是annotations那我我们只需要创建好action类定义相应的方法就可以了。。
- strusts2对于action请求的编码优化
- strusts2下Action收集表单数据的方法概述
- strusts2.0 Action Configuration(Action配置)
- 对于Servlet的get请求和post请求的两种数据请求的编码格式
- strusts2
- Strusts2数据校验的方法一(通过Action中的validate()方法实现)
- Strusts2框架的基本介绍
- 使用 springMVC 请求Action 返回400编码
- Action请求的写法
- 对于SPFA的优化
- 对于SPFA的优化
- 对于编码的初步认识
- 对于Http的编码统一
- Strusts2+Hibernate4+Spring3的框架整合
- 防止action的get请求
- 对于网站优化的意思
- 对于网站优化的意思
- 对于DAO的代码优化
- 一个简单的生成静态页的方法
- Changes to Remote Administration in Windows Server 2008
- xliff XML本地化数据交换格式
- struts2+SiteMesh(全局统一装饰布局)
- 分布式搜索Elasticsearch——创建客户端(三)
- strusts2对于action请求的编码优化
- android 华为桌面一直崩溃,强行关闭的解决办法。
- linux awk 运算符
- 少编码多思考:代码越多 问题越多
- 关于DVCS
- 百度搜索结果页面的参数 原先查询字符串(oq)
- 用SSH集成框架开发项目步骤
- openstack杂记
- SGA--Database Buffer Cache