当前位置:首页 > 开发 > 编程语言 > Hibernate > 正文

Maven + Spring + Hibernate + Struts2 整合第三部分

发表于: 2009-03-13   作者:asialee   来源:转载   浏览次数:
摘要: 现在我们写一个简单的单元测试来测试一下我们的代码,看看是不是配置文件都配好了。在src/test/java下面建立UserDAOTest.java文件,代码如下: package localhost.login.dao; import java.util.List; import junit.framework.Assert; import junit.framework.
现在我们写一个简单的单元测试来测试一下我们的代码,看看是不是配置文件都配好了。在src/test/java下面建立UserDAOTest.java文件,代码如下:
package localhost.login.dao;

import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;
import localhost.login.domain.User;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserDAOTest extends TestCase{
	private ApplicationContext context = null;
	
	private User user = null;
	
	private UserDAO userDAO = null;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		userDAO = (UserDAO)context.getBean("userDAO");
	}
	
	@Override
	protected void tearDown() throws Exception {
		userDAO = null;
		user = null;
	}
	
	public void testAddUser(){
		user = new User();
		user.setName("username");
		user.setPassword("password");
		userDAO.addUser(user);
		Assert.assertEquals(1, user.getId());
		userDAO.addUser(user);
		Assert.assertEquals(1, user.getId());
		
		User userTwo = new User();
		userTwo.setName("username1");
		userTwo.setPassword("password");
		userDAO.addUser(userTwo);
		Assert.assertEquals(2, userTwo.getId());
		
	}
	
	public void testlistAll(){
		List<User> users = userDAO.listAll();
		Assert.assertEquals(users.size(), 2);
	}
}


现在我们建立一个简单的辅助类,来获得ApplicationContext, 将它放在localhost.login.util包下面:
package localhost.login.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InitApplicationContext {
	
	private static ApplicationContext context = null;
	
	private InitApplicationContext(){
		
	}
	public static ApplicationContext getApplicationContext(){
		if(context == null){
			context = new ClassPathXmlApplicationContext("applicationContext.xml");
		}
		return context;
	}
}


现在我们建立用户登陆的action,struts2的action比较简单,请看下面代码:
package localhost.login.action;

import java.util.List;

import localhost.login.domain.User;
import localhost.login.service.UserService;
import localhost.login.util.InitApplicationContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport {
	private UserService userService;
	private User user;

	public UserLoginAction() {
		ApplicationContext context = InitApplicationContext.getApplicationContext();
		userService = (UserService) context.getBean("userService");
	}

	@Override
	public String execute() throws Exception {
		if (!isValid(user.getName())) {
			return INPUT;
		}
		if (!isValid(user.getPassword())) {
			return INPUT;
		}
		if(!userCheck(user)){
			return INPUT;
		}
		return SUCCESS;
	}

	public boolean isValid(String keyword) {
		return keyword != null && keyword != "";
	}

	public boolean userCheck(User user) {
		List<User> userList = userService.findUserByName(user.getName());
		if (userList == null || userList.size() < 1) {
			return false;
		}
		User checkUser = userList.get(0);
		if (user.getName().equals(checkUser.getName())
				&& user.getPassword().equals(checkUser.getPassword())) {
			return true;
		}
		addActionError("Username or password is wrong, please check!");
		return false;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}

struts2比struts1最大的优点就是使得单元测试比较容易,现在我们就建立一个简单的测试用例来测试我们刚建立的action
把它放到src/main/java包底下,代码如下:
package localhost.login.action;
import junit.framework.Assert;
import junit.framework.TestCase;
import localhost.login.domain.User;
import localhost.login.service.UserService;
import localhost.login.util.InitApplicationContext;

import org.springframework.context.ApplicationContext;

import com.opensymphony.xwork2.Action;


public class UserLoginActionTest extends TestCase{
	private User user;
	
	private UserService userService;
	
	private ApplicationContext context = null;
	
	private UserLoginAction loginAction = null;
	
	public void setUp(){
		context = InitApplicationContext.getApplicationContext();
		loginAction = new UserLoginAction();
		userService = (UserService)context.getBean("userService");
	}
	
	public void testUserLogin(){
		user = new User();
		user.setName("username");
		user.setPassword("password");
		try {
			loginAction.setUser(user);
			loginAction.setUserService(userService);
			Assert.assertEquals(Action.SUCCESS, loginAction.execute());
			user.setPassword("password1");
			System.out.println(loginAction.getActionErrors());
			Assert.assertEquals(Action.INPUT, loginAction.execute());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


现在到我们配置web.xml的时候了,到目前为止,它现在还是空的。
我们加上struts2的过滤器和spring的监听器。请看下面代码:
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
	<display-name>Common Login</display-name>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispather.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

Maven + Spring + Hibernate + Struts2 整合第三部分

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
第一次发帖:请大家见谅。 在Maven环境下搭建SpringMVC,比较快速,直接在项目上右击找到MyEclipse然
以下随便写写,自己最近捣弄的东西。maven,其实tapestry+spring + hibernate网站上都有一大堆整合
Struts2,Spring,Hibernate整合例子一个(自己笔记) 开发本项目的具体步骤请看:Struts2,Spring,Hib
[程序开发]Struts2 Spring Hibernate 的简单整合 创建工程,选择java EE5 首先,加载Spring框架,如
整合之前一定要的具备的包(在这里我使用的是Annotations方式来整合) web.xml的配置 <?xml versio
maven 的基本属性,请参看其帮助文档,或者参看网络上一些专门介绍maven的文章。 一下从一个tepestr
本文转载自 Struts 2 - Spring 4 - Hibernate 4 Integration Tutorial Part 1 - XML Configuration
初学 Struts2,在整合 Spring 和 Hibernate 时遇到一些麻烦,下面以一个简单样例说明操作过程,以备
第一步:导入所需jar包: 具体所需jar包见附件图片 第二步:修改jbpm配置文件: 1、src目录下建立jb
最近公司要用struts2、spring3、hibernate3整合,自己弄了一套,网上有好多helloworld例子,我也记
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号