已經(jīng)有很多人寫過文章,來介紹和說明這個問題了。
我在學(xué)習(xí)和使用之后,就也作了一下總結(jié)。
第一步:
配置和struts環(huán)境
第二步:
載入spring包
第三步:
在struts-config.xml中作spring插件配置,代碼如下:
? <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
??? <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
? </plug-in>
?
第四步:
在action中使用spring.
要使用spring,首先就要取得spring的控制權(quán),也就是取得ApplicationContext。
首先,如果我們了解了插件初始化的結(jié)果的話,那么我們看:
java.util.Enumeration e = this.getServlet().getServletContext().getAttributeNames();
??? ???
??? ??? while(e.hasMoreElements()){
??? ??? ??? String s = (String)e.nextElement();
??? ??? ???
??? ??? ??? System.out.println(s);
??? ??? }
??? ???
這一段程序?qū)@示所有的初始化的參數(shù)變量,顯示如下:
org.apache.catalina.jsp_classpath
org.apache.struts.action.MESSAGE
org.apache.struts.util.PREFIXES
org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.org.apache.catalina.WELCOME_FILES
org.apache.struts.action.ACTION_SERVLET
org.apache.struts.action.FORWARDS
org.apache.struts.action.REQUEST_PROCESSOR
javax.servlet.context.tempdir
org.apache.struts.action.MAPPINGS
org.apache.struts.action.SERVLET_MAPPING
org.apache.struts.action.FORM_BEANS
org.apache.struts.action.PLUG_INS
org.apache.catalina.resources
org.apache.struts.action.MODULE
藍色的部分就是spring初始化后的結(jié)果,也就是,spring初始化后的結(jié)果WebApplicationContext保存在
org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.
變量里面。那么我們就可以使用如下的方式來取得此對象:
??? ??? ApplicationContext ctx = (ApplicationContext)this.getServlet().getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
??? ??? System.out.println("ctx? is? :? " + ctx);
還有一個方法,就是使用spring給我們提供的接口,我們在構(gòu)建action繼承自ActionSuport,那么,我們就可以通過下面的方法來訪問:
??? ??? ApplicationContext ctx = this.getWebApplicationContext();
??? ??? System.out.println("the application context is :? " + ctx);
接下來,我們就可以測試一下,我們的spring.
先創(chuàng)建一個bean:
package org.zy.pro.demo.sd.bean;
public class User {
??? private String username;
??? private String password;
??? public String getPassword() {
??? ??? return password;
??? }
??? public void setPassword(String password) {
??? ??? this.password = password;
??? }
??? public String getUsername() {
??? ??? return username;
??? }
??? public void setUsername(String username) {
??? ??? this.username = username;
??? }
}
然后再spring的配置文件里面配置,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
??? <bean id="user" class="org.zy.pro.demo.sd.bean.User">
??? ??? <property name="username">
??? ??? ??? <value>everybody</value>
??? ??? </property>
??? ???
??? ??? <property name="password">
??? ??? ??? <value>everybodypassword</value>
??? ??? </property>
??? </bean>
</beans>
好了,調(diào)用一下吧:
創(chuàng)建一個action,
package org.zy.pro.demo.sd.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.web.struts.ActionSupport;
import org.zy.pro.demo.sd.bean.User;
public class Bus extends ActionSupport {
??? @Override
??? public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
??? ??? // TODO Auto-generated method stub
??? ???
??? ??? ApplicationContext ctx = this.getWebApplicationContext();
??? ??? System.out.println("the application context is :? " + ctx);
??? ???
??? ??? User user = (User) ctx.getBean("user");
??? ??? System.out.println("username is : "+user.getUsername());
???
??? ??? return super.execute(arg0, arg1, arg2, arg3);
??? }
}
然后再struts-config.xml文件里面配置action如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
? <data-sources />
? <form-beans />
? <global-exceptions />
? <global-forwards />
? <action-mappings >
??? <action path="/map" type="org.zy.pro.demo.sd.action.MapAction" />
??? <action path="/bus" type="org.zy.pro.demo.sd.action.Bus" />?
? </action-mappings>
?
? <message-resources parameter="org.zy.pro.demo.sd.ApplicationResources" />
?
? <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
??? <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
? </plug-in>
?
</struts-config>
好了,發(fā)布,測試一下,結(jié)果如下:
the application context is :? org.springframework.web.context.support.XmlWebAppl
icationContext: display name [WebApplicationContext for namespace 'action-servle
t']; startup date [Thu Jul 05 18:23:56 CST 2007]; root of context hierarchy; con
fig locations [/WEB-INF/applicationContext.xml]
username is : everybody
|----------------------------------------------------------------------------------------|
版權(quán)聲明 版權(quán)所有 @zhyiwww
引用請注明來源 http://m.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2007-07-05 18:27
zhyiwww 閱讀(1165)
評論(0) 編輯 收藏 所屬分類:
j2ee