- 浏览: 70696 次
- 性别:
- 来自: 山水之乡
最新评论
-
zongjianhui:
大哥,你写的是什么东西啊,如果不需要交互的就用TagSuppo ...
TagSupport与BodyTagSupport的区别 -
baiyuxiong:
果不需要交互的就用TagSupport,否则如果不需要交互就用 ...
TagSupport与BodyTagSupport的区别 -
jiaoxujin:
<bean id="baseControl ...
Spring 中继承的实现 -
xiao1955323:
...
Spring 中继承的实现 -
xiaxiaojuan:
这个错之前从来没见过,先试试你得办法吧!谢啦
解决了struts2.0 严重: Error filterStart 配置问题
如果为第个Form都建一个Action那是相当麻烦的,
如果像下面这样构建啦一个通用的Action会带来什么好处?
而像平常的做法是几乎是一个Form 和 Action的这种做法又有什么好处?
请大家谈谈自己的看法和经验?
下面是源码和相关的解释
请大家先看一下Struts-config.xml 中的一部分内容
xml 代码
- <!--第一种 parameter为空并且name为空时的情况 即 form=null 时 直接转发-->
- <action path="/dgroom/index" type="com.dgroom.struts.BeanAction"
- validate="false" >
- <forward name="success" path="/dgroom/index.xsp"/>
- </action>
- <!--第二种情况 parameter="*"时 意为想调用一个Bean但又不想调用他里面的任何方法时 -->
- <action path="/dgroom/loginForm" type="com.dgroom.struts.BeanAction"
- name="accountBean" scope="session" parameter="*"
- validate="false">
- <forward name="success" path="/account/loginForm.xsp"/>
- </action>
- <!--第三种 当 parameter=" NAME " 有值时 调用 accountBean中的displayAll()方法-->
- <action path="/dgroom/displayAllAccount" type="com.dgroom.struts.BeanAction"
- name="accountBean" scope="session" parameter="displayAll"
- validate="false">
- <forward name="success" path="/dgroom/displayAll.xsp"/>
- </action>
- <!--第四种 就直接调用地址栏中的最后的那个 login()方法 -->
- <action path="/dgroom/login" type="com.dgroom.struts.BeanAction"
- name="accountBean" scope="session"
- validate="false">
- <forward name="success" path="/dgroom/index.xsp"/>
- </action>
而在其它的Bean中的方法也如同下面的这个方法,都是返回一个字符串
而在AccountBean.java中的方法如下
java 代码
- public class AccountBean
- {
- public String login(){
- if(){
- //成功
- return "success";
- }
- //失败
- return "failure";
- }
- }
下面的是通用的 BeanAction.java 类
java 代码
- package com.dgroom.struts;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.lang.reflect.Method;
- import com.dgroom.util.*;
- public class BeanAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String forward = "success";
- try {
- //这里是对ActionContext.save(request,reponse);
- //对映 Struts-config.xml 中的 第一种 :当请求中的 Form 为空时直接转 发
- if (form != null) {
- Method method = null;
- String methodName = mapping.getParameter();
- //第三种:当参数中存在着某个值时并且不等于 * 时就直接调用那个参数中的方法
- if (methodName != null && !"*".equals(methodName)) {
- try {
- method = form.getClass().getMethod(methodName, null);
- forward = (String) method.invoke(form, null);
- } catch (Exception e) {
- // 这里是一个自定义的异常
- }
- }
- //第四种 当参数为空时 直接调用地址栏最后的方法
- if (method == null && !"*".equals(methodName)) {
- methodName = mapping.getPath();
- if (methodName.length() > 1) {
- int slash = methodName.lastIndexOf("/") + 1;
- methodName = methodName.substring(slash);
- if (methodName.length() > 0) {
- try {
- method = form.getClass().getMethod(methodName, null);
- forward = (String) method.invoke(form, null);
- } catch (Exception e) {
- // 这里是一个自定义的异常
- }
- }
- }
- }
- }
- } catch (Exception e) {
- // 这里是一个自定义的异常
- throw e;
- }
- return mapping.findForward(forward);
- }
- }
评论
13 楼
ice123456
2007-09-06
用法:struts.config.xml,定义一个parameter="action"
你可以在你自己写的action里面继承DispatchAction这个类,然后直接写方法,跟继承action是一样的,不同的是方法名可以不一样
你在前台调用的时候是admin.*?action=test
这里就相当于调用你在action写的test方法
你可以在你自己写的action里面继承DispatchAction这个类,然后直接写方法,跟继承action是一样的,不同的是方法名可以不一样
你在前台调用的时候是admin.*?action=test
这里就相当于调用你在action写的test方法
12 楼
ice123456
2007-09-06
我认为你还不如去看下DispatchAction
这个类应该可以达到你想的哪种效果
这个类应该可以达到你想的哪种效果
11 楼
leeon
2007-08-24
xiaoxiaodi5834 写道
个人认为,你是想用一个通用的Action来替换每个Action,然后再通过这个Action来调用业务类。应该是想再做一层封装。
刚好有用过一个这种框架的东西,即,将Action用一个EJB来实现,将所有的通用的东西放到这里面,来做事务控制。
这个东西理论上是不错的,可是实际上却还是要再多一些配置文件来定义真正业务类的位置的东东。
个人认为,这样子做的效果不是太好。不如直接用Struts2.0
刚好有用过一个这种框架的东西,即,将Action用一个EJB来实现,将所有的通用的东西放到这里面,来做事务控制。
这个东西理论上是不错的,可是实际上却还是要再多一些配置文件来定义真正业务类的位置的东东。
个人认为,这样子做的效果不是太好。不如直接用Struts2.0
这种框架我们以前做过,用一个通用的Action类,然后通过很多配置文件来描述
具体的service类在什么地方,需要传入什么参数到service中,然后把这些参数
一股脑扔到一个map中在传入service
后来被用过的人一阵狂骂,现在还是回到写action类来,
其实用struts2.0已经可以帮我减少很多麻烦事了,大家用下了都觉得效果不错
10 楼
xiaoxiaodi5834
2007-08-23
个人认为,你是想用一个通用的Action来替换每个Action,然后再通过这个Action来调用业务类。应该是想再做一层封装。
刚好有用过一个这种框架的东西,即,将Action用一个EJB来实现,将所有的通用的东西放到这里面,来做事务控制。
这个东西理论上是不错的,可是实际上却还是要再多一些配置文件来定义真正业务类的位置的东东。
个人认为,这样子做的效果不是太好。不如直接用Struts2.0
刚好有用过一个这种框架的东西,即,将Action用一个EJB来实现,将所有的通用的东西放到这里面,来做事务控制。
这个东西理论上是不错的,可是实际上却还是要再多一些配置文件来定义真正业务类的位置的东东。
个人认为,这样子做的效果不是太好。不如直接用Struts2.0
9 楼
sam1982
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">
<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">
8 楼
yiding_he
2007-07-17
<div class='code_title'>要避免写 Action 是不可能的,最好是写一个项目中通用的 Action,提供大量已实现的方法。其他 Action 都从此继承,这样要好写得多。<br/>
<br/>
(不要引用这个回复啊,会死人的)<br/>
<br/>
java 代码</div>
<div class='dp-highlighter'>
<div class='bar'> </div>
<ol class='dp-j' start='1'>
<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'> * @param</span></span></li></ol></div>
<br/>
(不要引用这个回复啊,会死人的)<br/>
<br/>
java 代码</div>
<div class='dp-highlighter'>
<div class='bar'> </div>
<ol class='dp-j' start='1'>
<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'> * @param</span></span></li></ol></div>
7 楼
gs.liutong
2007-07-17
这样做是不是让MVC结构更模糊了?Action应该是显示层的组间,返回值forward定义了要将结过呈现出来的页面。
由后台Bean来决定forward的值,增加了显示层和后台模型层的耦合。这样会影响到系统组间的可重用性。对项目的成本和效率都会造成一定影响吧。
由后台Bean来决定forward的值,增加了显示层和后台模型层的耦合。这样会影响到系统组间的可重用性。对项目的成本和效率都会造成一定影响吧。
6 楼
dongguoh
2007-07-17
ActionContext不是Struts当中的类,
这是一个自定义的类,这个类用来保存一些错误的信息
初始化request和response
你所说的在Bean中的业务方法是增加啦不少的代码~
至于如何调用action的一些方法,这里没有仔细想过
你说到的这几个问题很好,点醒啦我
这是一个自定义的类,这个类用来保存一些错误的信息
初始化request和response
你所说的在Bean中的业务方法是增加啦不少的代码~
至于如何调用action的一些方法,这里没有仔细想过
你说到的这几个问题很好,点醒啦我
5 楼
flxi78
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的代码会比较凌乱。
另外,我看楼主的思路是通过自己写一个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的代码会比较凌乱。
4 楼
dongguoh
2007-07-16
flxi78 写道
要想通用太难了。。就我个人在开发中碰到的问题,动态方法,include,download等action的功能要整合在一起就很麻烦。至于在dispatchAction下,由于form的invalidate方法出错后,返回的input页面如何解决
其实这就是一个通用的Action
我是在Jpetstore上分析源码时发现的,觉得很好就发上来与大家共享
你上面所说的返回input页面如何解决?
因上面有一段代码我把它注释啦 ActionContext.save(request.response);
这是把所传过来的 request和response进行保存
而当页面出错的时候是通过ActionContext来保存的,并且是在login()方法中进行保存
由于关联的代码太多,我就只发这个最主要的上来
3 楼
flxi78
2007-07-16
要想通用太难了。。就我个人在开发中碰到的问题,动态方法,include,download等action的功能要整合在一起就很麻烦。至于在dispatchAction下,由于form的invalidate方法出错后,返回的input页面如何解决
2 楼
giscat
2007-07-16
Map model = JspUtil.getRequestModel(request);
//把request参数塞到map里
ORM.insert(tableName,model);
//ORM.update(tableName,model);
//orm 是diy的ORM小工具
1 楼
stone
2007-07-16
你还是看看dispathAction和dynamicFormBean吧
相关推荐
在Struts框架中,"一个Action对应多个类实例"的概念是设计模式的一种体现,它涉及到MVC(模型-视图-控制器)架构的实现。Struts是一个基于Java Servlet和JSP的开源框架,用于构建企业级Web应用程序。在这个框架中,...
Maven是一个流行的项目管理和综合工具,它通过提供一套标准化的构建过程,简化了Java项目的构建、依赖管理和部署。在Spring开发中,Maven扮演着至关重要的角色,它帮助开发者管理各种库依赖,使得Spring应用程序的...
标题 "YII 通用CURD外部Action 适合单表" 提到的是在Yii框架中创建一个通用的CURD(创建、读取、更新、删除)操作的外部Action,这种Action适用于处理单个数据表的常见数据库操作。在Yii框架中,Action是Controller...
总结一下,构建通用领域语言的关键在于识别和封装业务中的原子操作,并提供一个框架让业务层能够自由地组合这些操作。在C++中,我们可以利用面向对象和设计模式来实现这一目标。在给定的示例中,我们创建了一个简单...
在本文中,我们将探讨如何自己构建一个基于Struts2的简单框架,了解其基本思路和主要步骤。 首先,Struts2是Apache Jakarta项目中的一个组件,它是Struts1和WebWork框架的合并产物。Struts2的核心是基于拦截器的...
SSH是Java Web开发中的一个流行组合,由Spring、Struts和Hibernate三个开源框架组成。这三个框架协同工作,为开发者提供了一种高效、灵活且可扩展的解决方案,用于构建企业级应用。下面我们将深入探讨SSH框架的通用...
通用web层action,完美解决shiro注解权限控制,读取子类泛型
标题中的“一个通用积分的网站,开发了一半”意味着这个项目是一个在线积分管理系统,它可能被设计用来跟踪和管理用户在各种活动或购买中的积分积累。这个系统在开发过程中已经完成了部分功能,但还未全部完成,暗示...
在Java Web开发中,Struts框架是一个非常流行的MVC(模型-视图-控制器)架构,用于构建可维护和可扩展的Web应用程序。在Struts框架中,Action类是核心组件,负责处理用户请求并调用业务逻辑。为了确保Action类的正确...
【ActionServlet demo】是一个基于Java和Servlet技术的示例项目,它主要展示了如何在Web应用程序中使用Servlet来处理用户请求并驱动业务逻辑。这个项目可能与Struts 1.3框架相结合,Struts是一个广泛使用的MVC(模型...
每个命名空间可以看作是应用程序的一个独立部分,有自己的Action和视图。 综上所述,这些文件详细阐述了Struts2中Action的使用和相关配置,包括Action的创建、执行方法的指定、路径配置、通配符映射、参数处理、...
**Struts2框架** 是一个基于MVC设计模式的Web应用框架,它允许开发者通过Action类来处理用户请求,并使用配置文件定义Action与结果视图之间的映射。Struts2与Spring的集成使得我们可以轻松地利用Spring的DI特性来...
使用Ruby和GitHub API构建Action的快速入门配方
《基于Struts2、Spring、Hibernate构建一个网上投票系统》 ...对于初学者而言,这是一个很好的实践案例,有助于理解Web应用的开发流程和框架之间的协作。而对于有经验的开发者,它也能提供一些新的思考角度和优化方案。
在实际应用中,分页通常会涉及到一个通用的Page类,该类包含当前页、总页数、每页条数以及数据列表等属性。在Service层,我们可以创建一个通用的分页方法,接受页码和每页大小作为参数,然后调用MyBatis的Mapper进行...
如果这是测试代码,那么可能包含了一个模拟用户行为的测试类,用于测试Struts Action的功能。如果是配置文件,可能是Struts的配置文件(struts-config.xml或struts2的struts.xml),包含了Action的定义及其与视图和...
5. 远程操作(Operation):封装了一个具体的请求,包括请求类型、URL、参数和回调函数。 在提供的压缩包文件中,可能包含了与视频教程同步的示例代码,如控制器(Controller)、视图(View)、模型(Model)、存储...
4. `@IncludeResult`: 如果Action需要引用一个通用的结果页面,可以使用`@IncludeResult`注解。这样可以避免在每个Action中重复定义相同的结果。 5. `@ParentPackage`: 用于指定Action继承自哪个Struts 2配置包,...