1.所有的jar 包
2.struts.properties struts.custom.i18n.resources=globalMessages 3.struts.xml <!DOCTYPE struts PUBLIC <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="SSH2" extends="struts-default"> 4.web.xml <?xml version="1.0" encoding="UTF-8"?> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encodingFilter</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <listener> <welcome-file-list> </web-app> 5.aplicationContext.xml <?xml version="1.0" encoding="UTF-8" ?> <bean id="myDataSource1" <!-- 管理Hibernate --> <bean id="sessionFactory" </bean> <bean id="userdao" class="ssh2.news.dao.impl.UserDaoImpl"> <property name="sessionFactory"> </bean> <bean id="userservice" class="ssh2.news.service.impl.UserServiceImpl"> <property name="userDao"> <ref bean="userdao"/> </property> </bean> <bean id="loginAction" class="ssh2.news.action.LoginAction"> </beans> 6.index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" %> <div style="color: red"><s:fielderror /><s:actionmessage /></div> <s:form action="login" method="post"> <s:textfield name="user.uid" label="UID" tooltip="ENTER YOUR UID" /> <s:password name="user.password" label="PASSWORD" tooltip="ENTER YOUR PASSWORD" /> <s:submit></s:submit> </s:form> <s:a href="regist.jsp">REGIST NEW COUNT</s:a></center> </body> 7.java文件结构 8.action package ssh2.news.action; import java.util.Map; import ssh2.news.service.UserService; import ssh2.news.vo.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 登录Action * @author bulktree * */ public class LoginAction extends ActionSupport { private User user; private UserService userService; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() throws Exception { if (isInvalid(user.getUid())) { this.addFieldError("uid", "登录ID不能为空"); return INPUT; } if (isInvalid(user.getPassword())) { this.addFieldError("password", "密码项不能为空"); return INPUT; }
String uname = userService.isLogin(user.getUid(), user.getPassword()); if (uname != null) { Map session = ActionContext.getContext().getSession(); session.put("uname", uname); session.put("uid", user.getUid()); } else { this.addFieldError("idorpassword", "登录ID或密码错误"); return INPUT; } } private boolean isInvalid(String value) { return (value == null || value.length() == 0); } } 9.bean package ssh2.news.vo; public class User { private String uid; private String uname; private String password; public String getPassword() { 10. Hibernate 的 xml 文件 User.hbm.xml <?xml version="1.0"?> 11.Dao接口类 package ssh2.news.dao; import java.util.List; import ssh2.news.vo.User; public interface UserDao { /** *增加一个用户 */ public void addUser(User user) throws Exception; /** *根据uid/password查询User */ public User queryByUidAndPassword(String uid, String password) throws Exception; /** *删除用户 */ public void delete(String uid) throws Exception; /** *查询全部用户 */ public List<User> queryAll() throws Exception; } package ssh2.news.dao.impl; import java.util.List; import ssh2.news.dao.UserDao; import ssh2.news.vo.User; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; /** * 继承HibernateDaoSuppor类实现getHibernateTemplate() * * @author bulktree * */ public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void addUser(User user) throws Exception { this.getHibernateTemplate().save(user); } // @Override public void delete(String uid) throws Exception { // TODO Auto-generated method stub } // @Override public List<User> queryAll() throws Exception { // TODO Auto-generated method stub return null; } // @Override public User queryByUidAndPassword(String uid, String password) throws Exception { String hql = "FROM User as u WHERE u.uid=? and u.password=?"; String[] str = new String[] { uid, password }; List<User> users = this.getHibernateTemplate().find(hql, str); if (users != null && users.size() >= 1) { return users.get(0); } else { return null; } }
package ssh2.news.service; public interface UserService { /** *添加一个用户 *@return新增用户的uid */ public String addUser(String uid, String uname, String password) throws Exception; /** *验证登录 */ public String isLogin(String uid, String password) throws Exception; }
package ssh2.news.service.impl; import ssh2.news.dao.UserDao; import ssh2.news.service.UserService; import ssh2.news.vo.User; /** * UserService实现类 * * @author bulktree * */ public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } // @Override public String addUser(String uid, String uname, String password) throws Exception { User user = new User(); user.setUid(uid); user.setUname(uname); user.setPassword(password); userDao.addUser(user); return user.getUid(); } // @Override public String isLogin(String uid, String password) throws Exception { User user = userDao.queryByUidAndPassword(uid, password); if(user != null) { return user.getUname(); } else { return null; } } } CREATE TABLE `user` (
备注:此文转载自http://hi.baidu.com/gaoshan1919/blog/item/d0c595b332e38ea2d8335a05.html |