当前位置: 代码迷 >> 综合 >> 如何使用xml配置多个似你想要的${pageContext.request.contextPath}
  详细解决方案

如何使用xml配置多个似你想要的${pageContext.request.contextPath}

热度:58   发布时间:2023-11-13 16:32:40.0

我相信我们在项目中肯定遇到过这个${pageContext.request.contextPath},因为相对路径实在太麻烦了,而我当前项目是分布式的,有很多个模块,多个类似于这种路径,所有配置在xml里面方便与后期的修改,话不多说直接贴代码


实体类 OrderInfo

package com.pk.entity;

import org.springframework.stereotype.Component;

@Component
public class OrderInfo {

   
   private String staticPath;//静态地址
   private String orderPath;//订单order地址
   private String loginPath;//PC登录地址

  public String getOrderPath() {return orderPath;
   }public void setOrderPath(String orderPath) {this.orderPath = orderPath;
   }public String getStaticPath() {return staticPath;
   }public void setStaticPath(String staticPath) {this.staticPath = staticPath;
   }public String getLoginPath() {return loginPath;
   }public void setLoginPath(String loginPath) {this.loginPath = loginPath;
   }}

order-local.xml   XML文件 

<?xml version="1.0" encoding="UTF-8"?>
<Order>
       
      <!-- 静态地址首页 -->
      <staticPath>http://www.pk.cn/static/</staticPath>
      <orderPath>http://www.pk.cn/trade/</orderPath>
      <loginPath>http://www.pk.cn/trade/order/login.jhtml</loginPath>

<!--    线上地址 -->
</Order>

Order_AutoConfig  (@postconstruct 注解执行servlet的构造函数的后面  init的前面 详情百度 )

package com.pk.utils;

import java.net.URL;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.pk.entity.OrderInfo;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**  * 系统初始自动获取配置信息  *  *  */ @Component
public class Order_AutoConfig {/** logger */  //private static final Logger logger = LoggerFactory.getLogger(Order_AutoConfig.class);
   public static final String CONFIG_FILE_NAME = "order-local.xml"; // 系统配置文件名称

   @Resource
   OrderInfo order;

   /**  * 系统配置信息读取  *  *  */  @PostConstruct
   public void autoInIt() {Document document = null;
      try {URL configFilePath = Thread.currentThread().getContextClassLoader().getResource(CONFIG_FILE_NAME);
         SAXReader saxReader = new SAXReader();
         document = saxReader.read(configFilePath);
         Element root = document.getRootElement();
         
         String staticPath =  root.element("staticPath").getTextTrim();
         String orderPath =  root.element("orderPath").getTextTrim();
         String loginPath =  root.element("loginPath").getTextTrim();

         order.setOrderPath(orderPath);
         order.setStaticPath(staticPath);
         order.setLoginPath(loginPath);
      } catch (Exception e) {
  
         e.printStackTrace();
      }}}

InitListener 实现 ServletContextAware 就可以拿到servletContext  把数据放进去你就可以在jsp页面使用了


package com.pk.listener;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import com.pk.entity.OrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;


/**  * Listener - 初始化  */ @Component("initListener")
public class InitListener implements ServletContextAware {/** logger */  private static final Logger logger = LoggerFactory.getLogger(InitListener.class);

   /** servletContext */  private ServletContext servletContext;

   @Resource
   OrderInfo orderInfo;

   public void setServletContext(ServletContext servletContext) {this.servletContext = servletContext;
      onApplicationInIt();
   }public void onApplicationInIt() {try {if (servletContext != null) {servletContext.setAttribute("OrderInfo", orderInfo);
         }} catch (Exception e) {logger.error("加载模块的系统变量失败{}",servletContext.getAttribute("OrderInfo"));
         e.getStackTrace();
      }}}


  相关解决方案