1.导入相关的jar包

2.建立数据库

1 create table account(
2 id int(10),
3 user varchar(50),
4 paw varchar(50)
5 );
6 insert into account values(1,'admin','admin');

3.建立包结构

4.配置文件的配置及代码

4.1 数据库配置文件:db.properties

1 #jdbc
2 jdbc.driver=com.mysql.jdbc.Driver
3 jdbc.url=jdbc:mysql://127.0.0.1:3306/test
4 jdbc.username=root
5 jdbc.password=

4.2 spring配置文件:applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
6 xmlns:mvc="http://www.springframework.org/schema/mvc"
7 xsi:schemaLocation="
8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
11 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
13 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
14 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
15 <context:component-scan base-package="ssh.ft"></context:component-scan>
16
17 <context:property-placeholder location="classpath:configs/db.properties" />
18 <!-- datasource -->
19 <bean id="dataSource"
20 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
21 <property name="driverClassName" value="${jdbc.driver}" />
22 <property name="url" value="${jdbc.url}" />
23 <property name="username" value="${jdbc.username}" />
24 <property name="password" value="${jdbc.password}" />
25 </bean>
26 <!-- spring与hibernate整合 spring来管理session的创建、打开和关闭 -->
27 <bean id="sessionFactory"
28 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
29 <!-- 通过配置文件的方式获取数据源,出现异常,未解决 -->
30 <property name="hibernateProperties">
31 <props>
32 <prop key="connection.useUnicode">true</prop>
33 <prop key="connection.characterEncoding">utf-8</prop>
34 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
35 <prop key="hibernate.show_sql">true</prop>
36 <prop key="hibernate.hbm2ddl.auto">update</prop>
37 </props>
38 </property>
39 <property name="dataSource" ref="dataSource" />
40 <property name="mappingResources">
41 <list>
42 <!-- 以下用来列出所有的PO映射文件 -->
43 <value>configs/account.hbm.xml</value>
44 </list>
45 </property>
46 </bean>
47 <!-- 定义事物管理器,并位事物管理器配置上述所定义的session -->
48 <!-- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
49 <property name="sessionFactory"> <ref bean="sessionFactory" /> </property>
50 </bean> <tx:annotation-driven transaction-manager="transactionManager"/> -->
51
52 <!-- 对事物管理器进行设置 表示对save、del、update开头的方法应用事物 -->
53 <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
54 <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method
55 name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED"
56 /> </tx:attributes> </tx:advice> -->
57
58 <!-- 注入dao -->
59 <bean class="ssh.ft.dao.impl.AccountDaoImpl" id="accountDao">
60 <property name="sessionFactory" ref="sessionFactory"></property>
61 </bean>
62
63 <!--注入service -->
64 <bean class="ssh.ft.service.impl.AccountManagerImpl" id="accountManager">
65 <property name="dao" ref="accountDao"></property>
66 </bean>
67
68 <!--写action和业务逻辑层依赖注入 -->
69 <!--将scope设置成prototype,预防了线程安全问题 -->
70 <bean class="ssh.ft.action.LoginAction" id="loginAction" scope="prototype">
71 <property name="accountManager" ref="accountManager"></property>
72 </bean>
73 </beans>

注:上述配置中注释掉的事物部分,因为小编未使用到,所以也没有认证,在配置时可去掉

4.4 配置完spring,可以先测试下配置是否正确

 1 package ssh.ft.test;
2
3 import java.util.List;
4
5 import org.hibernate.SessionFactory;
6 import org.junit.Test;
7 import org.springframework.context.ApplicationContext;
8 import org.springframework.context.support.ClassPathXmlApplicationContext;
9
10 import ssh.ft.entity.Account;
11
12 public class DBTest {
13 @Test
14 public void test1() {
15 String config = "configs/applicationContext.xml";
16 ApplicationContext ac = new ClassPathXmlApplicationContext(config);
17 SessionFactory sf = ac.getBean(SessionFactory.class);
18 String sql = "from Account";
19 @SuppressWarnings("unchecked")
20 List<Account> list = sf.openSession().createQuery(sql).list();
21 System.out.println(list.size());
22 }
23 }

