ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

ssh框架学习笔记(Myeclipse-java-web)

2021-12-26 12:02:18  阅读:225  来源: 互联网

标签:web java Commodity List void Myeclipse FONT public left


什么是SSH框架:
SSH框架是struts+spring+hiberanate的一个集成框架。集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,其中使用Struts作为系统的整体基础架构,负责MVC的分离【MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写】,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理struts和hibernate。

Struts
Struts的实现依赖Servlet和JSP实现,struts 负责 web 层(表现层),EJB和JavaBean两个组件是Struts框架业务功能实现的基础部件;Action和ActionServlet部件是框架实现控制力能的重要部件 ,ActionFormBean 接收网页中表单提交的数据,然后通过 Action 进行处理,再 Forward 到对应的网页,在 struts-config.xml 中定义 , ActionServlet 会加载。

Spring
【 spring 负责业务层管理,即 Service (或 Manager).】

Spring通过采用依赖注入(DI)的方式,通过属性的Setter和Getter方法来注入这个对象的属性,这样的好处就是不完全依赖于容器的API,且查询依赖与代码实现了解耦。而AOP,则是将应用的业务逻辑和系统级服务(例如事务)分离开来,进行内聚性的开发,应用对象只负责完成业务逻辑而不关心日志或者事务的处理。

service 为 action 提供统计的调用接口,封装持久层的 DAO;可以写一些自己的业务方法;统一的 javabean 管理方法;声明式事务管理;集成 Hiberante。

Hibernate
【负责跟数据库的交接,通过持久化数据对象,进行对象关系的映射,并以对象的角度来访问数据库。通过封装JDBC,使得开发人员可以以面向对象编程的思想来操控数据库,从而摆脱了以往使用JDBC编程时的“死板”操作,并且Hibernate有自己的HQL语句,与数据库的SQL语句相似。】

对象的调用流程是: jsp-> Action - > Service ->DAO ->Hibernate 。

数据的流向是 ActionFormBean 接受用户的数据, Action 将数据从ActionFromBean 中取出封装,再调用业务层的 Bean 类,完成各种业务处理后再 forward 。而业务层 Bean 收到这个 PO 对象之后,会调用 DAO 接口方法

部分代码记录:
所需使用的jar包:
在这里插入图片描述在这里插入图片描述

在web下的web.xml中配置struts的filter.
代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- 配置struts开始 -->
    <filter>
        <filter-name>myStruts</filter-name> <!--这个过滤器的名字可以随便写-->
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>myStruts</filter-name> <!--要和上面的那个过滤器的名字一致-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 配置struts结束 -->


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

在bean.xml文件中各种配置的注释

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.springframework.org/schema/beans" 
		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-4.2.xsd 
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">


				<context:annotation-config/>
					<!-- 打开自动扫描 -->
			<context:component-scan base-package="cn.itbaizhan" />
	<!-- 配置数据源和数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
	<property name="driverClass" value="com.mysql.jdbc.Driver"/>
	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/webshopping" />
	<property name="user" value="root"/>
	<property name="password" value="root"/>
	<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="1"/>
	<!--连接池中保留的最小连接数。-->
        <property name="minPoolSize" value="1"/>	
	<!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="100"/>
	<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="60"/>	
	<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="5"/>	
	<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod" value="60"/>
	
</bean>

	<!-- 将数据源注入到Spring的sessionFactory中 -->
<!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

		<property name="dataSource" ref="dataSource"/>
		<property name="packagesToScan">
    		<list>
    		 	<value>cn.itbaizhan.po</value>
    		</list>
   		</property>
		<property name="hibernateProperties">
				<props>
					<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
					<prop key="hibernate.hbm2ddl.auto">update</prop>
					<prop key="hibernate.show_sql">true</prop>
					<prop key="format_sql">true</prop>
				</props>
		</property>

</bean>
	
	<!-- 配置spring的HibernateTemplate的事务模版 自动生成数据表-->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
</beans>	

创建Action类继承ActionSupport

public class GoIndexAction extends ActionSupport {
	private CommodityClassService commodityClassService;
	private List<CommodityClass> commodityClasses;// 商品种类列表


