当前位置: 移动技术网 > IT编程>开发语言>Java > MyEclipse整合ssh三大框架环境搭载用户注册源码下载

MyEclipse整合ssh三大框架环境搭载用户注册源码下载

2019年07月22日  | 移动技术网IT编程  | 我要评论

张可颐老公,李敖视频,王武龙

前言

ssh不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种web应用程序开源集成框架,用于构建灵活、易于扩展的多层web应用程序。

集成ssh框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层(实体层)。

struts作为系统的整体基础架构,负责mvc的分离,在struts框架的模型部分,控制业务跳转,利用hibernate框架对持久层提供支持。spring一方面作为一个轻量级的ioc容器,负责查找、定位、创建和管理对象及对象之间的依赖关系,另一方面能使struts和hibernate更好地工作。

使用myeclipse整合ssh三大框架,并实现一个模拟用户注册的demo,对应版本:

struts版本:2.1;

spring版本:3.1;

hibernate版本:3.3;

一、整合前准备工作

1.建立一个web项目,如下:

注意:支持action的包名必须是“action”,且action类必须是以action结尾,即形如xxxaction这种形式,如上图中所示

2.创建数据库以及表:

create database sshdemo; 
create table t_user( 
id int primary key, 
username varchar(10), 
password varchar(20) 
) 

3.导入数据库连接池c3p0jar包,点击可下载:


二、struts框架的配置:

1.选中项目,右键选择:myeclipse -> project facets[capabilities] -> install apache struts (2.x) facet,如下:

2.选择版本,在这里我选择的是2.1,点击"finish",如下:

3.完成上述步骤以后,会发现在src目录下多出一个struts.xml 文件,内容如下:

<?xml version="1.0" encoding="utf-8" ?> 
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd"> 
<struts> 
 
</struts> 

4.在web-inf目录下的web.xml文件中多一段关于struts过滤器的配置代码,如下:

5.参考上图,将*.action修改为"/*",至此struts框架配置完毕;

三、spring框架的配置:
1.参考struts的配置,选中项目,右键选择:myeclipse -> project facets[capabilities] -> install spring facet,选择版本,在此选择3.1如下:

2.点击"finish",会发现src目录下多了一个applicationcontext.xml文件,web-inf目录下多了一个spring-form.tld与spring.tld文件,并且在web.xml文件中多了一段与spring配置有关的代码,spring框架搭建基本完毕(引入命名空间会在后面讲到),如下所示:

四、hibernate框架的配置:

1.参考struts的配置,选中项目,右键选择:myeclipse -> project facets[capabilities] -> install hibernatefacet,选择版本,在此选择3.3如下:

2.点击"finish",会发现src目录下多了一个缺省包(可以删除),并且在web.xml文件中多了一段代码(后面会重新配置),如下所示:

3.支持“@entity”注解的jar包导入:选中项目,右键选择:myeclipse -> project facets[capabilities] ->manage...,然后照下图中的步骤操作:

完成上述步骤,三大框架基本就搭建起来了,接下来整合它们。

五、整合

1.为了不让applicationcontext.xml看起来太臃肿,以及便于管理,我们将hibernate有关的配置保存在另外一个.xml文件中,然后再在applicationcontext.xml导入,其具体步骤: 

(1)在src目录下(与applicationcontext.xml同级)创建一个名为hibernatecontext.xml的文件,复制applicationcontext.xml里面的内容,然后再做修改; 

(2)hibernatecontext.xml文件里面的内容:

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
 
 
 <!-- sessionfactory 配置 --> 
 <bean id="sessionfactory" 
 class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> 
 <!-- datasource的属性会在applicationcontext.xml文件中配置,在这里先引用 --> 
 <property name="datasource" ref="datasource"></property> 
 <!-- 设置hibernate相关的配置项 --> 
 <property name="hibernateproperties"> 
  <!-- props标签是为了注入properties这个类型的属性 --> 
  <!-- key必须加上hibernate.前缀 --> 
  <props> 
  <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop> 
  <!-- show_sql目的是打印sql语句 --> 
  <prop key="hibernate.show_sql">true</prop> 
  <!-- 美化sql的打印格式 --> 
  <prop key="hibernate.format_sql">true</prop> 
  <!-- a) create-drop:在执行程序的时候创建数据表,在执行完了之后删除表,实际开发中,常用于测试 
   b) create:在每次执行程序的时候重新创建数据表 
   c) update:在执行程序的时候会判断,如果存在,不创建表,否则创建数据表,并且会根据实体类中的属性的增加,而自动增加数据表中的字段(开发环境) 
   d) validate:在执行程序的时候会判断,如果实体类中的属性与表中的字段不一致,那么就报错(生产环境) --> 
  <prop key="hibernate.hbm2ddl.auto">validate</prop> 
  </props> 
 </property> 
 
 <!-- 配置hibernate的实体类 --> 
 <property name="packagestoscan"> 
  <!--list标签是用来注入string[]类型的属性 ,其值一般是对应的bean包的全限名,而bean包中的类一般又是与数据库中的表对应--> 
  <list> 
  <value>com.beauxie.bean</value> 
  </list> 
 </property> 
 </bean> 
 
 <!-- 配置 hibernatetemplate模板 --> 
 <bean id="hibernatetemplate" class="org.springframework.orm.hibernate3.hibernatetemplate"> 
 <property name="sessionfactory" ref="sessionfactory"></property> 
 </bean> 
 
