ITeye博客

SSH集成后如何在Action中获取web.xml中<context-param>的参数值

orange5458 · 2008-12-18 11:47 · SSH · Markdown

我想在Action的分页实现方法中获取web.xml中<context-param>配置的pageSize(页面的最大数据条数),

但是运行后发现在Action中连ActionServlet都获取不了。

 

希望有人能帮我解决此问题,谢谢。。。。。

 

下面是分页流程的主要类和配置

 

web.xml:

 

 <context-param>
  <param-name>pageSize</param-name>
  <param-value>5</param-value>
 </context-param>

 

 

ProductManagerAction:

 

public class ProductManagerAction extends MappingDispatchAction {
 private Logger logger = Logger.getLogger(ProductManagerAction.class);
 private ProductService productService;

 public void setProductService(ProductService productService) {
  this.productService = productService;
 }

 public ActionForward listCategory(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  int currentPage = 1;
  System.out.println("--------------------------------------");
  System.out.println(this.getServlet());
  System.out.println(this.getServlet().getServletContext());
  System.out.println(this.getServlet().getServletContext().getInitParameterNames());
  int pageSize = Integer.parseInt(this.getServlet().getServletContext().getInitParameter("pageSize"));
  Pager pager = new Pager(currentPage, pageSize, productService.getCategoryAmount());
  String currPage = request.getParameter("currentPage");
  if((currPage != null)){
   try {
    currentPage = Integer.parseInt(currPage);
    if(currentPage<1 || currentPage>pager.getTotalPage())
     currentPage = 1;
   } catch (NumberFormatException e) {
    logger.fatal("----------管理员改变URL中currentPage的值,使之不可转化为int类型数据--------------");
   }
  }
  pager.setCurrentPage(currentPage);
  request.setAttribute("pager", pager);
  List<Category> categories = productService.listCategories(currentPage, pageSize);
  request.setAttribute("categories", categories);
  return mapping.findForward("success");
 }

}

 

 

struts-config.xml:

 

<action path="/manager/product/categoryManager"
    type="org.springframework.web.struts.DelegatingActionProxy"
    parameter="listCategory">
   <forward name="success" path="/manager/product/listCategory.jsp"></forward>
  </action>

applicationContext-web.xml:

 

<bean id="productManager" class="com.siyuan.estore.struts.action.ProductManagerAction" abstract="true">
  <property name="productService" ref="productService"></property>
 </bean>
 <bean name="/manager/product/categoryManager" parent="productManager"></bean>

 

运行时console输出:

 --------------------------------------
null

评论(1)

  1. #1 楼 · xxMix · 2013-07-19 13:41

    web.xml中的context-param参数: contextConfigLocation classpath*:applicationContext.xml

    在Action中获取context-param的方法: public String index(){
    System.out.println(ServletActionContext.getServletContext().getInitParameter("contextConfigLocation")); return SUCCESS; }

    ServletActionContext.getServletContext()源码: public static ServletContext getServletContext() { return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT); } 找到常量SERVLET_CONTEXT: /**

    • Constant for the {@link javax.servlet.ServletContext servlet context} object. */ public static final String SERVLET_CONTEXT = "com.opensymphony.xwork2.dispatcher.ServletContext";

    发现ServletActionContext.getServletContext()其实质就是javax.servlet.ServletContext servlet context.

    由此我们可以知道获取request 和 response对象的方法: HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);

    HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);

评论功能维护中,当前仅展示历史评论。