若配置正确则有如下结果:红色框中的数字1表示数据库中表account中有一条数据,若未出现正确结果,则需要检查上述代码哪里出错,或者是jar的问题

因为小编也遇到过很多jar包不全之类的问题,务必正确之后再往下编写,否则到后面错误或更多导致无法查找

4.5 struts配置文件:struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5 <struts>
6 <!-- 开发模式 -->
7 <constant name="struts.devMode" value="true" />
8 <!-- 将Action的创建交给spring来管理 -->
9 <constant name="struts.objectFactory" value="spring" />
10
11 <!-- 包含的配置文件 <include file="/configs/struts-user.xml"></include> -->
12 <package name="s2sh" extends="s2sh1">
13 <!-- Action的配置在这里 -->
14 <action name="login" class="loginAction" method="login">
15 <result name="success">/WEB-INF/index.jsp</result>
16 <result name="error">/WEB-INF/login.jsp</result>
17 </action>
18 </package>
19 <package name="s2sh1" extends="struts-default">
20 <action name="tologin" >
21 <result >/WEB-INF/login.jsp</result>
22 </action>
23 </package>
24 </struts>

4.6 hibernate配置文件:hibernate.cfg.xml

 1 <!DOCTYPE hibernate-configuration PUBLIC
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
4 <hibernate-configuration>
5 <session-factory>
6
7 <!-- 配置数据库方言 -->
8 <property name="dialect"> org.hibernate.dialect.MySQLDialect</property>
9 <!-- 配置打印语句 -->
10 <property name="show_sql">true</property>
11 <property name="format_sql">true</property>
12 <!-- 配置线程安全的session -->
13 <property name="hibernate.current_session_context_class">thread</property>
14 <!--如果表不存在,则会帮你新建 -->
15 <property name="hbm2ddl.auto">create</property>
16 <!-- 映射文件 -->
17 <mapping resource="configs/account.hbm.xml" />
18 </session-factory>
19 </hibernate-configuration>

hibernate配置文件还有一个实体映射文件:account.hbm.xml

 1 <!DOCTYPE hibernate-mapping PUBLIC
2 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
4 <hibernate-mapping>
5 <class name="ssh.ft.entity.Account" table="account">
6 <!-- 主键 -->
7 <id name="id">
8 <!-- 固定值:主键生成策略 -->
9 <generator class="native"></generator>
10 </id>
11 <!-- 普通属性 -->
12 <property name="user"></property>
13 <property name="paw"></property>
14 </class>
15 </hibernate-mapping>

在hibernate.cfg.xml中的

  <mapping resource="configs/account.hbm.xml" />

这个语句里面的account.hbm.xml就是指上述的account.hbm.xml配置文件

