今天的内容是使用ssh框架实现登陆的功能。spring使用的版本是spring-4.2.5,struts2使用的版本是struts-2.3.24.1,hibernate使用的版本是hibernate-5.1.0。
一、首先建立数据库表(使用的是mysql,表以下图所示)
二、建立Web Project,名为ssh。
三、加载须要的jar包。
spring(初学者,将全部的release的jar都导进去):
hibernate(添加了required文件夹中全部的jar包):html
struts2(其中包括了spring和struts2的支持包):
四、修改web.xml。配置struts和spring。内容以下所示:java
<?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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- struts过滤全部的请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class > </listener> <context-param> <param-name>contextConfigLocation</param-name> <!-- 注意路径的问题 --> <param-value> /WEB-INF/classes/applicationContext.xml </param-value> </context-param> </web-app>
五、在src目录下建立hibernate.cfg.xml,用于链接数据库。mysql
<hibernate-configuration> <session-factory> <!-- 须要操做的数据库的配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/book</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">sll</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 是否在控制台输出操做的sql语句 --> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration>
六、建立User.hbm.xml,我放置的目录是sll/hibernate/model。User.hbm.xml与数据库中的user表对应。web
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- User.hbm.xml的做用简而言之就是对实体和数据库中的表相呼应 --> <hibernate-mapping package="sll.action"> <class name="LoginAction" table="user"> <!-- 属性与字段一一对应 --> <id name="name"></id> <property name="password"></property> </class> </hibernate-mapping>
七、在src目录下新建applicationContext.xml文件。在applicationContext.xml中添加sessionFactory的bean。而且配置好hibernate.cfg.xml和User.hbm.xml的信息。
在这里,添加了id为loginAction的bean,对应的类是sll.action.LoginAction。(这个类的具体内容会在后面给出)spring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> <property name="mappingResources"> <list> <value>sll/hibernate/model/User.hbm.xml</value> </list> </property> </bean> <bean id="loginAction" class="sll.action.LoginAction"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> </beans>
八、在src目录下建立struts.xml。struts配置Action的信息。Action接收来自视图层的请求,并接收请求参数,同时负责调用模型方法来完成业务逻辑的处理,最后控制程序的逻辑,选择一个合适的视图将结果显示给客户。
由于以前在applicationContext.xml中已经定义了id为loginAction的bean,在这里咱们定义一个action,名为login,class为在applicationContext中定义的bean:loginAction。sql
<?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="default" extends="struts-default" > <action name="login" class="loginAction"> <result name="success">/login_success.jsp</result> <result name="error">/login_error.jsp</result> </action> </package> </struts>
九、在src目录下新建struts.properties数据库
struts.objectFactory=spring
十、LoginAction.java(事实上,应该将控制、业务、模型层分开,可是出于简单考虑,主要目的是使用ssh框架,因此将内容全都写在LoginAction.java中)apache
public class LoginAction { private static final long serialVersionUID = 4833662754330237479L; private String name; private String password; private SessionFactory sessionFactory; public String execute(){ Session session = sessionFactory.openSession(); //查询语句from后面接的不是表名称,而是applicationContext.xml中定义的javabean数据对象名。 String hql = "from LoginAction where name=? and password=?"; Query q = session.createQuery(hql); //值得注意的是,页面中的name要与属性一一对应,不然没法获得对应的属性值 q.setParameter(0, name); q.setParameter(1, password); List user = q.list(); session.close(); if (user.size() > 0) { return "success"; } else { return "error"; } } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } 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; } }
十一、login.jspspring-mvc
<html> <body> <form action="login.action" method="post"> 用户登陆<br> 姓名:<input type="text" name="name"/><br> 密码:<input type="text" name="password"/><br> <input type="submit" value="登陆"/> </form> </body> </html>
十二、login_success.jspsession
<html> <body> <h2>您好! 用户<s:property value="username"/>欢迎您登陆成功 </h2> </body> </html>
1三、login_error.jsp
<html> <body> <h2>登陆失败</h2> </body> </html>
1三、部署运行成功