使用Struts2+Spring3+Hibernate3+MySQL实现简单登录,下面就开始说小编写的代码。
1.导入相关的jar包
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.建立包结构
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包不全之类的问题,务必正确之后再往下编写,否则到后面错误或更多导致无法查找
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 页面结构
5.2 登录页面:login.jsp
1
2
3
4
5
6
7
Login8
9
10
11
12
13
14
15
16
17
5.3 登录成功页面:index.jsp
1
2
3
4
5
6
7
Login8
9
10 欢迎【】登陆!
11
12
6.测试,在浏览器输入:http://localhost:8080/ssh/tologin
成功页面:
整合到此结束。
推荐阅读:
Struts 的详细介绍:请点这里
Struts 的下载地址:请点这里