4.7 web.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
3 <display-name>ssh</display-name>
4 <!-- spring -->
5 <listener>
6 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7 </listener>
8 <context-param>
9 <param-name>contextConfigLocation</param-name>
10 <param-value>classpath:configs/applicationContext.xml</param-value>
11 </context-param>
12 <filter>
13 <filter-name>struts2</filter-name>
14 <filter-class>
15 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
16 </filter-class>
17 <!-- 将struts.xml配置文件放置在src的其他位置无法访问,未解决
18 <init-param>
19 <param-name>filterConfig</param-name>
20 <param-value>classpath:configs/struts.xml</param-value>
21 </init-param>
22 -->
23 </filter>
24 <filter-mapping>
25 <filter-name>struts2</filter-name>
26 <url-pattern>/*</url-pattern>
27 </filter-mapping>
28 </web-app>

注:上述小编注释了一个未解决的问题,就是struts.xml文件一定要在src目录下方,否则会报错,如果想放在其他地方的话,若有人解决希望一定要留言告诉小编

4.8 实体类:Account.java

 1 package ssh.ft.entity;
2
3 public class Account {
4 private Integer id;
5 private String user;
6 private String paw;
7
8 public Integer getId() {
9 return id;
10 }
11
12 public void setId(Integer id) {
13 this.id = id;
14 }
15
16 public String getUser() {
17 return user;
18 }
19
20 public void setUser(String user) {
21 this.user = user;
22 }
23
24 public String getPaw() {
25 return paw;
26 }
27
28 public void setPaw(String paw) {
29 this.paw = paw;
30 }
31
32 }

4.9 dao和daoImpl:AccountDao.java

1 package ssh.ft.dao;
2
3 import ssh.ft.entity.Account;
4
5 public interface AccountDao {
6 public Account findByUser(String user);
7 }

AccountDaoImpl.java

 1 package ssh.ft.dao.impl;
2
3 import java.util.List;
4
5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
6
7 import ssh.ft.dao.AccountDao;
8 import ssh.ft.entity.Account;
9
10 public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao {
11
12 @Override
13 public Account findByUser(String user) {
14 @SuppressWarnings("unchecked")
15 List<Account> list = this.getHibernateTemplate().find("from Account a where a.user=?", user);
16 if (list == null || list.isEmpty()) {
17 return null;
18 }
19 return list.get(0);
20 }
21 }

4.10 service和serviceImpl:AccountManager.java

1 package ssh.ft.service;
2
3 public interface AccountManager {
4 public boolean login(String user, String paw);
5 }

AccountManagerImpl.java

 1 package ssh.ft.service.impl;
2
3 import ssh.ft.dao.AccountDao;
4 import ssh.ft.entity.Account;
5 import ssh.ft.service.AccountManager;
6
7 public class AccountManagerImpl implements AccountManager {
8 private AccountDao dao;
9
10 public AccountDao getDao() {
11 return dao;
12 }
13
14 public void setDao(AccountDao dao) {
15 this.dao = dao;
16 }
17
18 @Override
19 public boolean login(String user, String paw) {
20 Account account = dao.findByUser(user);
21 if (account == null) {
22 return false;
23 }
24 if (account.getPaw().equals(paw)) {
25 return true;
26 }
27 return false;
28 }
29
30 }

4.11 account处理类:LoginAction.java

 1 package ssh.ft.action;
2
3 import com.opensymphony.xwork2.ActionSupport;
4
5 import ssh.ft.entity.Account;
6 import ssh.ft.service.AccountManager;
7
8 public class LoginAction extends ActionSupport {
9 private static final long serialVersionUID = 1L;
10
11 private Account account;
12
13 public Account getAccount() {
14 return account;
15 }
16
17 public void setAccount(Account account) {
18 this.account = account;
19 }
20
21 private AccountManager accountManager;
22
23 public AccountManager getAccountManager() {
24 return accountManager;
25 }
26
27 public void setAccountManager(AccountManager accountManager) {
28 this.accountManager = accountManager;
29 }
30
31 private String msg;
32
33 public String getMsg() {
34 return msg;
35 }
36
37 public void setMsg(String msg) {
38 this.msg = msg;
39 }
40
41 public String login() {
42 System.out.println("login........");44 if (accountManager.login(account.getUser(), account.getPaw())) {
45 return SUCCESS;
46 }
47 setMsg("用户或密码错误");
48 return ERROR;
49 }
50 }

5.页面部分

5.1 页面结构

5.2 登录页面:login.jsp

 1 <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 <%@ taglib uri="/struts-tags" prefix="s"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>Login</title>
8 </head>
9 <body>
10 <s:form action="login.action">
11 <s:textfield name="account.user" label="用户" />
12 <s:password name="account.paw" label="密码" />
13 <s:submit value="提交" />
14 </s:form>
15 <s:property value="msg"/>
16 </body>
17 </html>

5.3 登录成功页面:index.jsp

 1 <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2 <%@ taglib uri="/struts-tags" prefix="s"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>Login</title>
8 </head>
9 <body>
10 <span style="font-family: KaiTi_GB2312; font-size: 18px;">欢迎【<s:property value="account.user" />】登陆!</span>
11 </body>
12 </html>

6.测试,在浏览器输入:http://localhost:8080/ssh/tologin

成功页面:

struts2+spring3+hibernate3+mysql简单登录实现的更多相关文章

  1. Struts2整合Hibernate3实现用户登录功能

    所用技术:struts2 ,hibernate,jsp,mysql 本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰 ...

  2. SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)

    这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hiberna ...

  3. java web: eclipse & maven & jetty & struts2 & mysql = 简单登录页面

    第一次接触java web开发,花费了一天半的时间,写了个简单的登录页面,以此文为记. 开发工具 Eclipse Luna Release (4.4.0) 已集成maven,maven目前的体会就是管 ...

  4. Struts2 Spring3 Hibernate3 集成xml版本

    Struts2 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互 ...

  5. Struts2+Spring3+Hibernate3+Maven构建(基于Eclipse)

    长时间不做后台了,整理一下资料,以便翻阅. Eclipse.JDK安装略…… Maven下载地址:http://maven.apache.org/download.cgi 版本比较新的Eclipse基 ...

  6. Eclipse 搭建struts2 spring3 hibernate3环境实战 待完善

    1.struts2 目前是2.3版本,下载地址http://struts.apache.org/download.cgi struts2包 struts2-core-2.3.16.3.jar stru ...

  7. 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.3

    作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...

  8. 【Struts2+Spring3+Hibernate3】SSH框架整合实现CRUD_1.2

    作者: hzboy192@192.com Blog: http://my.csdn.net/peng_hao1988 版本总览:http://blog.csdn.net/peng_hao1988/ar ...

  9. ExtJs、Struts2、Hibernate3.2登录页面的简单实现

    1.思想的大致模型 2.建立数据库test和数据库表tb_user 1 CREATEDATABASE `test`; 2  CREATETABLE `test`.`tb_user` ( 3 `user ...

随机推荐

  1. 支持向量机SVM(一)

    [转载请注明出处]http://www.cnblogs.com/jerrylead 1 简介 支持向量机基本上是最好的有监督学习算法了.最开始接触SVM是去年暑假的时候,老师要求交<统计学习理论 ...

  2. mui框架移动开发初体验

      前  言 博主最近在接触移动APP,学习了几个小技巧,和大家分享一下. 1. 状态栏设置 现在打开绝大多数APP,状态栏都是与APP一体,不仅美观,而且与整体协调.博主是个中度强迫症患者,顶部那个 ...

  3. PIC24 通过USB在线升级 -- USB HID bootloader

    了解bootloader的实现,请加QQ: 1273623966 (验证填bootloader):欢迎咨询或定制bootloader; 我的博客主页www.cnblogs.com/geekygeek ...

  4. java 学习笔记——类之间的关系之封装、继承与多态的详解

    封装 一个封装的简单例子 封装就是把对象的属性(状态)和方法(行为)结合在一起,并尽可能隐蔽对象的内部细节,成为一个不可分割的独立单位(即对象),对外形成一个边界,只保留有限的对外接口使之与外部发生联 ...

  5. C# readonly

    当某个字段是引用类型,并且该字段被标记为readonly时,不可改变的是引用,而非字段引用的对象,例如:

  6. 【转】 Python subprocess模块学习总结

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...

  7. MSSQL查询数据分页

    这几天刚好碰到数据的分页查询,觉得不错,Mark一下,方法有两种,都是使用select top,效率如何就不在这讨论 方法1:利用select top配合not in(或者not exists),查询 ...

  8. 起名字好难啊!(初识Django)

    这次我们将实现一个简单的登录注册功能,并吧相应的数据写入数据库: 做这件事之前我已经在数据库中新建了两张表(当然一张表也可以用,先注册后登录嘛···)    两张结构很简单的数据表:↓ 接下来就该干正 ...

  9. vim搭建笔记

    在接触vim近一年后,自己的vimrc都是拼凑别人的,所以有很多插件和配置并不会使用 现在,我决定,花费一天时间,一步一步的搭建自己的vim配置! 去该网址下载安装vim http://www.vim ...

  10. MySQL数据库分区的概念与2大好处(1)

    我们大家都知道通过MySQL数据库分区(Partition)可以提升MySQL数据库的性能,那么到底什么是MySQL数据库分区呢?以及其实际应用的好处的表现有哪些呢?以下的文章就是对这些内容的描述. ...