精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-07-16
如果为第个Form都建一个Action那是相当麻烦的, 如果像下面这样构建啦一个通用的Action会带来什么好处? 而像平常的做法是几乎是一个Form 和 Action的这种做法又有什么好处? 下面是源码和相关的解释 请大家先看一下Struts-config.xml 中的一部分内容 xml 代码
而在其它的Bean中的方法也如同下面的这个方法,都是返回一个字符串 而在AccountBean.java中的方法如下 java 代码
下面的是通用的 BeanAction.java 类 java 代码
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-07-16
你还是看看dispathAction和dynamicFormBean吧
|
|
返回顶楼 | |
发表时间:2007-07-16
Map model = JspUtil.getRequestModel(request); //把request参数塞到map里 ORM.insert(tableName,model); //ORM.update(tableName,model); //orm 是diy的ORM小工具 |
|
返回顶楼 | |
发表时间:2007-07-16
要想通用太难了。。就我个人在开发中碰到的问题,动态方法,include,download等action的功能要整合在一起就很麻烦。至于在dispatchAction下,由于form的invalidate方法出错后,返回的input页面如何解决
|
|
返回顶楼 | |
发表时间:2007-07-16
flxi78 写道 要想通用太难了。。就我个人在开发中碰到的问题,动态方法,include,download等action的功能要整合在一起就很麻烦。至于在dispatchAction下,由于form的invalidate方法出错后,返回的input页面如何解决
其实这就是一个通用的Action 我是在Jpetstore上分析源码时发现的,觉得很好就发上来与大家共享 你上面所说的返回input页面如何解决? 因上面有一段代码我把它注释啦 ActionContext.save(request.response); 这是把所传过来的 request和response进行保存 而当页面出错的时候是通过ActionContext来保存的,并且是在login()方法中进行保存 由于关联的代码太多,我就只发这个最主要的上来 |
|
返回顶楼 | |
发表时间:2007-07-17
ActionContext是struts 1.X提供的类么?我好像找不到。
另外,我看楼主的思路是通过自己写一个bean(这个bean还没有继承ActionFrom,我记得好像struts调用这个bean的时候,会报错。),然后在bean当中定义业务方法。最后通过反射方式去访问bean的业务方法. 我个人从类的数量上来说,form是没有减少工作量的,但是Action的确可以减少一些。但是这样的做法引起更多的一些不方便。因为你的业务方法,比如说login是在bean里面的,那如何访问一些request和response的东西?比如request.getParameterValues,该问题或许可以在反射调用时把request, response都传到bean的业务方法里去来解决;那么又如何访问一些Action提供的一些方法呢?比如action.getLocale(Request reqeust)? 我们也得在反射调用时把action传到bean的业务方法里? 那么如果这样的话,struts1.X提供的一些action,比如IncludeAction,DownloadAction,DispatchAction,我们怎么使用? 楼主想通过一个Action,把由此引起的调用压力全部释放到bean上去,我还是会觉得这个bean的代码会比较凌乱。 |
|
返回顶楼 | |
发表时间:2007-07-17
ActionContext不是Struts当中的类,
这是一个自定义的类,这个类用来保存一些错误的信息 初始化request和response 你所说的在Bean中的业务方法是增加啦不少的代码~ 至于如何调用action的一些方法,这里没有仔细想过 你说到的这几个问题很好,点醒啦我 |
|
返回顶楼 | |
发表时间:2007-07-17
这样做是不是让MVC结构更模糊了?Action应该是显示层的组间,返回值forward定义了要将结过呈现出来的页面。
由后台Bean来决定forward的值,增加了显示层和后台模型层的耦合。这样会影响到系统组间的可重用性。对项目的成本和效率都会造成一定影响吧。 |
|
返回顶楼 | |
发表时间:2007-07-17
要避免写 Action 是不可能的,最好是写一个项目中通用的 Action,提供大量已实现的方法。其他 Action 都从此继承,这样要好写得多。
(不要引用这个回复啊,会死人的) java 代码
|
|
返回顶楼 | |
发表时间:2007-08-22
<div class="code_title">要避免写 Action 是不可能的,最好是写一个项目中通用的 Action,提供大量已实现的方法。其他 Action 都从此继承,这样要好写得多。<br />
<br /> (不要引用这个回复啊,会死人的)<br /> <br /> java 代码</div> <div class="dp-highlighter"> <div class="bar"> </div> <ol start="1" class="dp-j"> <li class="alt"><span><span class="keyword">package</span><span> com.chinacreator.ccimp.common.struts; </span></span></li> <li class=""><span> </span></li> <li class="alt"><span><span class="keyword">import</span><span> com.chinacreator.ccimp.common.ErrorReporter; </span></span></li> <li class=""><span><span class="keyword">import</span><span> com.chinacreator.ccimp.common.page.PageHelper; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> com.chinacreator.ccimp.CcimpException; </span></span></li> <li class=""><span><span class="keyword">import</span><span> com.chinacreator.commons.beans.NameValuePair; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> com.chinacreator.commons.utils.StringUtil; </span></span></li> <li class=""><span><span class="keyword">import</span><span> com.chinacreator.security.AccessControl; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> org.apache.log4j.Logger; </span></span></li> <li class=""><span><span class="keyword">import</span><span> org.apache.struts.action.Action; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> org.apache.struts.action.ActionForm; </span></span></li> <li class=""><span><span class="keyword">import</span><span> org.apache.struts.action.ActionForward; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> org.apache.struts.action.ActionMapping; </span></span></li> <li class=""><span><span class="keyword">import</span><span> org.apache.commons.fileupload.FileItem; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> org.apache.commons.fileupload.FileItemFactory; </span></span></li> <li class=""><span><span class="keyword">import</span><span> org.apache.commons.fileupload.FileUploadException; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> org.apache.commons.fileupload.servlet.ServletFileUpload; </span></span></li> <li class=""><span><span class="keyword">import</span><span> org.apache.commons.fileupload.disk.DiskFileItemFactory; </span></span></li> <li class="alt"><span> </span></li> <li class=""><span><span class="keyword">import</span><span> javax.servlet.http.HttpServletRequest; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> javax.servlet.http.HttpServletResponse; </span></span></li> <li class=""><span><span class="keyword">import</span><span> javax.servlet.ServletException; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> java.util.ArrayList; </span></span></li> <li class=""><span><span class="keyword">import</span><span> java.util.Enumeration; </span></span></li> <li class="alt"><span><span class="keyword">import</span><span> java.util.List; </span></span></li> <li class=""><span><span class="keyword">import</span><span> java.io.IOException; </span></span></li> <li class="alt"><span> </span></li> <li class=""><span><span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 信息平台使用的 struts Action 类。本类包含大量方法方便处理请求、校验参数、写业务日志等。</span> </span></li> <li class=""><span><span class="comment"> * <p/></span> </span></li> <li class="alt"><span><span class="comment"> * 使用时只需实现 execute 方法即可。</span> </span></li> <li class=""><span><span class="comment"> * <p/></span> </span></li> <li class="alt"><span><span class="comment"> * 本类已经实现对访问权限的控制,不需要再写。</span> </span></li> <li class=""><span><span class="comment"> * 如果要获取参数,使用 getParam() 方法即可。</span> </span></li> <li class="alt"><span><span class="comment"> * 如果要向 request 存储数据,使用 setAttribute() 方法即可。</span> </span></li> <li class=""><span><span class="comment"> * 如果要校验参数,实现 validate() 方法即可。validate() 方法没有返回值,校验失败时请抛出异常。</span> </span></li> <li class="alt"><span><span class="comment"> * 建议使用 assertExists()、assertTrue() 等方法校验参数。</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @author hyd</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">abstract</span><span> </span><span class="keyword">class</span><span> CcimpAction </span><span class="keyword">extends</span><span> Action { </span></span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">static</span><span> Logger log = Logger.getLogger(CcimpAction.</span><span class="keyword">class</span><span>); </span></span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 防止重复提交的标识</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String REQUEST_TOKEN = </span><span class="string">"duplicate_request_token"</span><span>; </span></span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 预定义的 forward</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String SUCCESS = </span><span class="string">"success"</span><span>; </span></span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">boolean</span><span> SHOW_PARAMS = </span><span class="keyword">false</span><span>; </span></span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">static</span><span> ThreadLocal helperContainer = </span><span class="keyword">new</span><span> ThreadLocal(); </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">static</span><span> ThreadLocal errorsContainer = </span><span class="keyword">new</span><span> ThreadLocal(); </span></span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="keyword">public</span><span> ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, </span></span></li> <li class="alt"><span> HttpServletResponse response) <span class="keyword">throws</span><span> Exception { </span></span></li> <li class=""><span> request.setCharacterEncoding(<span class="string">"GBK"</span><span>); </span></span></li> <li class="alt"><span> response.setContentType(<span class="string">"text/html; charset=GBK"</span><span>); </span></span></li> <li class=""><span> response.setHeader(<span class="string">"Pragma"</span><span>, </span><span class="string">"No-cache"</span><span>); </span></span></li> <li class="alt"><span> response.setHeader(<span class="string">"Cache-Control"</span><span>, </span><span class="string">"no-cache"</span><span>); </span></span></li> <li class=""><span> response.setDateHeader(<span class="string">"Expires"</span><span>, </span><span class="number">0</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li class=""><span> String forward = <span class="string">"error"</span><span>; </span></span></li> <li class="alt"><span> PageHelper helper = <span class="keyword">new</span><span> PageHelper(request, response); </span></span></li> <li class=""><span> helperContainer.set(helper); </span></li> <li class="alt"><span> log.debug(<span class="string">"当前 Action:"</span><span> + </span><span class="keyword">this</span><span>.getClass().getName()); </span></span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">// 检查用户是否登录</span><span> </span></span></li> <li class=""><span> <span class="keyword">if</span><span> (!helper.checkAccess()) { </span></span></li> <li class="alt"><span> log.debug(<span class="string">"用户未登录,页面访问被阻止。"</span><span>); </span></span></li> <li class=""><span> <span class="keyword">return</span><span> mapping.findForward(</span><span class="string">"login"</span><span>); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> TokenManager tm = <span class="keyword">new</span><span> TokenManager(request); </span></span></li> <li class=""><span> <span class="keyword">if</span><span> (!tm.isTokenValid()) { </span></span></li> <li class="alt"><span> log.debug(<span class="string">"重复提交被阻止。"</span><span>); </span></span></li> <li class=""><span> setAttribute(<span class="string">"result"</span><span>, </span><span class="string">"不允许重复提交。"</span><span>); </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> mapping.findForward(forward); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> tm.saveToken(); </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">// 在后台输出请求参数</span><span> </span></span></li> <li class=""><span> <span class="keyword">if</span><span> (SHOW_PARAMS) { </span></span></li> <li class="alt"><span> log.debug(<span class="string">"请求参数:\n"</span><span> + getRequestParamsString()); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">// 进行参数校验</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">try</span><span> { </span></span></li> <li class=""><span> validate(form); </span></li> <li class="alt"><span> ArrayList errors = getErrors(); </span></li> <li class=""><span> <span class="keyword">if</span><span> (errors.size() > </span><span class="number">0</span><span>) { </span></span></li> <li class="alt"><span> StringBuffer buffer = <span class="keyword">new</span><span> StringBuffer(); </span></span></li> <li class=""><span> <span class="keyword">for</span><span> (</span><span class="keyword">int</span><span> i = </span><span class="number">0</span><span>; i < errors.size(); i++) { </span></span></li> <li class="alt"><span> String msg = (String) errors.get(i); </span></li> <li class=""><span> buffer.append(msg).append(<span class="string">"\n"</span><span>); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> String result = buffer.toString(); </span></li> <li class="alt"><span> log.error(<span class="string">"参数验证失败:"</span><span> + result); </span></span></li> <li class=""><span> setAttribute(<span class="string">"result"</span><span>, result); </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> mapping.findForward(forward); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> } <span class="keyword">catch</span><span> (Exception e) { </span></span></li> <li class=""><span> setAttribute(<span class="string">"result"</span><span>, e.getMessage()); </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> mapping.findForward(forward); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">// 执行子类实现的预处理</span><span> </span></span></li> <li class="alt"><span> preProcess(); </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">try</span><span> { </span></span></li> <li class=""><span> forward = execute(form); </span></li> <li class="alt"><span> } <span class="keyword">catch</span><span> (Exception e) { </span></span></li> <li class=""><span> reportError(e); </span></li> <li class="alt"><span> <span class="keyword">throw</span><span> e; </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> <span class="keyword">return</span><span> mapping.findForward(forward); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> reportError(Exception e) { </span></span></li> <li class="alt"><span> log.warn(<span class="string">"发送错误报告..."</span><span>); </span></span></li> <li class=""><span> ErrorReporter.report(getRequest().getRequestURI(), e); </span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">protected</span><span> String getRequestParamsString() { </span></span></li> <li class=""><span> NameValuePair[] params = getHelper().getAllParams(); </span></li> <li class="alt"><span> StringBuffer buffer = <span class="keyword">new</span><span> StringBuffer(); </span></span></li> <li class=""><span> <span class="keyword">for</span><span> (</span><span class="keyword">int</span><span> i = </span><span class="number">0</span><span>; i < params.length; i++) { </span></span></li> <li class="alt"><span> NameValuePair param = params[i]; </span></li> <li class=""><span> buffer.append(<span class="string">"setRequestParam(\""</span><span>) </span></span></li> <li class="alt"><span> .append(param.getName()) </span></li> <li class=""><span> .append(<span class="string">"\", \""</span><span>) </span></span></li> <li class="alt"><span> .append(param.getValue()) </span></li> <li class=""><span> .append(<span class="string">"\");\n"</span><span>); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> <span class="keyword">return</span><span> buffer.toString(); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> addError(String message) { </span></span></li> <li class=""><span> ArrayList errors = getErrors(); </span></li> <li class="alt"><span> errors.add(message); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 检查参数是否为空</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param paramName request 中的参数名</span> </span></li> <li class=""><span><span class="comment"> * @param failMessage 如果参数为空,则向用户显示的错误讯息</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @throws ValidationFailException 如果参数为空</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> assertExists(String paramName, String failMessage) </span><span class="keyword">throws</span><span> ValidationFailException { </span></span></li> <li class="alt"><span> assertTrue(paramExists(paramName), failMessage); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 检查参数是否符合条件</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param expression 检查表达式</span> </span></li> <li class=""><span><span class="comment"> * @param failMessage 如果不符合条件,则向用户显示的错误讯息</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @throws ValidationFailException 如果不符合条件</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> assertTrue(</span><span class="keyword">boolean</span><span> expression, String failMessage) </span><span class="keyword">throws</span><span> ValidationFailException { </span></span></li> <li class="alt"><span> <span class="keyword">if</span><span> (!expression) { </span></span></li> <li class=""><span> <span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> ValidationFailException(failMessage); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 检查参数是否符合条件</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param expression 检查表达式</span> </span></li> <li class=""><span><span class="comment"> * @param failMessage 如果不符合条件,则向用户显示的错误讯息</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @throws ValidationFailException 如果不符合条件</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> assertFalse(</span><span class="keyword">boolean</span><span> expression, String failMessage) </span><span class="keyword">throws</span><span> ValidationFailException { </span></span></li> <li class="alt"><span> assertTrue(!expression, failMessage); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 检查参数是否为空</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param value 参数值</span> </span></li> <li class=""><span><span class="comment"> * @param failMessage 如果参数为空,则向用户显示的错误讯息</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @throws ValidationFailException 如果参数为空</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> assertNotEmpty(String value, String failMessage) </span><span class="keyword">throws</span><span> ValidationFailException { </span></span></li> <li class="alt"><span> assertTrue(!StringUtil.isEmptyString(value), failMessage); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 检查数组是否为空</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param values 数组</span> </span></li> <li class=""><span><span class="comment"> * @param failMessage 如果数组为空,则向用户显示的错误讯息</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @throws ValidationFailException 如果数组为空</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> assertNotEmpty(String[] values, String failMessage) </span><span class="keyword">throws</span><span> ValidationFailException { </span></span></li> <li class="alt"><span> assertTrue(values != <span class="keyword">null</span><span> && values.length > </span><span class="number">0</span><span>, failMessage); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="keyword">protected</span><span> ArrayList getErrors() { </span></span></li> <li class="alt"><span> ArrayList list = (ArrayList) getRequest().getAttribute(<span class="string">"CCIMP_REQUEST_ERRORS"</span><span>); </span></span></li> <li class=""><span> <span class="keyword">if</span><span> (list == </span><span class="keyword">null</span><span>) { </span></span></li> <li class="alt"><span> list = <span class="keyword">new</span><span> ArrayList(); </span></span></li> <li class=""><span> getRequest().setAttribute(<span class="string">"CCIMP_REQUEST_ERRORS"</span><span>, list); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> <span class="keyword">return</span><span> list; </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 获得一个 AccessControl 实例</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @return 一个 AccessControl 实例</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> AccessControl getAccessControl() { </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> getHelper().getAccessControl(); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 获得请求对象</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @return 请求对象</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> HttpServletRequest getRequest() { </span></span></li> <li class=""><span> <span class="keyword">return</span><span> getHelper().getRequest(); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 获得回应对象</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @return 回应对象</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> HttpServletResponse getResponse() { </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> getHelper().getResponse(); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 将对象放入 request</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param name 名称</span> </span></li> <li class=""><span><span class="comment"> * @param value 对象</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setAttribute(String name, Object value) { </span></span></li> <li class="alt"><span> getRequest().setAttribute(name, value); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 将数值放入 request</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param name 名称</span> </span></li> <li class=""><span><span class="comment"> * @param num 数值</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setAttribute(String name, </span><span class="keyword">int</span><span> num) { </span></span></li> <li class="alt"><span> setAttribute(name, <span class="keyword">new</span><span> Integer(num)); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 将 boolean 值放入 request</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param name 名称</span> </span></li> <li class=""><span><span class="comment"> * @param b boolean 值</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setAttribute(String name, </span><span class="keyword">boolean</span><span> b) { </span></span></li> <li class="alt"><span> setAttribute(name, Boolean.valueOf(b)); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 获得 request 中的值</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param name 名称</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @return request 中的值</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> Object getAttribute(String name) { </span></span></li> <li class=""><span> <span class="keyword">return</span><span> getRequest().getAttribute(name); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">// 获得请求参数</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> String getParam(String name) { </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> getHelper().getParam(name); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">// 获得请求参数</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> String getParamFromRequestAndSession(String name) { </span></span></li> <li class=""><span> String value = getHelper().getParam(name); </span></li> <li class="alt"><span> <span class="keyword">if</span><span> (StringUtil.isEmptyString(value)) { </span></span></li> <li class=""><span> value = StringUtil.replaceNull(getSessionAttributeOnce(name)); </span></li> <li class="alt"><span> } </span></li> <li class=""><span> <span class="keyword">return</span><span> value; </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">// 获得请求参数</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> String getParam(String name, Object defaultValue) { </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> getHelper().getParam(name, StringUtil.replaceNull(defaultValue)); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">// 获得请求参数</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> String[] getParamValues(String name) { </span></span></li> <li class=""><span> <span class="keyword">return</span><span> getHelper().getParamValues(name); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">protected</span><span> String getSessionString(String name) { </span></span></li> <li class=""><span> <span class="keyword">return</span><span> getHelper().getSessionString(name); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="keyword">protected</span><span> String[] getParamNames() { </span></span></li> <li class=""><span> ArrayList nameArr = <span class="keyword">new</span><span> ArrayList(); </span></span></li> <li class="alt"><span> Enumeration names = getRequest().getParameterNames(); </span></li> <li class=""><span> <span class="keyword">while</span><span> (names.hasMoreElements()) { </span></span></li> <li class="alt"><span> nameArr.add(names.nextElement()); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> <span class="keyword">return</span><span> (String[]) nameArr.toArray(</span><span class="keyword">new</span><span> String[nameArr.size()]); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 设置处理结果信息</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param message 处理结果信息(如“记录添加成功!”等,用于显示在页面上)</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setActionMessage(String message) { </span></span></li> <li class=""><span> setSessionAttribute(<span class="string">"ccimp_message"</span><span>, </span><span class="keyword">new</span><span> Message(Message.INFO, message)); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 设置处理结果信息</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @param message 处理结果信息(如“记录添加成功!”等,用于显示在页面上)</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setActionMessage(Message message) { </span></span></li> <li class="alt"><span> setSessionAttribute(<span class="string">"ccimp_message"</span><span>, message); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 设置处理结果错误信息</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param message 处理结果错误信息(如“记录添加失败:ID 重复”等,用于显示在页面上)</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setActionErrorMessage(String message) { </span></span></li> <li class=""><span> setSessionAttribute(<span class="string">"ccimp_message"</span><span>, </span><span class="keyword">new</span><span> Message(Message.ERROR, message)); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 设置是否进行访问权限的检查。单元测试中应将其设置为false。</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @param validation 是否进行访问权限的检查</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setAccessValidation(</span><span class="keyword">boolean</span><span> validation) { </span></span></li> <li class="alt"><span> getHelper().setAccessValidation(validation); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setHelper(PageHelper h) { </span></span></li> <li class="alt"><span> helperContainer.set(h); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="keyword">protected</span><span> PageHelper getHelper() { </span></span></li> <li class="alt"><span> <span class="keyword">return</span><span> (PageHelper) helperContainer.get(); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 执行请求</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param form 表单</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @return 要跳转的 forward 名称</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @throws Exception 如果执行失败</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">abstract</span><span> String execute(ActionForm form) </span><span class="keyword">throws</span><span> Exception; </span></span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 执行请求之前的预处理</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> preProcess() { </span></span></li> <li class="alt"><span> <span class="comment">// 给子类使用</span><span> </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 校验参数</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param form 表单</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @throws Exception 如果执行校验失败</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> validate(ActionForm form) </span><span class="keyword">throws</span><span> Exception { </span></span></li> <li class=""><span> <span class="comment">// 给子类使用</span><span> </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 写业务日志</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @param action 动作</span> </span></li> <li class="alt"><span><span class="comment"> * @param object 对象</span> </span></li> <li class=""><span><span class="comment"> * @param message 日志信息</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> log(String action, String object, String message) { </span></span></li> <li class="alt"><span> getHelper().writeLog(action, object, message); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 写业务日志</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param action 动作</span> </span></li> <li class=""><span><span class="comment"> * @param message 日志信息</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> log(String action, String message) { </span></span></li> <li class="alt"><span> log(action, <span class="string">""</span><span>, message); </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 设置 session 属性</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param name 属性名称</span> </span></li> <li class=""><span><span class="comment"> * @param value 属性值</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> setSessionAttribute(String name, Object value) { </span></span></li> <li class="alt"><span> getRequest().getSession().setAttribute(name, value); </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 获得 session 属性</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @param name 属性名</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @return 属性值</span> </span></li> <li class=""><span><span class="comment"> */</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">protected</span><span> Object getSessionAttribute(String name) { </span></span></li> <li class=""><span> <span class="keyword">return</span><span> getRequest().getSession().getAttribute(name); </span></span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> <span class="comment">/**</span> </span></li> <li class=""><span><span class="comment"> * 获得 session 属性,只取一次</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @param name 属性名</span> </span></li> <li class="alt"><span><span class="comment"> *</span> </span></li> <li class=""><span><span class="comment"> * @return 属性值</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li class=""><span> <span class="keyword">protected</span><span> Object getSessionAttributeOnce(String name) { </span></span></li> <li class="alt"><span> Object value = getRequest().getSession().getAttribute(name); </span></li> <li class=""><span> getRequest().getSession().removeAttribute(name); </span></li> <li class="alt"><span> <span class="keyword">return</span><span> value; </span></span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> <span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * 检查当前用户是否拥有指定操作的权限</span> </span></li> <li class=""><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> |
|
返回顶楼 | |