关于SSH 用户注册的例子

这两天一直在集成SSH,错误百出今天终于搞定了菜鸟一个呵呵

之前是先把struts2和spring集成了然后把spring和hibernate集成了大概知道怎么回事之后今天终于搞定了

/**
 * @author jeedroid
 */
package com.jeedroid.dao;

import com.jeedroid.model.User;

public interface UserDao
{
public abstract boolean addUser(User user);
}
package com.jeedroid.daoimpl;

import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.jeedroid.dao.UserDao;
import com.jeedroid.model.User;

public class UserDaoImpl implements UserDao
{
	private SessionFactory sessionFactory;
	
	
	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean addUser(User user)
	{
		try {
			HibernateTemplate hibernateTemplate =new HibernateTemplate(sessionFactory);
			hibernateTemplate.saveOrUpdate(user);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
		
	}

}
/**
 * @author jeedroid
 */
package com.jeedroid.action;

import com.jeedroid.daoimpl.UserDaoImpl;
import com.jeedroid.model.User;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport
{
private User user;
private UserDaoImpl userDaoImpl;

public User getUser()
{
	return user;
}

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

public void setUserDaoImpl(UserDaoImpl userDaoImpl)
{
	this.userDaoImpl = userDaoImpl;
}

@Override
public String execute() throws Exception
{
	if(userDaoImpl.addUser(user))
			return SUCCESS;
	else
		return INPUT;
}

}

<struts>

<constantname="struts.enable.DynamicMethodInvocation"value="false"/>

<constantname="struts.devMode"value="true"/>

<packagename="struts2"extends="struts-default">

<actionname="register"class="registerAction">

<resultname="success">/success.jsp</result>

<resultname="input">/register.jsp</result>

</action>

</package>

</struts>

applicationContext.xml:

<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<propertyname="driverClassName">

<value>com.mysql.jdbc.Driver</value>

</property>

<propertyname="url">

<value>jdbc:mysql://localhost:3306/spring_hibernate</value>

</property>

<propertyname="username">

<value>root</value>

</property>

<propertyname="password">

<value>123321</value>

</property>

</bean>

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<propertyname="dataSource">

<reflocal="dataSource"/>

</property>

<propertyname="hibernateProperties">

<props>

<propkey="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<propkey="hibernate.show_sql">true</prop>

</props>

</property>

<propertyname="mappingResources">

<list>

<value>com/jeedroid/model/User.hbm.xml</value>

</list>

</property>

</bean>

<beanid="userDaoImpl"class="com.jeedroid.daoimpl.UserDaoImpl">

<propertyname="sessionFactory">

<refbean="sessionFactory"/>

</property>

</bean>

<beanid="registerAction"class="com.jeedroid.action.RegisterAction">

<propertyname="userDaoImpl">

<reflocal="userDaoImpl"/>

</property>

</bean>

之前一直报attempttocreatesaveOrUpdateeventwithnullentity这个错误,后来把User.java里的含参构造函数去掉就行了应该是struts2传值过来时实例化的问题

只是一个简单的用户注册没有加事务了什么的看看就行了呵呵

ssh

相关推荐