properties 파일의 구성을 전체 웹 응용 프로그램의 글로벌 변수로 설정합니다.

4054 단어
네 가지 역할 영역:
웹 응용 프로그램의 변수는 서로 다른 jsp 대상에 저장되어 서로 다른 역할 영역이 있을 수 있습니다. 네 가지 서로 다른 역할 영역은 페이지 Context 1. 페이지 Context: 페이지 필드는 현재 페이지만 유효하며 페이지를 떠난 후 방향을 바꾸든 방향을 바꾸든 (즉redirect든forward든) 페이지 Context의 속성 값은 모두 효력을 상실한다.
2. Request: 요청 필드는 한 번의 요청에 유효합니다. forward로 방향을 바꾸면 다음 요청은 지난번 Request의 속성 값을 보존할 수 있습니다. Redirect가 다른 페이지로 방향을 바꾸면 지난번 Request의 속성 값이 효력을 상실합니다.
3.session: 세션 필드는 한 번의 세션 과정(브라우저에서 이 과정을 열고 브라우저에서 닫는 과정)에서session 대상의 속성 값이 모두 유효하며 이번 세션 과정에서session의 값은 모든 페이지에서 얻을 수 있다.
4. 응용 프로그램: 응용 필드, 응용 프로그램이 닫히지 않으면 이 대상의 속성 값이 유효하고 모든 세션에 공유됩니다.
 
Servlet Context Listener 감청기를 이용하여 응용 프로그램이 불러오면properties의 값을 응용 프로그램에 저장합니다
현재 모든 jsp에서 EL 표현식을 통해properties의 속성을 읽을 수 있고 모든 세션을 대상으로 하기 때문에 응용 프로그램 역할 영역을 이용하여
그럼 언제properties의 속성을 응용 프로그램에 저장합니까?properties의 속성 값을 전역 변수로 하여 모든 EL을 쉽게 얻을 수 있기 때문에 웹 응용 프로그램이 불러올 때 값을 응용 프로그램에 저장합니다.
서브렛ContextListener를 활용하려면 다음과 같이 하십시오.
서브렛ContextListener는 서브렛 API의 인터페이스로, 서브렛Context 객체의 라이프 사이클을 수신할 수 있으며, 실제로는 웹 응용프로그램의 라이프 사이클을 수신합니다.
서브렛 컨테이너가 웹 응용 프로그램을 시작하거나 종료하면 서브렛 컨텍스트 이벤트가 트리거되고 이 이벤트는 서브렛 컨텍스트 리스트가 처리합니다.
 
구체적인 단계는 다음과 같습니다.
1. 새 클래스propertyListenter가 ServletContextListener 인터페이스를 실현하는 contextInitialized 방법;
2. properties 프로필을 읽고 Map에 저장하기;
3. 서브렛Context 객체를 사용하여 맵을 응용 프로그램 역할 영역에 저장합니다.
/**
 *       
 * @author meikai
 * @version 2017 10 23    2:15:19
 */
public class PropertyListenter implements ServletContextListener {

    /* (non-Javadoc)
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     */
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        
        
        /**
         *   properties  
         * 
         */
        final Logger logger = (Logger) LoggerFactory.getLogger(PropertyListenter.class);
        
        Properties properties = new Properties(); 
        
        InputStream in = null;
        try {
            //          properties   
            in = PropertiesUtil.class.getClassLoader().getResourceAsStream("kenhome-common.properties");         
            properties.load(in);
            
        } catch (FileNotFoundException e) {
            logger.error("   properties  ");
        } catch (IOException e) {
            logger.error("  IOException  ");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("properties         ");
            }
        }
                     
        
        /**
         *  properties     map
         */
        Map<String, String> pros = new HashMap<String,String>((Map)properties);
        
        /**
         *  Map  ServletContext         
         */
        ServletContext sct=sce.getServletContext();  
        
        sct.setAttribute("pros", pros);

    }
    

}

4、웹에서.위의 모니터 PropertyListenter를 xml에 구성하려면:

    
      class>com.meikai.listener.PropertyListenterclass>
    

 
설정이 완료되면 웹 응용 프로그램을 실행하면 모든 jsp 페이지에서 EL 표현식으로properties의 속성 값을 얻을 수 있습니다.
 
전재 대상:https://www.cnblogs.com/kenhome/p/7717952.html

좋은 웹페이지 즐겨찾기