	public CommodityClassService getCommodityClassService() {
		return commodityClassService;
	}

Dao层接口:

public interface CommodityDao {
	public void save(Commodity commodity);
	public void delete(Commodity commodity);
	public Commodity findCommodityById(int id);
	public List<Commodity> findAllCommoditys();
	public void update(Commodity commodity);
	public List<Commodity> findCommodityByName(String Name);
	public List<Commodity> findCommodityByClass(CommodityClass commodityclass);
	public List<Commodity> findCommodityBName(String name);
}

执行Hql语句操作数据库

@SuppressWarnings("unchecked")
	public List<Message> findAllMessages() {
		String hql = "from Message";
		return (List<Message>)hibernateTemplate.find(hql);
	}

Service层的封装

public interface CommodityService {
//	和CommodityDao的方法一样,可以封装,因为这边只是调用
	public void save(Commodity commodity);
	public void delete(Commodity id);
	public Commodity findCommodityById(int id);
	public List<Commodity> findAllCommoditys();
	public void update(Commodity commodity); 
	public List<Commodity> findCommodityByName(String Name);
	public List<Commodity> findCommodityByClass(CommodityClass commodityclass);
	public List<Commodity> findCommodityBName(String commodityName);
}

Jsp店铺管理页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>网上商城系统后台管理</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<LINK href="images/style.css" type=text/css rel=stylesheet>
<STYLE>
.main_left {
	TABLE-LAYOUT: auto;
	BACKGROUND: url(images/left_bg.gif)
}

.main_left_top {
	BACKGROUND: url(images/left_menu_bg.gif);
	PADDING-TOP: 5px
}

.main_left_title {
	PADDING-LEFT: 15px;
	FONT-WEIGHT: bold;
	FONT-SIZE: 14px;
	COLOR: #fff;
	TEXT-ALIGN: left
}

.left_iframe {
	BACKGROUND: none transparent scroll repeat 0% 0%;
	VISIBILITY: inherit;
	WIDTH: 180px;
	HEIGHT: 92%
}

.main_iframe {
	Z-INDEX: 1;
	VISIBILITY: inherit;
	WIDTH: 100%;
	HEIGHT: 92%
}

TABLE {
	FONT-SIZE: 12px;
	FONT-FAMILY: tahoma, 宋体, fantasy
}

TD {
	FONT-SIZE: 12px;
	FONT-FAMILY: tahoma, 宋体, fantasy
}
</STYLE>

<SCRIPT language=javascript src="images/admin.js" type=text/javascript></SCRIPT>

<SCRIPT language=javascript src="images/Admin(1).js"></SCRIPT>

<SCRIPT>
var status = 1;
var Menus = new DvMenuCls;
document.onclick=Menus.Clear;
function switchSysBar(){
     if (1 == window.status){
		  window.status = 0;
          switchPoint.innerHTML = '<img src="images/left.gif">';
          document.all("frmTitle").style.display="none";
     }
     else{
		  window.status = 1;
          switchPoint.innerHTML = '<img src="images/right.gif">';
          document.all("frmTitle").style.display="";
     }
}
</SCRIPT>

<META content="MSHTML 6.00.2900.5726" name=GENERATOR>
</HEAD>
<BODY>
	<!--导航部分-->

	<c:if test="${!empty sessionScope.admin.username}">
		<DIV class=top_table align="center" style="margin:1px auto;width:80%">
			<DIV class=top_table_leftbg>
				<DIV class=system_logo></DIV>
				<DIV class=menu>
					<UL>
						<LI id=menu_1 onm ouseover=Menus.Show(this,0)
							onclick=getleftbar(this);><A href="#">商品管理</A>
							<DIV class=menu_childs onm ouseout=Menus.Hide(0);>
								<UL>
									<LI><A href="include/commodityClass/commodityClassAdd.jsp"
										target="frmright">新增商品种类</A></LI>
									<LI><A
										href="CommodityClassAction!listCommodityClass.action"
										target=frmright>商品种类列表</A></LI>
									<LI><A href="CommodityAction!adCommodity.action"
										target=frmright>商品录入</A></LI>
									<LI><A href="CommodityAction!listCommodity.action"
										target=frmright>商品列表</A></LI>
								</UL>
							</DIV>
							<DIV class=menu_div>
								<IMG style="VERTICAL-ALIGN: bottom"
									src="images/menu01_right.gif">
							</DIV></LI>
						<LI id=menu_2 onm ouseover=Menus.Show(this,0)
							onclick=getleftbar(this);><A href="#">订单管理</A>
							<DIV class=menu_childs onm ouseout=Menus.Hide(0);>
								<UL>
									<LI><A href="OrderFormAction!listOrderForm.action"
										target=frmright>订单列表</A></LI>
								</UL>
							</DIV>
							<DIV class=menu_div>
								<IMG style="VERTICAL-ALIGN: bottom"
									src="images/menu01_right.gif">
							</DIV></LI>
						<LI id=menu_3 onm ouseover=Menus.Show(this,0)
							onclick=getleftbar(this);><A href="#">用户管理</A>
							<DIV class=menu_childs onm ouseout=Menus.Hide(0);>
								<UL>
									<LI><A href="UserAction!listUser.action" target=frmright>用户列表</A>
									</LI>
									<LI><A href="include/user/userQuery.jsp" target=frmright>用户查询</A>
									</LI>
								</UL>
							</DIV>


