日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
如何在JavaWeb應(yīng)用中獲取Spring的ApplicationContext

在Web開(kāi)發(fā)開(kāi)發(fā)中,常常需要從JSP或者Servlet或者Action中獲取ApplicationContext對(duì)象,這時(shí)候,就無(wú)法使用new關(guān)鍵字通過(guò)查找配置文件來(lái)實(shí)例化ApplicationContext這個(gè)對(duì)象了。Spring通過(guò)WebApplicationContextUtils可以方便實(shí)現(xiàn)您的需求。下面看個(gè)例子:

創(chuàng)新互聯(lián)是一家專(zhuān)注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作與策劃設(shè)計(jì),永登網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:永登等地區(qū)。永登做網(wǎng)站價(jià)格咨詢(xún):18982081108

Spring2.5+Struts2環(huán)境下

1. 配置web.xml,通過(guò)這個(gè)配置來(lái)獲取的。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">
   ?。糲ontext-param>
       ?。紁aram-name>contextConfigLocation</param-name>
       ?。紁aram-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
   ?。糵ilter>
       ?。糵ilter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   ?。?filter>
    <filter-mapping>
       ?。糵ilter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
   ?。?filter-mapping>
   ?。糽istener>
       ?。糽istener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   ?。約ervlet>
        <servlet-name>dispatcher</servlet-name>
       ?。約ervlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   ?。約ervlet-mapping>
       ?。約ervlet-name>dispatcher</servlet-name>
       ?。紆rl-pattern>*.form</url-pattern>
   ?。?servlet-mapping>
</web-app>

2. 在JSP、Servlet、Action中獲取ApplicationContext

<%@ page import="lavasoft.service.TestService" %>

<%@ page import="org.springframework.context.ApplicationContext" %>

<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head><title>Simple jsp page</title></head>

<body>

<%

//    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());

    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());

    TestService service = (TestService) ctx.getBean("testService");

    String s = service.test();

    out.print(s);

%>

</body>

</html>

Spring+JSP的環(huán)境

在此環(huán)境下web.xml配置會(huì)有些變化:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

           version="2.5">

    <context-param>

       ?。紁aram-name>contextConfigLocation</param-name>

       ?。紁aram-value>/WEB-INF/applicationContext.xml</param-value>

   ?。?context-param>

   ?。糽istener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

   ?。約ervlet>

        <servlet-name>dispatcher</servlet-name>

       ?。約ervlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

       ?。糽oad-on-startup>1</load-on-startup>

    </servlet>

   ?。約ervlet-mapping>

       ?。約ervlet-name>dispatcher</servlet-name>

        <url-pattern>*.form</url-pattern>

   ?。?servlet-mapping>

</web-app>

獲取的方式和上述完全一樣。

【編輯推薦】

  1. Spring中XML配置的12個(gè)技巧
  2. Spring MVC框架的高級(jí)配置
  3. Spring中的事務(wù)傳播屬性詳解

文章題目:如何在JavaWeb應(yīng)用中獲取Spring的ApplicationContext
URL分享:http://www.5511xx.com/article/dhoiscp.html