1,项目中用的Maven做项目管理工具,在pom.xml中的必须配置如下:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.1.8.1</version>
<scope>test</scope>
</dependency>
这个是使用struts2-junit-plugin插件中必须的,当然我说的是ssh2的项目,肯定也少不了struts2-spring-plugin.jar的依赖了,如下:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.8.1</version>
</dependency>
2,struts.xml中的配置(简单的贴一下):
<action name="person_*" class="personAction" method="{1}">
<result name="adminLoginSuccess">/admin_index.jsp</result>
<result name="personLoginSuccess">/go_person_index.jsp</result>
。。。。
</action>
3,application-action.xml中的配置如下:
<bean id="personAction" class="com.woyo.action.PersonAction">
<property name="personBo" ref="personBo"></property>
<property name="adminBo" ref="adminBo"></property>
<property name="groupBo" ref="groupBo"></property>
。。。配置你的bo层
</bean>
4,在PersonAction中:
public class PersonAction extends BaseAction implements ModelDriven<Person>,Preparable{
private static final long serialVersionUID = 1L;
private PersonBO personBo;
private AdminBO adminBo;
private GroupBO groupBo;
。。。其他的就不写了,还有getter和setter
public String login(){
if(type==1){
Person p =personBo.findByName(name);
if(p!=null){
if(this.password.equals(p.getPassword())){
ServletActionContext.getRequest().getSession().setAttribute("loginPerson", p);
//ActionContext.getContext().getSession().put("loginPerson", p);
return "personLoginSuccess";
}
}else{
this.setMessage("用户名和密码不正确!");
}
}else{
Admin admin=adminBo.findByName(name);
if(admin!=null){
if(this.password.equals(admin.getPassword())){
ServletActionContext.getRequest().getSession().setAttribute("loginAdmin", admin);
//context.getContext().getSession().put("loginAdmin", admin);
return "adminLoginSuccess";
}
}else{
this.setMessage("管理员用户名或者密码不正确!");
}
}
return "login";
}
什么是PersonAction的代码,你也看到了,我有个注释的一行,//ActionContext.getContext().getSession().put("loginPerson", p);就是它
单元测试的时候这个ActionContext.getContext().getSession()老返回为null
就改用这个ServletActionContext.getRequest().getSession().setAttribute("loginPerson", p)了。
5,在PersonActionTest中:
public class PersonActionTest extends StrutsSpringTestCase {
private PersonAction personAction;
private PersonBO personBo;
private AdminBO adminBo;
private GroupBO groupBo;
private HttpSession session;
/**
* 默认查找classpath下的applicationContext.xml文件
* 下面的这个重写方法可以不写
*/
protected String getContextLocations() {
return "file:src/main/resources/applicationContext.xml";
}
public void testLogin() {
//从applicationContext中取得实体对象
personBo =(PersonBO)applicationContext.getBean("personBo");
adminBo=(AdminBO)applicationContext.getBean("adminBo");
groupBo=(GroupBO)applicationContext.getBean("groupBo");
//从struts2的配置文件中取得的实例
ActionProxy proxy = getActionProxy("/person/person_login.action");
personAction = (PersonAction) proxy.getAction();
int type=2;
if(type==1){
//测试person的登陆
personAction.setName("bill");
personAction.setPassword("pass");
personAction.setType(type);
String result=personAction.login();
PersonActionTest.assertEquals("personLoginSuccess", result);
}else{
//测试admin的登陆
personAction.setType(type);
personAction.setName("admin");
personAction.setPassword("pass");
String result=personAction.login();
PersonActionTest.assertEquals("adminLoginSuccess", result);
}
}
分享到:
相关推荐
花了好多时间搞了这个单元测试,在网上找相关的资料竟然都没有,我只有自己花时间搞了,搞这个ssh2,action的单元测试,花了好长时间,所有要5分了。
Eclipse搭建一个SSH2的空白项目,配置好数据库的信息直接启动即可运行测试 http://localhost:8081/ssh2/three/exe.action
struts2.1 spring2.5 hibernate3.2 很好的集成实例代码,Hibernate配置文件采用POjo注解的形势,spring管理action和BPO、DAO等 用MySQL数据库,用TestServer做JUnit测试时,会自动创建所需要的表
这是一个集成struts2+hibernate+spring+ajax的小型测试项目!对于初次学习和很想复习这方面知识的朋友很有帮助!
里面有我做公司项目前用MyEclipse搭建ssh框架测试通过后的一个项目,此项目添加了aop和事务支持,有完整的action测试; 还有自己做的3到4页的MyEclipse搭建SSH的傻瓜式教程,还有oracle数据库连接引擎的配置的图片...
本资源是在“ssh框架搭建实例源码4”基础上进行较好的测试和修改;进行部分代码注释;成功移植到ssm(strut2、spring、mybatis)实现了“hibernate和mybatis的之CRUD封装比较”...
SSH整合开发的一个实例,项目中有两张表(学生表与班级表)它们的关系是一对多的关系,项目中的action包是应用的表示层(Struts2)dao包是应用的数据访问层(Hibernate)domain是对应的两个类,由对象来生成关系,...
一,集成 Spring 与 Hibernate 1,配置SessionFactory 1,配置 ---------------------- applicationContext.xml ------------------------ ... 2,在struts.xml中配置action时,在class属性中写bean的名称
如果需要,Struts2 Action仍然可以访问初始的Request和Response。 但是,其他的元素减少或者消除了直接访问HttpServletRequest和HttpServletResponse的必要性。 d、可测性 测试struts1.2 Action的一个主要问题是...
ssh整合demo 整合, structs 附带了多个测试页面可以跳转,写了一个简单action,structs.xml 使用了模糊匹配, spring 注释方式 注入依赖,xml方式实现aop,hibernate 配置好数据库,可自动生成表,标准的model ,dao...
直接导入后 对DAO在spring注入sessionFactory,然后在Action中可以进行简单的调用,测试测试例子都在代码main函数写好了,希望对大家有帮助
声明式事务管理,include方式的模板管理,视图显示使用EL + struts标签库完成, 业务层 和 DAO 层层次清楚, 所有jar包已经提供,工程完整,可直接使用 完整的eclipse3.2.2 +Myeclipse 5.5 +tomcat6.0测试 JDK6.0...
经过测试: (Windows Server 2019,macOS Catalina和Ubuntu 20.04 / 18.04 / 16.04) /需要openssh-client软件包; apt install -y openssh-client /需要openssh-clients软件包; yum install -y openssh-...
cn.itcast.elec.web.action:系统控制层,负责页面和项目后台的跳转 cn.itcast.elec.web.form:封装值对象(VO对象),对应页面传递的表单值的属性 junit:测试类 * 配置文件 放置到src的目录下: beans.xml:...
ssh的源代码加jar包测试使用action访问跳转到页面,业务层controller层 继承com.opensymphony.xwork2.ActionSupport
本资料详细介绍了SSH架构的优缺点,可作为面试资料备用! 常说的好处 开源 常说的坏处 配置文件过大我就不提了 struts 优点: 收集,验证表单数据相比传统servlet简单 优雅的实现可配置的请求转发 ...
ssh-agent GitHub操作这个动作启动ssh-agent ... 在运行GitHub Action工作流以暂存项目,运行测试或构建映像时,您可能需要从私有存储库中获取其他库或供应商。 GitHub Actions只能访问其运行的存储库。 因此,为了访问
SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2、hibernate、mybatis等,今天我们来同时整合下 struts、spring、hibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合...
Struts2负责处理用户请求,将请求分发到相应的Action进行处理;Spring负责管理各个组件,如Service、DAO等,实现解耦和便于维护;Hibernate负责与MySQL数据库进行数据持久化操作,简化了数据库操作的复杂度。此外,...
免费获取Ubuntu SSH虚拟机;)这是什么该脚本可帮助您使用SSH获得虚拟Ubuntu服务器! 这适用于GitHub动作,感谢GitHub而不是我:... 注意:这仅用于测试和部署应用程序,我不鼓励滥用Github Action! 谢谢(c)Area69Lab