SSH整合的小例子

1、struts.xml

 

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE struts PUBLIC  
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="struts2" extends="struts-default" namespace="/">
		<action name="user" class="userAction">
			<result name="success">success.jsp</result>
			<result name="list">list.jsp</result>
			<result name="error">error.jsp</result>
		</action>
	</package>
</struts>

 

2、UserAction.java

 

package test.action;

import java.util.List;
import java.util.logging.Logger;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import test.model.User;
import test.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

@Component("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserService userService;
	private User user;
	private List<User> userList;

	public User getUser() {
		return user;
	}

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

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserService getUserService() {
		return userService;
	}

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

	public String addUser() {
		Logger.getAnonymousLogger().info("add action");
		if (userService.exits(user.getUsername())) {
			return ERROR;
		}
		userService.save(user);
		return SUCCESS;
	}

	public String listUser() {
		Logger.getAnonymousLogger().info("list action");
		this.userList = userService.list();
		return "list";
	}

}

 3、UserDAO.java

 

 

package test.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import test.model.User;

@Component
public class UserDAO {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void save(User user) {
		hibernateTemplate.save(user);
	}

	@SuppressWarnings("unchecked")
	public List<User> findByUsername(String username) {
		 return
		 (List<User>)hibernateTemplate.find("from User u where u.username = ?",username);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> listUser(){
		return hibernateTemplate.find("from User u");
	}
}

 4、Struts2ListenerOnBAE.java

 

 

package test.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
import ognl.OgnlRuntime;
 
public class Struts2ListenerOnBAE implements ServletContextListener,
		HttpSessionListener, HttpSessionAttributeListener {
 
	public void contextInitialized(ServletContextEvent sce) {
		OgnlRuntime.setSecurityManager(null);
	}
 
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
 
	}
 
}

 5、User.java

 

 

package test.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

	private int id;
	private String username;
	private String password;
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "user:"+username+",pwd:"+password;
	}
}

 6、UserService.java

 

 

package test.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import test.dao.UserDAO;
import test.model.User;
@Component
public class UserService {
	private UserDAO userDAO = new UserDAO();;

	public UserDAO getUserDAO() {
		return userDAO;
	}
	@Resource
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void save(User user) {
		System.out.println(user);
		userDAO.save(user);
	}
	
	public List<User> list(){
		return userDAO.listUser();
	}
	
	public boolean exits(String username){
		List<User> users = userDAO.findByUsername(username);
		if(users!=null&&users.size()>0){
			return true;
		}
		return false;
	}
}

其余的文件自附件里!

 

你可能感兴趣的:(spring,Hibernate,struts,ssh,J2EE)