							<DIV class=menu_div>
								<IMG style="VERTICAL-ALIGN: bottom"
									src="images/menu01_right.gif">
							</DIV></LI>
						<LI id=menu_4 onm ouseover=Menus.Show(this,0)
							onclick=getleftbar(this);><A href="#">系统管理</A>
							<DIV class=menu_childs onm ouseout=Menus.Hide(0);>
								<UL>
									<LI><A href="include/admin/amend.jsp" target=frmright>修改登录密码</A>
									</LI>
								</UL>
							</DIV>
							<DIV class=menu_div>
								<IMG style="VERTICAL-ALIGN: bottom"
									src="images/menu01_right.gif">
							</DIV></LI>
					</UL>
				</DIV>
			</DIV>
		</DIV>
		<DIV style="BACKGROUND: #337abb; WIDTH: 80%;margin:1px auto; HEIGHT: 24px"></DIV>
		<!--导航部分结束-->
		<TABLE style="BACKGROUND: #337abb" height="92%" cellSpacing=0 align="center"
			cellPadding=0 width="80%" border=0>
			
			<TBODY>
				<TR>
					<TD class=main_left id=frmTitle vAlign=top align=center>
						<TABLE class=main_left_top cellSpacing=0 cellPadding=0
							width="100%" border=0>
							
						</TABLE> 
						<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
							<TBODY>
								<TR height=32>
									<TD vAlign=top></TD>
									<TD vAlign="bottom" align="center"></TD>
									<TD vAlign=top align=right></TD>
								</TR>
							</TBODY>
						</TABLE>
					</TD>
					<TD style="WIDTH: 10px" bgColor=#337abb>
>
					<TD vAlign=top width="100%" bgColor=#337abb>
						<TABLE cellSpacing=0 cellPadding=0 width="100%" bgColor=#c4d8ed
							border=0>
							<TBODY>
								<TR height=32>
									

									<TD text-align:center><SPAN >店铺系统后台管理</SPAN>
										<SPAN id=dvbbsannounce
										style="FONT-WEIGHT: bold; FLOAT: left; WIDTH: 80%; COLOR: #c00">
									</SPAN></TD>
									<TD style="COLOR: #135294; TEXT-ALIGN: right"
										>管理员:${sessionScope.admin.username}|
										<A href="index.jsp">后台首页</A> | <A href="/webbikeshop/GoIndexAction.action"
										target=_top>店铺首页</A> | <A href="login.jsp"
										onclick="return confirm('确定要退出吗?')" target=_top>退出 </A>
									</TD>
									
									<TD align=right width=16 bgColor=#337abb></TD>
								</TR>
							</TBODY>
						</TABLE> <IFRAME class=main_iframe id=frmright name="frmright"
							src="syscome.htm" frameBorder=0 scrolling=yes></IFRAME>
						<TABLE style="BACKGROUND: #c4d8ed" cellSpacing=0 cellPadding=0
							width="100%" border=0>
							<TBODY>
								
							</TBODY>
						</TABLE>
					</TD>
				</TR>
			</TBODY>
		</TABLE>
		<DIV id=dvbbsannounce_true style="DISPLAY: none"></DIV>
	</c:if>
	<c:if test="${empty sessionScope.admin.username}">

		<table width=100% height=80% align=center bgColor="#c0c0c0">
			<tr>
				<td align="center">
					<h3>对不起,还未登录,请登录</h3>
					<h3>
						正在跳转到登录页面,若没有跳转请<a href=login.jsp>点击这里!</a>
					</h3>
				</td>
			</tr>
		</table>

		<%
			response.setHeader("refresh", "2;url=login.jsp");
		%>
	</c:if>

</BODY>
</HTML>



标签:web,java,Commodity,List,void,Myeclipse,FONT,public,left
来源: https://blog.csdn.net/Tian208/article/details/122142139

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有