組態Spring監聽器 「org.springframework.web.context.ContextLoaderListener」 到 web.xml 檔案中。
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Struts 2 Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
註冊所有的Spring Beans 組態在 applicationContext.xml 檔案中, Spring監聽器會自動找到這個 XML 檔案。
applicationContext.xml
<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.5.xsd"> <bean id="userBo" class="com.yiibai.user.bo.impl.UserBoImpl" /> <bean id="userSpringAction" class="com.yiibai.user.action.UserSpringAction"> <property name="userBo" ref="userBo" /> </bean> </beans>
UserBo.java
package com.yiibai.user.bo; public interface UserBo{ public void printUser(); }
UserBoImpl.java
package com.yiibai.user.bo.impl; import com.yiibai.user.bo.UserBo; public class UserBoImpl implements UserBo{ public void printUser(){ System.out.println("printUser() is executed..."); } }
UserSpringAction.java
package com.yiibai.user.action; import com.yiibai.user.bo.UserBo; public class UserSpringAction{ //DI via Spring UserBo userBo; public UserBo getUserBo() { return userBo; } public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String execute() throws Exception { userBo.printUser(); return "success"; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="userAction" class="com.yiibai.user.action.UserAction" > <result name="success">pages/user.jsp</result> </action> <action name="userSpringAction" class="userSpringAction" > <result name="success">pages/user.jsp</result> </action> </package> </struts>
//struts.xml <action name="userSpringAction" class="userSpringAction" > <result name="success">pages/user.jsp</result> </action> //applicationContext.xml <bean id="userSpringAction" class="com.yiibai.user.action.UserSpringAction"> <property name="userBo" ref="userBo" /> </bean>
要存取此操作,請使用網址: http://localhost:8080/struts2spring/userSpringAction.action
預設情況下,Spring監聽器啟用 「通過匹配bean的名字自動裝配「。 因此,它會通過setUserBo自動傳遞Spring 「userBo」 Bean 到UserAction。請參閱下面的Struts2動作:
UserAction.java
package com.yiibai.user.action; import com.yiibai.user.bo.UserBo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ //DI via Spring UserBo userBo; public UserBo getUserBo() { return userBo; } public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String execute() throws Exception { userBo.printUser(); return SUCCESS; } }
要存取此操作,請使用網址: http://localhost:8080/struts2spring/userAction.action
package com.yiibai.user.action; import org.apache.struts2.ServletActionContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.yiibai.user.bo.UserBo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ public String execute() throws Exception { WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext( ServletActionContext.getServletContext() ); UserBo userBo1 = (UserBo)context.getBean("userBo"); userBo1.printUser(); return SUCCESS; } }