没有合适的资源?快使用搜索试试~ 我知道了~
spring讲义4.txt
需积分: 10 10 下载量 47 浏览量
2007-11-12
16:09:02
上传
评论
收藏 17KB TXT 举报
温馨提示
试读
18页
spring讲义4.txtspring讲义4.txt
资源推荐
资源详情
资源评论
Spring 和 Struts的整合使用
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的的控制之下
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的的控制之下
剩余17页未读,继续阅读
资源评论
lixinli1029
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功