`

Spring与Struts整合3种方式实例

阅读更多

Spring与Struts整合3种方式实例

 

Spring 和 Struts的整合有3种方式:

1:通过Spring的ActionSupport类

2:通过Spring的DelegatingRequestProcessor类

3:通过Spring的DelegatingActionProxy类

 

 

a: 通过Spring的ActionSupport类:(对应工程:SpringStruts)

方法是Action类不再继承Struts的Action而是继承Spring提供的ActionSupport,

然后在Action中获得Spring的ApplicationContext.

缺点是Action和Spring耦合在一起,而且Action不在Spring控制之内。也不能处理多个动作在一个Action中的情况。

 

步骤:

1:加入spring.

2: 加入struts

3:修改struts配置文件struts-config.xml文件注册ContextLoaderPlugIn插件。

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

</plug-in>

4:创建Action时:

(1) 处,通过从 Spring 的 ActionSupport 类而不是 Struts 的 Action 类进行扩展,创建了一个新的 Action。

(2) 处,使用 getWebApplicationContext() 方法获得一个 ApplicationContext。为了获得业务服务,我使用在

(3) 处 查找一个 Spring bean。

//(1)

public class LoginAction extends ActionSupport {

 

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) {

LoginForm loginForm = (LoginForm) form;

// TODO Auto-generated method stub

//(2)

ApplicationContext ac = this.getWebApplicationContext();//获得ApplicationContext

//(3)

LoginInterface li = (LoginInterface)ac.getBean("loginInterface");//获得Bean

boolean you = li.checkUser(loginForm.getName(),loginForm.getPassword());

if(you){

request.setAttribute("msg","welcome");

return mapping.findForward("show");

}

else{

request.setAttribute("msg","failed");

return mapping.findForward("show");

}

}

 

}

applicationContext.xml:

<beans>

<bean id="loginInterface" class="spring.LoginImp"/>

 

</beans>

 

 

b: 通过Spring的DelegatingRequestProcessor类:(对应工程:SpringStruts2)

方法是Spring的DelegatingRequestProcessor代替Struts的RequstProcessor,

把Struts的Action置于Spring的的控制之下

缺点是开发人员可以自己定义RequestProcessor这样就需要手工整合Struts和Spring。

 

步骤:

1:加入spring.

2: 加入struts

3:修改struts配置文件struts-config.xml文件注册ContextLoaderPlugIn插件。

<struts-config>

<form-beans >

<form-bean name="loginForm" type="com.yourcompany.struts.form.LoginForm" />

</form-beans>

 

<action-mappings >

<action

attribute="loginForm"

input="/login.jsp"

name="loginForm"

path="/login"

scope="request"

type="com.yourcompany.struts.action.LogAction">

<forward name="show" path="/show.jsp" />

</action>

 

</action-mappings>

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

</plug-in>

</struts-config>

4:创建Action时:

public class LogAction extends Action {

private LoginInterface logInterface;

 

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) {

LoginForm loginForm = (LoginForm) form;

// TODO Auto-generated method stub

boolean you = logInterface.checkUser(loginForm.getName(),loginForm.getPassword());

if(you){

request.setAttribute("msg","welcome");

return mapping.findForward("show");

}

else{

request.setAttribute("msg","failed");

return mapping.findForward("show");

}

}

public void setLogInterface(LoginInterface logInterface) {

this.logInterface = logInterface;

}

 

}

 

applicationContext.xml:

<beans>

<bean id="loginInterface" class="spring.LoginImp"/>

<!--要和Struts的路径对应-->

<bean name="/login" class="com.yourcompany.struts.action.LogAction">

<property name="logInterface">

<ref bean="loginInterface"/>

</property>

</bean>

</beans>

 

 

c: 通过Spring的DelegatingActionProxy类:(对应工程:SpringStruts3)

方法是Spring的DelegatingActionProxy代替Struts的Action,

把Struts的Action置于Spring的的控制之下

这种方式最灵活强大。并且它可以利用 Spring AOP 特性的优点。

 

步骤:

1:加入spring.

2: 加入struts

3:修改struts配置文件struts-config.xml文件注册ContextLoaderPlugIn插件。

<struts-config>

<data-sources />

<form-beans >

<form-bean name="loginForm" type="com.yourcompany.struts.form.LoginForm" />

</form-beans>

<action-mappings >

<action

attribute="loginForm"

input="/form/login.jsp"

name="loginForm"

path="/login"

scope="request"

type="org.springframework.web.struts.DelegatingActionProxy">

<forward name="show" path="/show.jsp" />

</action>

</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

</plug-in>

</struts-config>

 

4:创建Action时:

public class LogAction extends Action {

private LoginInterface logInterface;

 

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) {

LoginForm loginForm = (LoginForm) form;

// TODO Auto-generated method stub

boolean you = logInterface.checkUser(loginForm.getName(),loginForm.getPassword());

if(you){

request.setAttribute("msg","welcome");

return mapping.findForward("show");

}

else{

request.setAttribute("msg","failed");

return mapping.findForward("show");

}

}

public void setLogInterface(LoginInterface logInterface) {

this.logInterface = logInterface;

}

 

}

 

applicationContext.xml:

<beans>

<bean id="loginInterface" class="spring.LoginImp"/>

<!--要和Struts的路径对应-->

<bean name="/login" class="com.yourcompany.struts.action.LogAction">

<property name="logInterface">

<ref bean="loginInterface"/>

</property>

</bean>

</beans>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics