项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727
项目源代码下载地址:http://download.csdn.net/detail/adam_zs/7262749
今天花时间把ssh整合了一下,又一次再学习一下,希望对大家有所帮助!
我用的是mysql数据库,建表语句比較简单就不贴出来了,建表的时候记的设置id为自己主动添加?哦。
项目文件位置,项目引用jar包
项目配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 使用ContextLoaderListener初始化Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 定义Struts2的FilterDispathcer的Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- FilterDispatcher用来初始化Struts2而且处理全部的WEB请求。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置了系列常量 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="wangzs" extends="struts-default"> <action name="login" class="loginAction"> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action> <!-- 让用户直接訪问该应用时列出全部视图页面 --> <action name=""> <result>.</result> </action> </package> </struts>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- 定义数据源Bean,使用C3P0数据源实现 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost/test" /> <property name="user" value="root" /> <property name="password" value="wzs_626750095" /> <property name="maxPoolSize" value="40" /> <property name="minPoolSize" value="1" /> <property name="initialPoolSize" value="1" /> <property name="maxIdleTime" value="20" /> </bean> <!-- 定义Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mappingResouces属性用来列出所有映射文件 --> <property name="mappingResources"> <list> <value>com/wzs/bean/Person.hbm.xml</value> </list> </property> <!-- 定义Hibernate的SessionFactory的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="loginAction" class="com.wzs.action.LoginAction" scope="prototype"> <property name="ms" ref="myService" /> </bean> <bean id="myService" class="com.wzs.service.impl.MyServiceImpl"> <property name="personDao" ref="personDao" /> </bean> <bean id="personDao" class="com.wzs.dao.impl.PersonDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
java代码
Person.java
package com.wzs.bean; public class Person { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="lee"> <class name="com.wzs.bean.Person" table="Person"> <!-- 映射标识属性 --> <id name="id" type="int" column="id"> <generator class="identity" /> </id> <!-- 映射普通属性 --> <property name="name" type="string" column="name" /> <property name="password" type="string" column="password" /> </class> </hibernate-mapping>
LoginAction.java
package com.wzs.action; import com.opensymphony.xwork2.ActionSupport; import com.wzs.service.MyService; @SuppressWarnings("serial") public class LoginAction extends ActionSupport { // 以下是用于封装用户请求參数的两个属性 private String name; private String password; // 用于封装处理结果的属性 private String tip; // 系统所用的业务逻辑组件 private MyService ms; // 设置注入业务逻辑组件所必需的setter方法 public void setMs(MyService ms) { this.ms = ms; } /** * 用户登录 * * @return * @throws Exception */ public String login() throws Exception { // 调用业务逻辑组件的valid方法来 // 验证用户输入的username与password是否正确 if (ms.valid(getName(), getPassword())) { setTip("哈哈,整合成功!"); return SUCCESS; } else { return ERROR; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } public MyService getMs() { return ms; } }
MyService.java
package com.wzs.service; public interface MyService { /** * 校验username密码 * * @param name * username * @param password * 密码 * @return true:存在,false:不存在 */ boolean valid(String name, String password); }
MyServiceImpl.java
package com.wzs.service.impl; import com.wzs.dao.PersonDao; import com.wzs.service.MyService; public class MyServiceImpl implements MyService { private PersonDao personDao; /** * 校验username密码 * * @param name * username * @param password * 密码 * @return true:存在,false:不存在 */ public boolean valid(String name, String password) { return personDao.valid(name, password); } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } }
PersonDao.java
package com.wzs.dao; import java.util.List; import com.wzs.bean.Person; public interface PersonDao { /** * 校验username密码 * * @param name * username * @param password * 密码 * @return true:存在,false:不存在 */ public boolean valid(String name, String password); public Person get(Integer id); /** * 保存Person实例 * * @param person * 须要保存的Person实例 * @return 刚刚保存的Person实例的标识属性值 */ public Integer save(Person person); /** * 改动Person实例 * * @param person * 须要改动的Person实例 */ public void update(Person person); /** * 删除Person实例 * * @param id * 须要删除的Person实例的标识属性值 */ public void delete(Integer id); /** * 删除Person实例 * * @param person * 须要删除的Person实例 */ public void delete(Person person); /** * 依据username查找Person * * @param name * 查询的人名 * @return 指定username相应的所有Person */ public List<Person> findByName(String name); /** * 查询所有Person实例 * * @return 所有Person实例 */ @SuppressWarnings("unchecked") public List findAllPerson(); /** * 查询数据表中Person实例的总数 * * @return 数据表中Person实例的总数 */ public long getPersonNumber(); }
PersonDaoImpl.java
package com.wzs.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateTemplate; import com.wzs.bean.Person; import com.wzs.dao.PersonDao; public class PersonDaoImpl implements PersonDao { private HibernateTemplate ht = null; private SessionFactory sessionFactory; // 依赖注入SessionFactory的setter方法 public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } // 初始化HibernateTemplate的方法 private HibernateTemplate getHibernateTemplate() { if (ht == null) { ht = new HibernateTemplate(sessionFactory); } return ht; } /** * 校验username密码 * * @param name * username * @param password * 密码 * @return true:存在,false:不存在 */ @SuppressWarnings("unchecked") public boolean valid(String name, String password) { List<Person> list = getHibernateTemplate().find("from Person p where p.name=? and p.password=?", new String[] { name, password }); if (list.size() > 0) { return true; } return false; } /** * 载入Person实例 * * @param id * 须要载入的Person实例的标识属性值 * @return 指定id相应的Person实例 */ public Person get(Integer id) { return (Person) getHibernateTemplate().get(Person.class, id); } /** * 保存Person实例 * * @param person * 须要保存的Person实例 * @return 刚刚保存的Person实例的标识属性值 */ public Integer save(Person person) { return (Integer) getHibernateTemplate().save(person); } /** * 改动Person实例 * * @param person * 须要改动的Person实例 */ public void update(Person person) { getHibernateTemplate().update(person); } /** * 删除Person实例 * * @param id * 须要删除的Person实例的标识属性值 */ public void delete(Integer id) { getHibernateTemplate().delete(get(id)); } /** * 删除Person实例 * * @param person * 须要删除的Person实例 */ public void delete(Person person) { getHibernateTemplate().delete(person); } /** * 依据username查找Person * * @param name * 查询的人名 * @return 指定username相应的所有Person */ @SuppressWarnings("unchecked") public List<Person> findByName(String name) { return (List<Person>) getHibernateTemplate().find("from Person p where p.name like ?", name); } /** * 查询所有Person实例 * * @return 所有Person实例 */ @SuppressWarnings("unchecked") public List findAllPerson() { return (List<Person>) getHibernateTemplate().find("from Person"); } /** * 查询数据表中Person实例的总数 * * @return 数据表中Person实例的总数 */ public long getPersonNumber() { return (Long) getHibernateTemplate().find("select count(*) from Person as p").get(0); } }
jsp界面
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <title>登录页面</title> </head> <body> <form action="login!login" method="post"> <table align="center"> <caption> <h3> 用户登录 </h3> </caption> <tr> <td> username: <input type="text" name="name" /> </td> </tr> <tr> <td> 密 码: <input type="text" name="password" /> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="登录" /> <input type="reset" value="重填" /> </td> </tr> </table> </form> </body> </html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>成功页面</title> </head> <body> 您已经登录! <s:property value="tip" /> </body> </html>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <title>错误页面</title> </head> <body> 您不能登录! </body> </html>