`
chenhuilove123
  • 浏览: 8300 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多
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的例子

    花了好多时间搞了这个单元测试,在网上找相关的资料竟然都没有,我只有自己花时间搞了,搞这个ssh2,action的单元测试,花了好长时间,所有要5分了。

    Eclipse搭建的原始ssh2项目

    Eclipse搭建一个SSH2的空白项目,配置好数据库的信息直接启动即可运行测试 http://localhost:8081/ssh2/three/exe.action

    SSH2 集成实例代码

    struts2.1 spring2.5 hibernate3.2 很好的集成实例代码,Hibernate配置文件采用POjo注解的形势,spring管理action和BPO、DAO等 用MySQL数据库,用TestServer做JUnit测试时,会自动创建所需要的表

    ssh集成的小型测试项目

    这是一个集成struts2+hibernate+spring+ajax的小型测试项目!对于初次学习和很想复习这方面知识的朋友很有帮助!

    MyEclipse的SSH框架搭建教程带干净项目、oracle表空间用户密码权限创建

    里面有我做公司项目前用MyEclipse搭建ssh框架测试通过后的一个项目,此项目添加了aop和事务支持,有完整的action测试; 还有自己做的3到4页的MyEclipse搭建SSH的傻瓜式教程,还有oracle数据库连接引擎的配置的图片...

    ssh框架搭建实例源码5

    本资源是在“ssh框架搭建实例源码4”基础上进行较好的测试和修改;进行部分代码注释;成功移植到ssm(strut2、spring、mybatis)实现了“hibernate和mybatis的之CRUD封装比较”...

    SSH整合开发的一个实例

    SSH整合开发的一个实例,项目中有两张表(学生表与班级表)它们的关系是一对多的关系,项目中的action包是应用的表示层(Struts2)dao包是应用的数据访问层(Hibernate)domain是对应的两个类,由对象来生成关系,...

    OA项目SSH整合框架

    一,集成 Spring 与 Hibernate 1,配置SessionFactory 1,配置 ---------------------- applicationContext.xml ------------------------ ... 2,在struts.xml中配置action时,在class属性中写bean的名称

    JAVA-SSH面试题

    如果需要,Struts2 Action仍然可以访问初始的Request和Response。 但是,其他的元素减少或者消除了直接访问HttpServletRequest和HttpServletResponse的必要性。 d、可测性 测试struts1.2 Action的一个主要问题是...

    ssh整合demo 整合在一起

    ssh整合demo 整合, structs 附带了多个测试页面可以跳转,写了一个简单action,structs.xml 使用了模糊匹配, spring 注释方式 注入依赖,xml方式实现aop,hibernate 配置好数据库,可自动生成表,标准的model ,dao...

    实用SSH分页DAO

    直接导入后 对DAO在spring注入sessionFactory,然后在Action中可以进行简单的调用,测试测试例子都在代码main函数写好了,希望对大家有帮助

    ssh框架实现通讯录

    声明式事务管理,include方式的模板管理,视图显示使用EL + struts标签库完成, 业务层 和 DAO 层层次清楚, 所有jar包已经提供,工程完整,可直接使用 完整的eclipse3.2.2 +Myeclipse 5.5 +tomcat6.0测试 JDK6.0...

    ssh-key-action:将SSH密钥安装到.ssh的GitHub Action

    经过测试: (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-...

    SSH 项目框架搭建总结

    cn.itcast.elec.web.action:系统控制层,负责页面和项目后台的跳转 cn.itcast.elec.web.form:封装值对象(VO对象),对应页面传递的表单值的属性 junit:测试类 * 配置文件 放置到src的目录下: beans.xml:...

    ssh框架demo

    ssh的源代码加jar包测试使用action访问跳转到页面,业务层controller层 继承com.opensymphony.xwork2.ActionSupport

    SSH架构优缺点分析.rar

    本资料详细介绍了SSH架构的优缺点,可作为面试资料备用! 常说的好处 开源 常说的坏处 配置文件过大我就不提了 struts 优点: 收集,验证表单数据相比传统servlet简单 优雅的实现可配置的请求转发 ...

    ssh-agent:GitHub用私钥设置`ssh-agent`的动作

    ssh-agent GitHub操作这个动作启动ssh-agent ... 在运行GitHub Action工作流以暂存项目,运行测试或构建映像时,您可能需要从私有存储库中获取其他库或供应商。 GitHub Actions只能访问其运行的存储库。 因此,为了访问

    struts2+spring+hibernate整合示例

    SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2、hibernate、mybatis等,今天我们来同时整合下 struts、spring、hibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合...

    源码基于JSP的企业级新闻系统(SSH+MYSQL).rar

    Struts2负责处理用户请求,将请求分发到相应的Action进行处理;Spring负责管理各个组件,如Service、DAO等,实现解耦和便于维护;Hibernate负责与MySQL数据库进行数据持久化操作,简化了数据库操作的复杂度。此外,...

    Ubuntu-SSH:从工作流获取SSH服务器;)

    免费获取Ubuntu SSH虚拟机;)这是什么该脚本可帮助您使用SSH获得虚拟Ubuntu服务器! 这适用于GitHub动作,感谢GitHub而不是我:... 注意:这仅用于测试和部署应用程序,我不鼓励滥用Github Action! 谢谢(c)Area69Lab

Global site tag (gtag.js) - Google Analytics