基于ssh2的小demo

此文主要是练习并学习ssh2,适用于初学者群体,大牛勿进,不喜勿喷!微笑

主要解决有一下几个问题:

1.利用struts2的taglib标签库的iterator迭代生成多表单数据并存储,读取并添加跟新功能。

2.直接提取TXT的发票文件并上传解析存储

3.解决许多新手开发遇到的问题,架构已搭好,只需要专注于逻辑实现。

代码资源包

一.项目结构:



采用此demo的小伙伴最好不要更改项目结构,这是标准结构,因为可能加载的时候会因为包结构的问题导致找不到路径等~

二.项目结构的讲解

1.主体包

看主题路径分有一级包src和webroot:src内是类和方法包括mvc中的mc,处理业务问题等;webroot内包含的就是view的一些元素:jsp,css,js等,当然不是绝对的,但是这样不会有错。看波浪线的那个文件,必须要和src同级的,这就是为什么不建议修改主题结构的原因。


2.src的内容

src下面有structs.xml文件,主要是控制action的加载路径与界面跳转的问题。


由于invoice,purchaseOrder,salesOrder的业务逻辑是类似的,所以此处只来说一个:salesOrder。

action中的类主要是处理要进行的操作方法,所有方法都继承BaseAction(其中定义了一下基本操作和会话);

package com.mockservice.action;

import java.util.List;

import com.mockservice.model.Salesorder;
import com.mockservice.serviceImpl.SalesOrderServiceImpl;

