java ssh整合_整合SSH框架实现简单登录

SSH整合的大体结构:我们将Struts2和hibernate交给spring来管理

1ced8454144340cdae8a5ab65f1e3bd8.png

创建好web项目之后,首先当然是先引入需要的pom节点,需要的pom的节点可以在Maven官方仓库中下载https://mvnrepository.com/

当然以下是我自己准备好的一份pom节点,复制进去下载完即可使用

junit

junit

4.11

test

org.hibernate

hibernate-core

3.5.1-Final

javax.transaction

jta

1.1

org.hibernate

hibernate-annotations

3.4.0.GA

org.slf4j

slf4j-nop

1.6.4

org.slf4j

slf4j-api

1.7.25

org.hibernate.javax.persistence

hibernate-jpa-2.0-api

1.0.1.Final

net.sf.ehcache

ehcache

1.2.3

org.hibernate

hibernate-ehcache

3.6.10.Final

org.springframework

spring-context

4.2.0.RELEASE

org.aspectj

aspectjweaver

1.8.7

org.springframework

spring-web

4.2.0.RELEASE

javax.servlet

jstl

1.2

runtime

javax

javaee-api

6.0

provided

com.alibaba

druid

1.1.6

org.springframework

spring-tx

4.2.0.RELEASE

org.springframework

spring-orm

4.2.0.RELEASE

org.springframework

spring-beans

4.2.0.RELEASE

org.apache.struts

struts2-convention-plugin

2.3.24

commons-lang3

org.apache.commons

org.apache.struts

struts2-spring-plugin

2.3.24

javax.servlet

javax.servlet-api

3.1.0

provided

asm

asm-commons

3.3

commons-fileupload

commons-fileupload

1.3.1

mysql

mysql-connector-java

5.1.39

commons-io

commons-io

2.4

org.apache.commons

commons-lang3

3.3.1

commons-logging

commons-logging-api

1.1

org.freemarker

freemarker

2.3.23

javassist

javassist

3.11.0.GA

ognl

ognl

3.0.6

org.apache.struts

struts2-core

2.3.24

org.apache.struts.xwork

xwork-core

2.3.24

src/main/resources

src/main/java

**/*.xml

在src下创建resource目录,并在创建好的目录中创建相应的XML文件

315634917afee25cd7f95b5ad882b1b5.png

applicationContext.xml全局配置文件

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

true

true

update

cn/ssh/pojo/User.hbm.xml

hibernate.cfg.xml(当applicationtext.xml中出现Hibernate相关配置时,就可以不需要此文件。。当然此时的applicationtext.xml中文件是存在Hibernate配置)

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

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

com.mysql.jdbc.Driver

jdbc:mysql:///hibernate

root

0000

true

true

update

thread

至于Struts.xml文件我们等到需要的时候在进行配置

web.xml(需要修改web.xml的头部)

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Archetype Created Web Application

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

OpenSessionInViewFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

OpenSessionInViewFilter

*.action

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

指定项目启动后的首页面,如果不是index.jsp页面需要添加

login.jsp

创建Java包,创建实体类以及相关的xml文件

ca4658c41b84f51115fd43424ec47c21.png

Pojo层

packagecn.ssh.pojo;import javax.persistence.*;public classUser {privateInteger id;privateString name;privateString password;publicInteger getId() {returnid;

}public voidsetId(Integer id) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}

}

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

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

dao层

d1322c2068340339b2b4dbf4de9d1fc3.png

public interfaceUserDao {publicUser findUser(String name,String password);

}

在实现类中我们继承HibernateDaoSupport类,这个类主要给我们提供了一些增删改的方法

packagecn.ssh.dao;importcn.ssh.pojo.User;importorg.hibernate.SessionFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importorg.springframework.stereotype.Repository;importjava.util.List;/***@author: wmj

* @Data: 2018-10-30 14:44

**/

public class UserDaoImpl extends HibernateDaoSupport implementsUserDao {

@OverridepublicUser findUser(String name, String password) {

List users = (List) this.getHibernateTemplate()

.find("from User u WHERE u.name=? AND u.password=?", name, password);if (users.size()>0){return users.get(0);

}else{return null;

}

}

}

service层

2a1e05fa0285616814d46be6c09728fb.png

83eba38458b212e3d10344d34fe40b33.png

531dde930dbded3577c1e4755cafc764.png

action层

UserAction继承ActionSupport

packagecn.ssh.action;importcn.ssh.pojo.User;importcn.ssh.service.UserService;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.stereotype.Controller;importjava.util.Map;/***@author: wmj

* @Data: 2018-10-30 15:25

**/@Controllerpublic class UserAction extendsActionSupport {

@AutowiredprivateUserService userService;privateString name; //名称privateString password; //密码privateString message; //信息提示publicString login(){

User user=userService.findUser(name, password);

ActionContext context=ActionContext.getContext();

//用户名为空if (name==null || name.equals("")){this.setMessage("请输入名称");return "login";

}

//密码为空if (password==null || password.equals("")){this.setMessage("请输入密码");return "login";

}

//将登陆的用户保存在session集合中

Map session=context.getSession();if (null!=user){

session.put("user",user);return "index";

}this.setMessage("登录失败");return "login";

}

//要有getter和setter方法,否则映射不到publicUserService getUserService() {returnuserService;

}public voidsetUserService(UserService userService) {this.userService =userService;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}publicString getMessage() {returnmessage;

}public voidsetMessage(String message) {this.message =message;

}

}

创建相应的JSP页面

创建login.jsp 登陆页面

User: wumengjieDate:2018/10/30

Time:15:32

Tochange this template use File | Settings | File Templates.--%>

Title

用户名:
密码:

这时配置Struts.xml文件

/p>

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

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

/login.jsp

/index.jsp

登录成功页面:index.jsp

成功

配置TomCat

点击Edit Configurations....

8b24f2435042ec7a2ae773c7ec7e6feb.png

点击加号,找到Tomcat Server,配置本地tomcat

cefa4d589b1e9a225ad6b8a5936ebcbf.png

配置相关信息

7683a4a9ada4e046e5d1b7e0420db3f7.png

43ed5f74686bc5fc58967e1147a03e13.png

点击带有exploded的即可,把tomcat集成到项目中

6fb49ef32b2e7dbef32e77e6805711d3.png

也可以点击 Deployment------点击加号-----artifact

9d9a01c7a091bd745b615056006ba7ca.png

5b1622f7aa9e0bda730255358b316456.png

什么都不需要改,点击apply即可

1f085e7ca21291dd451f1617045209d4.png

启动tomcat,测试

b445fa7d3949266a8818d33f5e834619.png

6a9baa135c8083fd6f09abc489a13193.png

当没有输入密码或输入错误密码会进行提示,因为在UserAction中有message字段,对字段进行赋值,在页面获取即可

068261d6fcf685ff031013add00f8b61.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值