</beans>

 (3)在applicationcontext.xm删除“sessionfactory”的配置(因为在hibernatecontext.xml中已经配置好了),然后导入已经修改好的hibernatecontext.xml内容,导入完以后,此时applicationcontext.xml内容如下:

<?xml version="1.0" encoding="utf-8"?> 
<beans 
 xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
 
 
 <bean id="datasource" 
 class="org.apache.commons.dbcp.basicdatasource"> 
 </bean> 
 
 <!-- 导入其他的spring配置文件 ,如果都放在一个文件里,会看起来比较臃肿--> 
 <import resource="hibernatecontext.xml"/> 
 </beans> 

2.在applicationcontext.xm文件中原先datasource的基础上,修改其配置(数据库名、用户名、密码等),(注意:value标签中一定不能含有空格、回车!!),如下所示:

<bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"> 
  <property name="jdbcurl"> 
  <!--如果直接用value属性,而不用value标签,则需要将“&”转义(&) ,用value标签,<span style="color:#ff0000;">标签中一定不能含有空格、回车,因为它会将空格转换成" "</span>,导致数据库会连接不上,除非重写数据源 --> 
  <value><![cdata[jdbc:mysql://localhost:3306/sshdemo?useunicode=true&characterencoding=utf8&useserverprepstmts=true&prepstmtcachesqllimit=256&cacheprepstmts=true&prepstmtcachesize=256&rewritebatchedstatements=true]]></value> 
 </property> 
 <property name="driverclass" value="com.mysql.jdbc.driver"></property> 
 <property name="user" value="root"></property> 
 <property name="password" value="root"></property> 
  
 <property name="acquireincrement" value="3"></property> 
 <property name="initialpoolsize" value="10"></property> 
 <property name="minpoolsize" value="2"></property> 
 <property name="maxpoolsize" value="10"></property> 
 </bean> 

3.在applicationcontext.xm中,配置spring的扫描器,这样给我们的类加上spring组件注解,就可以实现bean的自动载入,具体步骤如下:(1)引入context命名空间,支持context标签,点击底部的"namespaces",然后勾选context那一项即可:


(2)配置spring扫描器:

<!-- 配置spring的扫描器,然后给我们的类加上spring组件注解,就可以实现bean的自动载入--> 
<context:component-scan base-package="com.beauxie.action,com.beauxie.service,com.beauxie.dao"> 
</context:component-scan> 

至此ssh三大框架环境搭建完毕,接下来是在ssh框架基础上实现用户注册

六、案例:简单的模仿用户注册

1.前台注册页面代码,index.jsp:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
 string path = request.getcontextpath(); 
 string basepath = request.getscheme() + "://" 
  + request.getservername() + ":" + request.getserverport() 
  + path + "/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
<head> 
<base href="<%=basepath%>"> 
 
<title>欢迎注册</title> 
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="this is my page"> 
<!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
</head> 
 
<body> 
 <form action="${pagecontext.request.contextpath }/user/regist" method="post"> 
 <!-- 也可以使用user.username自动装入user属性,但在这里不是重点,所以就在后台手动获取其值--> 
 用户名:<input type="text" name="username"><br> 密 
  码:<input type="password" name="password"><br> 
 <input type="submit" value="注册"> 
 </form> 
</body> 
</html> 

2.user类代码:

package com.beauxie.bean; 
 
import javax.persistence.entity; 
import javax.persistence.id; 
import javax.persistence.table; 
 
/** 
 * @author beauxie 
 * 在这里user的属性应当与t_user表中的字段相同, 
 * 否则就需要手动为不相同的属性指定对应表中的字段 
 */ 
@entity//映射数据库表 
@table(name="t_user")//不加这个注解,默认对应的是user表 
public class user { 
 
 @id//对应t_user表中的主键 
 private int id;//用户id 
 
 private string username;//用户名 
 
 private string password;//密码 
 
 
 public int getid() { 
 return id; 
 } 
 
 public void setid(int id) { 
 this.id = id; 
 } 
 
 public string getusername() { 
 return username; 
 } 
 
 public void setusername(string username) { 
 this.username = username; 
 } 
 
 public string getpassword() { 
 return password; 
 } 
 
 public void setpassword(string password) { 
 this.password = password; 
 } 
 
} 

3.userdao类代码:

package com.beauxie.dao; 
 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.orm.hibernate3.hibernatetemplate; 
import org.springframework.stereotype.repository; 
 
import com.beauxie.bean.user; 
 
/** 
 * @author beauxie 
 * dao层,对数据库进行操作 
 */ 
@repository//这个属性对应的是持久层(一般为dao层),说明交给spring管理,而对应的包下的类名也会有一个"s" 
public class userdao { 
 
 @autowired//自动注入,不需要设值,因为在spring配置文件中已经配置过 
 private hibernatetemplate template; 
 
 
 /** 
 * 用户注册,即向表中添加一条新的记录 
 * @param user 
 */ 
 public void adduser(user user){ 
 //往数据库中添加一条数据,一句话就可以搞定 
 template.save(user); 
 } 
 
} 

4.userservice类代码:

package com.beauxie.service; 
 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.stereotype.service; 
 
import com.beauxie.bean.user; 
import com.beauxie.dao.userdao; 
 
/** 
 * @author beauxie 
 * service层 
 */ 
 
@service//这个属性对应的是业务层一般为service层),说明交给spring管理,而对应的包下的类名也会有一个"s" 
public class userservice { 
 
 @autowired//同样是自动注入 
 private userdao userdao; 
 
 public void adduser(user user){ 
 //调用dao层的adduser方法 
 userdao.adduser(user); 
 } 
} 

5.useraction类代码:

package com.beauxie.action; 
 
import javax.servlet.http.httpservletrequest; 
 
import org.apache.struts2.servletactioncontext; 
import org.apache.struts2.convention.annotation.action; 
import org.apache.struts2.convention.annotation.namespace; 
import org.apache.struts2.convention.annotation.result; 
import org.apache.struts2.convention.annotation.results; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.context.annotation.scope; 
import org.springframework.stereotype.controller; 
 
import com.beauxie.bean.user; 
import com.beauxie.service.userservice; 
 
/** 
 * @author beauxie 
 * 
 */ 
@controller//用于标注控制层组件 
@namespace("/user")//url前缀 
@scope("prototype")//action默认是单例,但实际开发中,一般是多例,因为一般一个action可能会对应多个不同的请求 
//@parentpackage("struts-default")//继承特定的package,默认是“struts-default”,因此可以省略不写 
@results({ 
 @result(name="registsuccess",location="/msg.jsp") 
}) 
public class useraction { 
 
 @autowired//自动注入 
 private userservice service ; 
 
 //struts默认拦截“.action以及不加任何后缀” 
 @action(value="regist")//访问:/user/regist.action 或 /user/regist 
 public string regist(){ 
  
 //获取request 
 httpservletrequest request = servletactioncontext.getrequest(); 
  
 //获取表单提交的数据 
 string username = request.getparameter("username"); 
 string password = request.getparameter("password"); 
 //封装userbean 
 user user = new user(); 
 user.setid(1000); 
 user.setusername(username); 
 user.setpassword(password); 
 
 //调用service层的方法,向数据库中增加一条记录 
 service.adduser(user); 
 
 //将提示信息存入request域中,用以前台显示 
 request.setattribute("msg", "恭喜您,注册成功!<br>注册名:"+username); 
 
 return "registsuccess"; 
 } 
 
} 

6.消息提示界面:msg.jsp代码,如下:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
 string path = request.getcontextpath(); 
 string basepath = request.getscheme() + "://" 
  + request.getservername() + ":" + request.getserverport() 
  + path + "/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
<head> 
<base href="<%=basepath%>"> 
 
<title>消息提示</title> 
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="this is my page"> 
<!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
</head> 
 
<body> 
 ${msg } 
</body> 
</html> 

7.将项目添加到服务器中,启动服务,打开浏览器,访问:http://www.lhsxpumps.com/_localhost/sshdemo/user/regist

8.输入用户名与密码,点击“注册”,显示结果:

9.控制台输出sql语句(在hibernatecontext.xml文件中已经配置过输出并美化sql语句):

10.查看数据库结果:

到此这个简单的案例就已经结束了,关于表单提交数据校验、以及乱码问题并未涉及,后续应该会更新吧、、、

七、总结:

1.三大框架的整合,应该先引入每个框架以后,再整合;

2.一定要记得导入数据库jar包;

3.action类应该要放在包名为"action"的包下,并且类名应当要以action结尾,形如“xxxaction”;

4.在配置hibernate时,一定要导入支持“@entity”注解的jar包;

5.可以再struts.xml文件中定义struts拦截的请求类型,默认为.action与不加后缀

6.可以再web.xml文件中定义struts过滤器的过滤类型,默认为*.action,应当改为/*;

7.在applicationcontext.xm文件中需要配置:sessionfactory、hibernate的实体类、hibernatetemplate模板 、数据源datasource、spring扫描器五部分(包含hibernatecontext.xml);

8.各个类中一定要加上对应的注解,以及action中的方法上也要加注解。

实例源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网