eclipse环境搭建ssh(Struts2+Spring2.5+Hibernate3)框架小谈(下)

在这里真的感到非常抱歉,由于一些客观原因eclipse环境搭建ssh(Struts2+Spring2.5+Hibernate3)框架小谈(上)之后才更新eclipse环境搭建ssh(Struts2+Spring2.5+Hibernate3)框架小谈(下),以后如果发表连载的博文,我一定会注意的!

不过,我一定会用心的编辑每一篇文章,一方面便于自己回顾,另一方便也希望能够帮到需要的小伙伴。

eclipse环境搭建ssh(Struts2+Spring2.5+Hibernate3)框架小谈(上)中我们已经准备好所有的java文件部分,现在看一下ssh搭建的关键之处,配置文件!!

※1、ssh框架如何实现一个功能,(思考:页面上提交的数据到底怎么去的数据库  然后又回到页面重新显示的呢,我到底应该做些什么来实现它)

以下使用“注册”功能来详细讲解

View (视图):本项目中JSP来充当这个角色

1)、reg.jsp   注册过程数据流详细解析 源代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户注册界面</title>
</head>
<body>
	<form action="reg" method="post" name="regForm">
		<table>
			<tr>
				<td>用户名</td>
				<td><input type="text" name="userName" size="15" /></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="password" name="pwd" size="15" /></td>
			</tr>
			<tr>
				<td>姓名</td>
				<td><input type="text" name="name" size="15" /></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="注册"></td>
			</tr>
		</table>
	</form>

</body>
</html>

reg.jsp中 

<form action="reg" method="post" name="regForm">----->reg是怎么知道自己需要干什么事情的呢?

此刻至关重要的一个文件就是struts.xml(第一个配置文件)

jsp提交之后会寻找对应的action来操作,这个Action呢?????就定义在struts.xml中,action的定义规范 一般为如下形式

 <action name="login"  class="LoginAction">
            <result name="success">/result.jsp</result>
            <result name="input">/login.jsp</result>
 </action>

struts.xml源码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.i18n.encoding" value="GBK"></constant>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<constant name="struts.objectFactory" value="spring"/>

    <package name="ssh" namespace ="/" extends="struts-default">
        <!-- 此处的class的内容要与Spring配置文件中的bean的id相同 -->        
         <action name="login"  class="LoginAction">
            <result name="success">/result.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
        <action name="reg" class="RegAction">
            <result name="success">/regsuccess.jsp</result>
            <result name="input">/reg.jsp</result>
        </action>
        <action name="listUser" class="ListUserAction">
            <result name="success">/listUsers.jsp</result>
        </action>
         <action name="deleteUser" class="DeleteUserAction">
            <result name="success" type="redirect">listUser.action</result>
        </action>
        
        <action name="updateUserP" class="UpdateUserPAction">
            <result name="success" >/update.jsp</result>
        </action>
         <action name="updateUser" class="UpdateUserAction">
            <result name="success" type="redirect">listUser.action</result>
        </action>
        
        <!--  <action name="reg" class="com.jh.action.UserAction" >
            <result name="success">/regsuccess.jsp</result>
        </action> -->
    </package>
</struts>
接下就是ACTION会分配业务给这个请求,此时需要关联的就是spring的配置文件 applicationContext.xml(第二个配置文件)
 applicationContext.xml的文件中会有一个与上述struts.xml中呼应的bean 

也就是struts.xml的class的内容要与Spring配置文件 applicationContext.xml中的bean的id相同

 <bean id="RegAction" class="com.jh.action.RegAction" scope="prototype">
        <property name="userService" ref="userService"></property>
  </bean>

附 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:context="http://www.springframework.org/schema/context"    
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx"    
  xsi:schemaLocation="http://www.springframework.org/schema/beans         
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        
  http://www.springframework.org/schema/context         
  http://www.springframework.org/schema/context/spring-context-2.5.xsd        
  http://www.springframework.org/schema/aop         
  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd        
  http://www.springframework.org/schema/tx        
 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <!-- 指定连接数据库的驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!-- 指定连接数据库的URL -->
        <property name="url" value="jdbc:mysql://localhost:3306/Kind" />
        <!-- 指定连接数据库的用户名 -->
        <property name="username" value="root" />
        <!-- 指定连接数据库的密码 -->
        <property name="password" value="123456" />
    </bean>
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="sql_format">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <!-- 指定hibernate映射文件 -->
            <list>
                
                <value>com/jh/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>

   
    <!-- Dao配置 -->
    <bean id="userDao" class="com.jh.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- Service配置 -->
    <bean id="userService" class="com.jh.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="RegAction" class="com.jh.action.RegAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
     
    <bean id="LoginAction" class="com.jh.action.LoginAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    <bean id="ListUserAction" class="com.jh.action.ListUserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
     <bean id="DeleteUserAction" class="com.jh.action.DeleteUserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
     <bean id="UpdateUserPAction" class="com.jh.action.UpdateUserPAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
     <bean id="UpdateUserAction" class="com.jh.action.UpdateUserAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    
    <!-- <bean id="regAction" class="com.jh.action.UserAction"  scope="prototype" >
        <property name="userService" ref="userService"></property>
    </bean> -->
