今天的内容是使用ssh框架实现登录的功能。spring使用的版本是spring-4.2.5,struts2使用的版本是struts-2.3.24.1,hibernate使用的版本是hibernate-5.1.0。
1、首先创建数据库表(使用的是mysql,表如下图所示)
2、创建Web Project,名为ssh。
3、加载需要的jar包。
spring(初学者,将所有的release的jar都导进去):
hibernate(添加了required文件夹中所有的jar包):
struts2(其中包括了spring和struts2的支持包):
4、修改web.xml。配置struts和spring。内容如下所示:
<?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>
5、在src目录下创建hibernate.cfg.xml,用于连接数据库。
<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>
6、创建User.hbm.xml,我放置的目录是sll/hibernate/model。User.hbm.xml与数据库中的user表对应。
<?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>
7、在src目录下新建applicationContext.xml文件。在applicationContext.xml中添加sessionFactory的bean。并且配置好hibernate.cfg.xml和User.hbm.xml的信息。
在这里,添加了id为loginAction的bean,对应的类是sll.action.LoginAction。(这个类的具体内容会在后面给出)
<?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>
8、在src目录下创建struts.xml。struts配置Action的信息。Action接收来自视图层的请求,并接收请求参数,同时负责调用模型方法来完成业务逻辑的处理,最后控制程序的逻辑,选择一个合适的视图将结果显示给客户。
因为之前在applicationContext.xml中已经定义了id为loginAction的bean,在这里我们定义一个action,名为login,class为在applicationContext中定义的bean:loginAction。
<?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>
9、在src目录下新建struts.properties
struts.objectFactory=spring
10、LoginAction.java(事实上,应该将控制、业务、模型层分开,但是出于简单考虑,主要目的是使用ssh框架,所以将内容全都写在LoginAction.java中)
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;
}
}
11、login.jsp
<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>
12、login_success.jsp
<html>
<body>
<h2>您好! 用户<s:property value="username"/>欢迎您登录成功 </h2>
</body>
</html>
13、login_error.jsp
<html>
<body>
<h2>登录失败</h2>
</body>
</html>
13、部署运行成功