ssh mysql简单实例_ssh小例子实现登录

今天的内容是使用ssh框架实现登录的功能。spring使用的版本是spring-4.2.5,struts2使用的版本是struts-2.3.24.1,hibernate使用的版本是hibernate-5.1.0。

1、首先创建数据库表(使用的是mysql,表如下图所示)

b0a7a2e6a1294f377ced63ff5dce30d4.png

2、创建Web Project,名为ssh。

3、加载需要的jar包。

spring(初学者,将所有的release的jar都导进去):

fa39b94da01a5d3e0cc4b9df4720d1c3.png

hibernate(添加了required文件夹中所有的jar包):

72dd58ce0e1cadfc61992005cdbdb9b7.png

struts2(其中包括了spring和struts2的支持包):

07417cfce18e84baeb4dd6edf33e62fc.png

4、修改web.xml。配置struts和spring。内容如下所示:

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">

struts2org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

/WEB-INF/classes/applicationContext.xml

5、在src目录下创建hibernate.cfg.xml,用于连接数据库。

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/book

root

sll

org.hibernate.dialect.MySQLDialect

true

6、创建User.hbm.xml,我放置的目录是sll/hibernate/model。User.hbm.xml与数据库中的user表对应。

/p>

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

7、在src目录下新建applicationContext.xml文件。在applicationContext.xml中添加sessionFactory的bean。并且配置好hibernate.cfg.xml和User.hbm.xml的信息。

在这里,添加了id为loginAction的bean,对应的类是sll.action.LoginAction。(这个类的具体内容会在后面给出)

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">

value="classpath:hibernate.cfg.xml">

sll/hibernate/model/User.hbm.xml

8、在src目录下创建struts.xml。struts配置Action的信息。Action接收来自视图层的请求,并接收请求参数,同时负责调用模型方法来完成业务逻辑的处理,最后控制程序的逻辑,选择一个合适的视图将结果显示给客户。

因为之前在applicationContext.xml中已经定义了id为loginAction的bean,在这里我们定义一个action,名为login,class为在applicationContext中定义的bean:loginAction。

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

/login_success.jsp

/login_error.jsp

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

用户登录

姓名:

密码:

12、login_success.jsp

您好! 用户欢迎您登录成功

13、login_error.jsp

登录失败

13、部署运行成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值