</beans>

总结一下applicationContext.xml:个人认为这个文件就是连接了项目中定义的Action和 Service 起到了粘合的作用!

知道了Action是什么之后我们需要了解的就是 Service是什么?

Service就是业务层,业务层是“三层框架(三层体系结构)”的其中一层

简要解释一下三层框架(这个Kind项目扩展了几个功能所以和之前的有些不一样 但是框架没有变化)


2、其他配置文件以及jsp源码

配置文件部分

1)、User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
	<class name="com.jh.entity.User" table="USERDETAILS">
		<id name="id">
			<column name="ID" />
			<generator class="increment" />
		</id>
		<property name="userName" type="java.lang.String">
			<column name="userName"  />
		</property>
		<property name="pwd" type="java.lang.String">
			<column name="pwd"  />
		</property>
		<property name="name" type="java.lang.String">
			<column name="name" />
		</property>
	</class>
</hibernate-mapping>
2)、mess.properties

loginPage=登录页面
errorPage=错误页面
succPage=成功页面
failTip=对不起,您不能登录!
succTip=欢迎,{0},您已经登录!
user=用户名
pass=密码
login=登录

3)、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>TestDemo</display-name>
	<!-- 用来定位Spring框架配置文件 -->
	 <context-param> 
    <param-name>contextConfigLocation</param-name> 
        
         <param-value>/WEB-INF/classes/applicationContext*.xml,classpath*:applicationContext*.xml</param-value> 
    </context-param>  
     <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

	<welcome-file-list><welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- 配置Struts -->
	<filter>
		<filter-name>struts2</filter-name><!-- 此处名字要与下面保持一致 -->
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>

jsp部分

1)、regsuccess.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册成功e</title>
</head>
<body>
<h1>注册成功</h1>
<s:a href="login.jsp">登录</s:a><br/>
</body>
</html>


2)、login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户登陆界面</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="login">
  <label>姓名:
  <input type="text" name="userName" />
  </label>
  <p>
    <label>
    密码:
    <input type="text" name="password" />
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>
  </p>
</form>


</body>
</html>
3)、result.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆成功</title>
</head>
<body>
    成功
</body>
</html>

4)、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>

  </head>
  
  <body>
	<h1><font color="red">Operation List</font></h1>
	<s:a href="reg.jsp">注册</s:a><br/>
	<s:a href="login.jsp">登录</s:a><br/>
	<s:a href="listUser.action">List Users</s:a>		
  </body>
</html>

3、运行截图(+后期扩展功能截图)

1)、首页


2)、注册


3)、注册成功


4)、登录


5)、登录成功


6)、显示所有用户


7)、删除用户


8)、更新用户信息


9)、删除、更新之后



极少数情况下搭建过程可能也会遇到与以下一个文件有关的问题

\workspace\Kind\.settings\org.eclipse.wst.common.project.facet.core.xml (本工程对应内容如下)

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
  <runtime name="Apache Tomcat v8.0"/>
  <fixed facet="jst.web"/>
  <fixed facet="wst.jsdt.web"/>
  <fixed facet="java"/>
  <installed facet="java" version="1.7"/>
  <installed facet="jst.web" version="3.1"/>
  <installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>


相关jar包和工程下载链接如下


struts2:http://download.csdn.net/detail/acream/9744305

spring:http://download.csdn.net/detail/acream/9744306

hibernatehttp://download.csdn.net/detail/acream/9744307

Kind工程:http://download.csdn.net/detail/acream/9744315

以上为全部搭建过程,内容如有不当或者错误之处,希望阁下可以联系我,小生必加以纠正,让我们一起学习

邮箱------330151437(添加好友请备注csdn+博文名称)!



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值