public class SalesOrderAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SalesOrderServiceImpl salesOrderService;
	private String createDate;
	private String createTime;
	private String creater;
	private String lastChangeDate;
	private String lastChangeTime;
	private String lastChanger;
	private String customerId;
	private String cusPurOrderId;
	private String addressDescription;
	private String postCode;
	private String receiverName;
	private String phoneNumber;
	private int itemLine;
	private String status;
	List<Salesorder> listSO;
	private int[] rows;
	public String addSO(){
		Salesorder salesOrder = new Salesorder();
		salesOrder.setAddressDescription(addressDescription);
		salesOrder.setCreateDate(createDate);
		salesOrder.setCreater(creater);
		salesOrder.setCreateTime(createTime);
		salesOrder.setCusPurOrderId(cusPurOrderId);
		salesOrder.setCustomerId(customerId);
		salesOrder.setLastChangeDate(lastChangeDate);
		salesOrder.setLastChanger(lastChanger);
		salesOrder.setReceiverName(receiverName);
		salesOrder.setPostCode(postCode);
		salesOrder.setPhoneNumber(phoneNumber);
		salesOrder.setLastChangeTime(lastChangeTime);
		salesOrder.setItemLine(itemLine);
		rows=new int[itemLine];
		for(int i=0;i<itemLine;i++){
			rows[i]=i;
		}
		salesOrder.setStatus(status);
		
		salesOrderService.addSailesOrder(salesOrder);
		return "addSalesSuccess";
	}
	public String getAllSO(){
		listSO = salesOrderService.getAllSO();
		return SUCCESS;
	}
	public String updateSOStatus(){
		if(listSO.size()!=0 && listSO!=null){
			salesOrderService.updateSOStatus(listSO);
		}else{
			return ERROR;
		}
		
		return SUCCESS;
	}
	public List<Salesorder> getListSO() {
		return listSO;
	}

	public void setListSO(List<Salesorder> listSO) {
		this.listSO = listSO;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public SalesOrderServiceImpl getSalesOrderService() {
		return salesOrderService;
	}

	public void setSalesOrderService(SalesOrderServiceImpl salesOrderService) {
		this.salesOrderService = salesOrderService;
	}

	public int getItemLine() {
		return itemLine;
	}

	public void setItemLine(int itemLine) {
		this.itemLine = itemLine;
	}

	public String getLastChanger() {
		return lastChanger;
	}

	public void setLastChanger(String lastChanger) {
		this.lastChanger = lastChanger;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getCusPurOrderId() {
		return cusPurOrderId;
	}

	public void setCusPurOrderId(String cusPurOrderId) {
		this.cusPurOrderId = cusPurOrderId;
	}

	public String getAddressDescription() {
		return addressDescription;
	}

	public void setAddressDescription(String addressDescription) {
		this.addressDescription = addressDescription;
	}

	public String getCreateDate() {
		return createDate;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}

	public String getLastChangeDate() {
		return lastChangeDate;
	}

	public void setLastChangeDate(String lastChangeDate) {
		this.lastChangeDate = lastChangeDate;
	}

	public String getLastChangeTime() {
		return lastChangeTime;
	}

	public void setLastChangeTime(String lastChangeTime) {
		this.lastChangeTime = lastChangeTime;
	}

	public String getPostCode() {
		return postCode;
	}

	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}

	public String getReceiverName() {
		return receiverName;
	}

	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	public int[] getRows() {
		return rows;
	}
	public void setRows(int[] rows) {
		this.rows = rows;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	
}

serviceInterface是来定义接口,serviceImpl来实现接口,这里就是主要处理业务逻辑。(代码占篇幅,就不贴了,可以进行资源下载

而dao和model则很简单通过hibernate就可以实现,要现在数据库中定义好自己的数据属性及数据类型。此demo的sql语句下载


在表上右键,选择hibernate reverse engineering即可。规范类的位置,接下来就看路径的问题。

路径在struts的文件中控制,为了防止项目庞大之后不好管理,现在把invoice,purchaseOrder,salesOrder都分离出来了,同一有struts.xml来控制:


而在struts-salesOrder.xml中:


注意name,class,result的名称。

3.WebRoot的文件


lib中是要加载的jar包。在addsalesorder.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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>添加销售订单</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <link type="text/css" href="css/bootstrap-timepicker.min.css" />
	<script type="text/javascript" src="<%=basePath%>js/bootstrap-timepicker.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/bootstrap-timepicker.min.js"></script>
 
</head>

<body>
<div>
   <form action="addSO.action" method="post" id="salesOrder">
               销售订单号:<input type="text" name="salesOrderNumber"><br>
	    创建日期:<input type="text" name="createDate" class="datepicker"><br>
	    创建时间:<input type="text" name="createTime" class="timepicker"><span class="add-on"><i class="icon-time"></i></span><br>
	    创建人:<input type="text" name="creater"><br>
	    最终更改日期:<input type="text" name="lastChangeDate" class="datepicker"><br>
	    最终更改时间:<input type="text" name="lastChangeTime" class="timepicker"><br>
	    最终更改人:<input type="text" name="lastChanger"><br>
	    买方ID:<input type="text" name="customerId" ><br>
	    买方订货单号:<input type="text" name="cusPurOrderId" ><br>
	    地址描述:<input type="text" name="addressDescription"><br>
	    邮编:<input type="text" name="postCode"><br>
	    接收人姓名:<input type="text" name="receiverName"><br>
	    电话号码:<input type="text" name="phoneNumber"><br>
	    商品明细行数:<input type="text" name="ItemLine" id="ItemLine"><br>
	    状态:<input type="text" name="status"><br>
            <input type="submit" name="salesOrder" value="生成销售订单" >
    </form>
     
</div>
  
 	 <script>
    $(function() {
    $(".datepicker").datepicker();
    $(".timepicker").timepicker();
  });
  </script>
 

</body>
</html>
注意form的action就是在action类中定义的方法名,与struts.xml文件中加载的名称是一样的。这里salesOrderItem的表单数是要根据salesOrder中的itemline的数值数生成的,所以item就要用iterator迭代生成了。

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!--<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>-->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>添加销售订单内容明细</title>
<body>
<div>
<h2>销售订单添加成功,请为此销售订单添加内容明细:</h2>
   		<table>
   			<s:form action="addSOItem.action" method="post" name="salesOrderItemForm">
             	<tr>
                 <td>销售订单号</td><td>货物编号</td><td>货币种类</td><td>金额</td><td>货物数量</td><td>货物描述</td><td>计量单位</td><td>单价</td>
                 </tr>

				 <s:iterator value="rows" status="stat">
				   <tr align="center" height="30">
				   <td><input type="text" name="listSOItem[${stat.index}].salesOrderNumber"/></td>
	 		       <td><input type="text" name="listSOItem[${stat.index}].itemNumber"/></td> 	
				   <td><input type="text" name="listSOItem[${stat.index}].currency"/></td>
				   <td><input type="text" name="listSOItem[${stat.index}].amount"/></td>
				   <td><input type="text" name="listSOItem[${stat.index}].quantity"/></td>
				   <td><input type="text" name="listSOItem[${stat.index}].productDescription"/></td>
				   <td><input type="text" name="listSOItem[${stat.index}].unitOfMeasure"/></td>
				   <td><input type="text" name="listSOItem[${stat.index}].unitPrice"/></td>
				   </tr>
				 </s:iterator> 
            	<tr>
   				<td><s:submit  align="left" value="提交"></s:submit></td>
   				</tr>
			</s:form>
          </table>
                  
       
     
</div>
</body>
</html>
rows的值是从action传过来的。这个是利用iterator生成多表单数据进行存储。

下面的jsp就是从数据库查询数据并更新其中的status的数据:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>
  </head>
  
  <body>
    <form action="updateSOStatus.action" method="post" name="updateSOStatus">
		<table>
			<thead>
				<th>销售订单数据</th>
			</thead>
			<tbody>
				<tr>
			       <td>ID</td>
			       <td>SalesOrderNumber</td>
			       <td>Status</td>
			    </tr>
			   	<s:iterator value="listSO" var="so" status="stat">
				   	<tr>
				       <td>${so.id}</td>
				       <td>${so.salesOrderNumber}</td>
				       <td>
				       	<select name="listSO[${stat.index}].status">
				       		<option value="${so.status}">${so.status}</option> 	
				       		<option value="In Process">To be Invoiced</option>
				       		<option value="Ordered">Not Invoiced</option>
				       	</select>
				       </td>
				    </tr>
				    <input style="display:none" type="text" name="listSO[${stat.index}].id" value="${so.id }"/>   
			    </s:iterator>
			</tbody>
		</table>
		<input type="submit" value="update"/>	
	</form>
  </body>
</html>

4.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"
	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.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

		<property name="url"
			value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>
	<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"
		lazy-init="true" />
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="lobHandler" ref="lobHandler"></property>
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="format_sql">false</prop>
				<prop key="use_sql_comments">false</prop>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.connection.release_mode">  
                    after_statement  
                </prop> 
                <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
				<prop key="connection.useUnicode">true</prop>
				<prop key="connection.characterEncoding">UTF-8</prop>
 			</props>
		</property>

		<property name="mappingResources">
			<list>
			    <value>com/mockservice/model/Invoices.hbm.xml</value>
			    <value>com/mockservice/model/Invoiceitem.hbm.xml</value>
			    <value>com/mockservice/model/Purchaseorder.hbm.xml</value>	
			    <value>com/mockservice/model/Purchaseorderitem.hbm.xml</value>
			    <value>com/mockservice/model/Salesorder.hbm.xml</value>
			    <value>com/mockservice/model/Salesorderitem.hbm.xml</value>
			</list>
		</property>
	</bean>


	<!-- 配置事务管理器 -->

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<!-- 配置事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="showAllMindTestTitle" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="commit*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />
			<tx:method name="show*" read-only="true" propagation="NOT_SUPPORTED" />
		</tx:attributes>
	</tx:advice>

	
	<!-- dao -->
	<bean id="InvoicesDAO" class="com.mockservice.dao.InvoicesDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="InvoiceitemDAO"
		class="com.mockservice.dao.InvoiceitemDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	<bean id="PurchaseorderDAO"
		class="com.mockservice.dao.PurchaseorderDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	<bean id="PurchaseorderitemDAO"
		class="com.mockservice.dao.PurchaseorderitemDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	<bean id="SalesorderDAO"
		class="com.mockservice.dao.SalesorderDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	<bean id="SalesorderitemDAO"
		class="com.mockservice.dao.SalesorderitemDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory"></ref>
		</property>
	</bean>
	
	<!-- Action -->
	<bean id="InvoiceAction" class="com.mockservice.action.InvoiceAction" scope="prototype">
	    <property name="invoiceService" ref="InvoiceService"></property>
	</bean>
	<bean id="InvoiceItemAction" class="com.mockservice.action.InvoiceItemAction" scope="prototype">
	    <property name="invoiceItemService" ref="InvoiceItemService"></property>
	</bean>
	<bean id="PurchaseOrderAction" class="com.mockservice.action.PurchaseOrderAction" scope="prototype">
	    <property name="purchaseOrderService" ref="PurchaseOrderService"></property>
	</bean>
	<bean id="PurchaseOrderItemAction" class="com.mockservice.action.PurchaseOrderItemAction" scope="prototype">
	    <property name="purchaseOrderItemService" ref="PurchaseOrderItemService"></property>
	</bean>
	<bean id="SalesOrderAction" class="com.mockservice.action.SalesOrderAction" scope="prototype">
	    <property name="salesOrderService" ref="SalesOrderService"></property>
	</bean>
	<bean id="SalesOrderItemAction" class="com.mockservice.action.SalesOrderItemAction" scope="prototype">
	    <property name="salesOrderItemService" ref="SalesOrderItemService"></property>
	</bean>
	
			<!--分析上传发票-->
	<bean id="UploadInvoiceAction" class="com.mockservice.action.UploadInvoiceAction" scope="prototype">
	    <property name="invoiceService" ref="InvoiceService"></property>
	    <property name="invoiceItemService" ref="InvoiceItemService"></property>
	</bean>
	
	
	<!-- Service -->
	<bean id="InvoiceService" class="com.mockservice.serviceImpl.InvoiceServiceImpl">
		<property name="invoicesDAO" ref="InvoicesDAO" />
	</bean>
	<bean id="InvoiceItemService" class="com.mockservice.serviceImpl.InvoiceItemServiceImpl">
		<property name="invoiceItemDAO" ref="InvoiceitemDAO" />
	</bean>
	<bean id="PurchaseOrderService" class="com.mockservice.serviceImpl.PurchaseOrderServiceImpl">
		<property name="purchaseOrderDAO" ref="PurchaseorderDAO" />
	</bean>
	<bean id="PurchaseOrderItemService" class="com.mockservice.serviceImpl.PurchaseOrderItemServiceImpl">
		<property name="purchaseOrderItemDAO" ref="PurchaseorderitemDAO" />
	</bean>
	<bean id="SalesOrderService" class="com.mockservice.serviceImpl.SalesOrderServiceImpl">
		<property name="salesOrderDAO" ref="SalesorderDAO" />
	</bean>
	<bean id="SalesOrderItemService" class="com.mockservice.serviceImpl.SalesOrderItemServiceImpl">
		<property name="salesOrderItemDAO" ref="SalesorderitemDAO" />
	</bean>
	
	</beans>


三.其他功能

还有文件上传等功能,不过实现的比较粗糙,此处文件时金税发票,模板可百度,具体见代码。

基本的形式和过程就是这样,可先把demo下载,运行一下再参考学习。

tomcat启动运行:http://localhost:8080/mockservice/
界面:


------>>>


更改数据:



猜你喜欢

转载自blog.csdn.net/Jesse891012/article/details/22191531