日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring在WEB中的应用。

發布時間:2025/7/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring在WEB中的应用。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1:創建IOC容器。在WEB應用程序啟動的時候就創建。利用到監聽器。

ServletContextListener類的contextInitialized方法中

1 package com.struts2.listeners; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletContextEvent; 5 import javax.servlet.ServletContextListener; 6 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 10 public class SpringServletContextListener implements ServletContextListener{ 11 12 public void contextDestroyed(ServletContextEvent arg0) { 13 // TODO Auto-generated method stub 14 15 } 16 17 public void contextInitialized(ServletContextEvent arg0) { 18 //1:applicationContext.xml在web.xml中進行創建。然后利用ServletContext獲取到。 19 ServletContext sc=arg0.getServletContext(); 20 String config=sc.getInitParameter("configLocation"); 21 //創建IOC容器 22 ApplicationContext act=new ClassPathXmlApplicationContext(config); 23 //把創建的IOC容器放到ServletContext(即application域)中 24 sc.setAttribute("ApplicationContext", act); 25 } 26 27 }

在web.xml中創建監聽器和applicationContext.xml

<context-param><param-name>configLocation</param-name><param-value>applicationContext.xml</param-value></context-param><listener><listener-class>com.struts2.listeners.SpringServletContextListener</listener-class></listener>

然后創建一個實體:Person

package com.struts2.entyties;public class Person {private String username;public void setUsername(String username) {this.username = username;}public void hello(){System.out.println("My name is " + username);}}

然后創建bean

    <bean id="person" class="com.struts2.entyties.Person"><property name="username" value="陸偉"></property></bean>

然后寫個servlet去使用:

package com.struts2.servlet;import java.io.IOException;import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;import com.struts2.entyties.Person;public class TestServlet extends HttpServlet{private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1. 從 application 域對象中得到 IOC 容器的引用ServletContext sc=getServletContext();ApplicationContext act=(ApplicationContext) sc.getAttribute("ApplicationContext");//2. 從 IOC 容器中得到需要的 beanPerson person = act.getBean(Person.class);person.hello();}}

在web.xml中加載servlet

<servlet><description></description><display-name>TestServlet</display-name><servlet-name>TestServlet</servlet-name><servlet-class>com.struts2.servlet.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/TestServlet</url-pattern></servlet-mapping>

然后寫個頁面進行訪問:

<a href="TestServlet">TestServlet</a>

?

轉載于:https://www.cnblogs.com/bulrush/p/8000934.html

總結

以上是生活随笔為你收集整理的spring在WEB中的应用。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。