/src/com/meikai/core/action/BasicActionSupport.java

https://github.com/yookien/yookien · Java · 206 lines · 126 code · 28 blank · 52 comment · 3 complexity · 203a1ce3f09b01cd2c570b91961bbd55 MD5 · raw file

  1. package com.meikai.core.action;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.Map;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import com.meikai.core.util.Pagination;
  9. import org.apache.log4j.Logger;
  10. import org.apache.struts2.ServletActionContext;
  11. import com.opensymphony.xwork2.ActionContext;
  12. import com.opensymphony.xwork2.ActionSupport;
  13. /**
  14. * Action公共处理类
  15. *
  16. * @author ntyookien
  17. *
  18. * @version $Revision: 1.9 $ $Date: 2008/04/02 05:48:40 $
  19. */
  20. public abstract class BasicActionSupport extends ActionSupport {
  21. public static Logger logger = Logger.getLogger(sun.reflect.Reflection.getCallerClass(1));
  22. public static String CREATE = "create";
  23. public static String EDIT = "edit";
  24. public static String UPDATE = "update";
  25. public static String DELETE = "delete";
  26. public static String SELECT = "select";
  27. public static String INFO = "info";
  28. public static String WARN = "warn";
  29. public static String LIST = "list";
  30. public static String VIEW = "view";
  31. // 编号
  32. protected int id;
  33. public void setId(int id){
  34. this.id = id;
  35. }
  36. // 动作
  37. protected String action;
  38. public void setAction(String action){
  39. this.action = action;
  40. }
  41. /**
  42. * 取得request中parameter的值
  43. * @param parameter 参数
  44. * @return string 参数的值
  45. */
  46. protected String getParameter(String parameter) {
  47. Object value = getParameters().get(parameter);
  48. if(value!=null){
  49. return ((String[])value)[0];
  50. }
  51. return "";
  52. }
  53. protected HttpServletRequest getRequest() {
  54. return ServletActionContext.getRequest();
  55. }
  56. protected HttpServletResponse getResponse() {
  57. return ServletActionContext.getResponse();
  58. }
  59. /**
  60. * 取得request中parameter的值
  61. * @param parameter 参数
  62. * @return string 参数的值
  63. */
  64. protected String[] getParameterValues(String parameter) {
  65. String[] values = this.getRequest().getParameterValues(parameter);
  66. if(values!=null){
  67. return values;
  68. }
  69. return new String[]{};
  70. }
  71. /**
  72. * 取得整型参数的值
  73. * @param parameter 参数
  74. * @return int 当前类别
  75. */
  76. protected int getIntParameter(String parameter){
  77. Object value = getParameters().get(parameter);
  78. if(value!=null){
  79. try{
  80. return Integer.parseInt(((String[])value)[0]);
  81. }
  82. catch(Exception e){
  83. return 0;
  84. }
  85. }
  86. return 0;
  87. }
  88. /**
  89. * 取得分页的起始页
  90. * @return
  91. */
  92. protected int getPaginationStart(){
  93. return getIntParameter("start");
  94. }
  95. /**
  96. * 取得IP
  97. * @return
  98. */
  99. protected String getRemoteAddr() {
  100. return getRequest().getRemoteAddr();
  101. }
  102. /**
  103. *
  104. * @return
  105. */
  106. protected Map getParameters(){
  107. ActionContext actionContext = ActionContext.getContext();
  108. Map parameters = (Map)actionContext.getParameters();
  109. return parameters;
  110. }
  111. /**
  112. * 取得web发布路径
  113. * @return 容器路径
  114. */
  115. public String getContextPath(){
  116. return ServletActionContext.getServletContext().getRealPath("/");
  117. }
  118. /**
  119. * 运用j2ee api将数据发给客户端
  120. *
  121. * @param content
  122. * @throws IOException
  123. */
  124. protected void sendClient(String content) throws IOException
  125. {
  126. PrintWriter out = getResponseWriter();
  127. out.print(content);
  128. out.flush();
  129. out.close();
  130. }
  131. protected PrintWriter getResponseWriter() throws IOException
  132. {
  133. HttpServletResponse response = ServletActionContext.getResponse();
  134. response.setContentType("text/html;charset=utf-8");
  135. return response.getWriter();
  136. }
  137. /**
  138. * 返回值的定义
  139. */
  140. public String actionMessage;
  141. public String getActionMessage() {
  142. return actionMessage;
  143. }
  144. public void setActionMessage(String actionMessage) {
  145. this.actionMessage = actionMessage;
  146. }
  147. public String errorMessage;
  148. public String getErrorMessage() {
  149. return errorMessage;
  150. }
  151. public void setErrorMessage(String errorMessage) {
  152. this.errorMessage = errorMessage;
  153. }
  154. public String warnMessage;
  155. public String getWarnMessage() {
  156. return warnMessage;
  157. }
  158. public void setWarnMessage(String warnMessage) {
  159. this.warnMessage = warnMessage;
  160. }
  161. public String infoMessage;
  162. public String getInfoMessage() {
  163. return infoMessage;
  164. }
  165. public void setInfoMessage(String infoMessage) {
  166. this.infoMessage = infoMessage;
  167. }
  168. /**
  169. * 分页Bean的设置
  170. */
  171. private Pagination pagination = null;
  172. public Pagination getPagination() {
  173. return pagination;
  174. }
  175. public void setPagination(Pagination pagination) {
  176. this.pagination = pagination;
  177. }
  178. }