春天怎么与struts结合使用

  介绍

这期内容当中小编将会给大家带来有春天关怎么与struts结合使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

struts调用流程如下图所示。

春天怎么与struts结合使用“> </p> <p>,,,,,看到这幅图一下子就能了解了struts的原理.Spring的核心就是国际奥委会容器和AOP,所以我们用弹簧主要是管理业务对象和事务的管理,所以主要的是模型层来让弹簧管理,这是我们的一种方案。<br/> </p> <p> </p> <p>,,,,,还记的在上篇文章中,测试的时候是在单元测试中拿到的BeanFactory,与struts结合就是在行动中取得BeanFactory。步骤如下。</p> <p> 1,,,,,,,,,,,建立一个web项目。</p> <p> 2,,,,,,,,,,,建立相关页面,代码如下所示。</p> <p>,,,登录。jsp代码入下所示。</p> <pre类= & lt; % @ pagelanguage=癑ava"contentType=皌ext/html;charset=GB18030"   pageEncoding=癎B18030" %比;   & lt; !DOCTYPE html PUBLIC" -//W3C//DTD html 4.01过渡//EN"“http://www.w3.org/TR/html4/loose.dtd"比;   & lt; html>   & lt; head>   & lt; metahttp-equiv=癈ontent-Type"内容=皌ext/html; charset=GB18030"比;   & lt; title>插入titlehere   & lt;/head>   & lt; body>   & lt; formaction=發ogin.do"方法=皃ost"比;   用户:& lt;输入类型=皌ext" name=皍sername"祝辞& lt; br>   密码:& lt;输入类型=皃assword" name=皃assword"祝辞& lt; br>   & lt; inputtype=皊ubmit"值=https://www.yisu.com/zixun/钡锹?>   

Login_success.jsp 

& lt; % @ pagelanguage=癹ava"contentType=皌ext/html;charset=GB18030"   pageEncoding=癎B18030" %比;   & lt; !DOCTYPE html PUBLIC" -//W3C//DTD html 4.01过渡//EN"“http://www.w3.org/TR/html4/loose.dtd"比;   & lt; html>   & lt; head>   & lt; metahttp-equiv=癈ontent-Type"内容=皌ext/html; charset=GB18030"比;   & lt; title>插入titlehere   & lt;/head>   & lt; body>   xx,用户登录成功!      & lt;/body>   & lt;/html>

3,配置struts环境,关于struts的配置,拷贝struts和jstl的依赖包;在web . xml中配置ActionServlet,提供struts - config . xml文件。前篇文中有说明,在此就不赘述了。

struts-config。xml代码如下所示。

& lt; struts-config>   & lt; form-beans>   & lt; form-beanname=發oginForm"类型=癱om.bjpowernode.usermgr.web.forms.LoginActionForm"祝辞& lt;/form-bean>   & lt;/form-beans>   & lt; action-mappings>   & lt; actionpath=?login"   类型=癱om.bjpowernode.usermgr.web.actions.LoginAction"   name=發oginForm"   范围=皉equest"   比;   & lt; forwardname=皊uccess"路径=?login_success.jsp"/比;   & lt;/action>   & lt;/action-mappings>   & lt; message-resourcesparameter=皉esources.MessageResources"/比;   & lt;/struts-config>

4,配置弹簧环境,拷贝弹簧相关jar包,建立弹簧配置文件applicationContext-beans.xml。

applicationContext-beans。xml代码如下所示。

& lt; beansxmlns=癶ttp://www.springframework.org/schema/beans"   xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns: aop=癶ttp://www.springframework.org/schema/aop"   xmlns: tx=癶ttp://www.springframework.org/schema/tx"   xsi: schemaLocation=癶ttp://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-2.0.xsd   http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd   http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"比;   & lt; beanid=皍serManager"类=癱om.bjpowernode.usermgr.manager.UserManagerImpl"/比;   & lt;/beans>

5,建立相关的行动和ActionForm。代码如下所示。

,,,LoginAction。java代码如下所示。

公共类LoginAction extendsAction {
  @Override
  publicActionForward执行(ActionMapping映射,ActionForm形式,
  HttpServletRequestrequest HttpServletResponse响应)
  throwsException {
  LoginActionFormlaf=(LoginActionForm)形式;
  Stringusername=laf.getUsername ();
  Stringpassword=laf.getPassword ();//但是我们每次都要去调用,去创建太麻烦了。//我们在这里只需要去配置侦听器就可以了,春天给实现好了。
  BeanFactoryfactory=newClassPathXmlApplicationContext (“applicationContext.xml");
  UserManageruserManager=(UserManager) factory.getBean (“userManager");
  userManager.login(用户名、密码);
  }
  }

春天怎么与struts结合使用