struts2 spring mysql_Struts2+Spring3+Hibernate3+MySQL简单登录实现

使用Struts2+Spring3+Hibernate3+MySQL实现简单登录,下面就开始说小编写的代码。

1.导入相关的jar包

cf87589ea48cdb868ac754f4e95eb59b.png

2.建立数据库

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

3.建立包结构

2c7786ca22653efdb4a72aafb6ee0c8a.png

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

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

1 #jdbc2 jdbc.driver=com.mysql.jdbc.Driver3 jdbc.url=jdbc:mysql://127.0.0.1:3306/test4 jdbc.username=root5 jdbc.password=

4.2 spring配置文件:applicationContext.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

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.xsd9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd11 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd13 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd14 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

15

16

17

18

19

20 class="org.springframework.jdbc.datasource.DriverManagerDataSource">

21

22

23

24

25

26

27

28 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

29

30

31

32 true

33 utf-8

34 org.hibernate.dialect.MySQLDialect

35 true

36 update

37

38

39

40

41

42

43 configs/account.hbm.xml

44

45

46

47

48

51

52

53

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

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

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

1 packagessh.ft.test;2

3 importJava.util.List;4

5 importorg.hibernate.SessionFactory;6 importorg.junit.Test;7 importorg.springframework.context.ApplicationContext;8 importorg.springframework.context.support.ClassPathXmlApplicationContext;9

10 importssh.ft.entity.Account;11

12 public classDBTest {13 @Test14 public voidtest1() {15 String config = "configs/applicationContext.xml";16 ApplicationContext ac = newClassPathXmlApplicationContext(config);17 SessionFactory sf = ac.getBean(SessionFactory.class);18 String sql = "from Account";19 @SuppressWarnings("unchecked")20 List list =sf.openSession().createQuery(sql).list();21 System.out.println(list.size());22 }23 }

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

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

2a99033bdcd8905be5243152b2f358b4.png

4.5 struts配置文件:struts.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4 "http://struts.apache.org/dtds/struts-2.3.dtd">

5

6

7

8

9

10

11

12

13

14

15 /WEB-INF/index.jsp

16 /WEB-INF/login.jsp

17

18

19

20

21 /WEB-INF/login.jsp

22

23

24

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

1 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

4

5

6

7

8 org.hibernate.dialect.MySQLDialect

9

10 true

11 true

12

13 thread

14

15 create

16

17

18

19

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

1 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

4

5

6

7

8

9

10

11

12

13

14

15

在hibernate.cfg.xml中的

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

4.7 web.xml配置文件

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 ssh

4

5

6 org.springframework.web.context.ContextLoaderListener

7

8

9 contextConfigLocation

10 classpath:configs/applicationContext.xml

11

12

13 struts2

14

15 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter16

17

23

24

25 struts2

26 /*

27

28

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

4.8 实体类:Account.java

1 packagessh.ft.entity;2

3 public classAccount {4 privateInteger id;5 privateString user;6 privateString paw;7

8 publicInteger getId() {9 returnid;10 }11

12 public voidsetId(Integer id) {13 this.id =id;14 }15

16 publicString getUser() {17 returnuser;18 }19

20 public voidsetUser(String user) {21 this.user =user;22 }23

24 publicString getPaw() {25 returnpaw;26 }27

28 public voidsetPaw(String paw) {29 this.paw =paw;30 }31

32 }

4.9 dao和daoImpl:AccountDao.java

1 packagessh.ft.dao;2

3 importssh.ft.entity.Account;4

5 public interfaceAccountDao {6 publicAccount findByUser(String user);7 }

AccountDaoImpl.java

1 packagessh.ft.dao.impl;2

3 importjava.util.List;4

5 importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;6

7 importssh.ft.dao.AccountDao;8 importssh.ft.entity.Account;9

10 public class AccountDaoImpl extends HibernateDaoSupport implementsAccountDao {11

12 @Override13 publicAccount findByUser(String user) {14 @SuppressWarnings("unchecked")15 List 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 packagessh.ft.service;2

3 public interfaceAccountManager {4 public booleanlogin(String user, String paw);5 }

AccountManagerImpl.java

1 packagessh.ft.service.impl;2

3 importssh.ft.dao.AccountDao;4 importssh.ft.entity.Account;5 importssh.ft.service.AccountManager;6

7 public class AccountManagerImpl implementsAccountManager {8 privateAccountDao dao;9

10 publicAccountDao getDao() {11 returndao;12 }13

14 public voidsetDao(AccountDao dao) {15 this.dao =dao;16 }17

18 @Override19 public booleanlogin(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 packagessh.ft.action;2

3 importcom.opensymphony.xwork2.ActionSupport;4

5 importssh.ft.entity.Account;6 importssh.ft.service.AccountManager;7

8 public class LoginAction extendsActionSupport {9 private static final long serialVersionUID = 1L;10

11 privateAccount account;12

13 publicAccount getAccount() {14 returnaccount;15 }16

17 public voidsetAccount(Account account) {18 this.account =account;19 }20

21 privateAccountManager accountManager;22

23 publicAccountManager getAccountManager() {24 returnaccountManager;25 }26

27 public voidsetAccountManager(AccountManager accountManager) {28 this.accountManager =accountManager;29 }30

31 privateString msg;32

33 publicString getMsg() {34 returnmsg;35 }36

37 public voidsetMsg(String msg) {38 this.msg =msg;39 }40

41 publicString login() {42 System.out.println("login........");44 if(accountManager.login(account.getUser(), account.getPaw())) {45 returnSUCCESS;46 }47 setMsg("用户或密码错误");48 returnERROR;49 }50 }

5.页面部分

5.1 页面结构

ff46bd7cd971ec6f449fc3c15226a9f8.png

5.2 登录页面:login.jsp

1

2

3

4

5

6

7

Login

8

9

10

11

12

13

14

15

16

17

5.3 登录成功页面:index.jsp

1

2

3

4

5

6

7

Login

8

9

10 欢迎【】登陆!

11

12

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

0de391fda0c77251e74b4f39c0988e5f.png

成功页面:

4d81ec9394b10a9b2ce992682f23a92b.png

整合到此结束。

推荐阅读:

Struts 的详细介绍:请点这里

Struts 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值