SSH综合项目实战(快递) -- day07 定区关联快递员、搭建前台、客户注册

一、定区关联快递员

1、显示快递员列表

(1)、点击关联快递员按钮时的事件


(2)、编写CourierAction中提供查询快递员列表方法

	/**
	 * 获取所有未删除的快递员信息
	 */
	@Action(value="courierAction_listajax")
	public String listajax(){
		List<Courier> list = serivce.findCouriersNotDelete();
		this.list2json(list, new String[]{"standard","takeTime","fixedAreas"});
		return NONE;
	}

(3)、编写Service层代码

	/**
	 * 获取所有未删除的快递员信息
	 */
	public List<Courier> findCouriersNotDelete() {
		return dao.findByDeltagIsNull();
	}

(4)、编写Dao层代码

	/**
	 * 规范命名:
	 * 查询所有未被删除的快递员信息
	 * @return
	 */
	public List<Courier> findByDeltagIsNull();

(5)、页面展示效果


2、添加收派时间的增删改查功能


3、显示收派时间列表

(1)、修改fixed_area.html页面显示收派时间的列表

	<input data-options="ditable:false, url:'../../takeTimeAction_listajax.action',valueField:'id',textField:'name'"
		 required="true" class="easyui-combobox" name="takeTimeId">
	</input>


(2)、效果展示


4、实现定区关联快递员

(1)、绑定按钮点击事件

	<!-- 为关联快递员按钮绑定点击事件 -->
	<script type="text/javascript">
		$(function(){
			$("#associationCourierBtn").click(function(){
				//进行表单校验
				if($("#courierForm").form("validate")){
					//提交表单之前,给隐藏于(存放当前选中的定区id)动态赋值
					var id = $("#grid").datagrid("getSelections")[0].id;
					$("#courierFixedAreaId").val(id);
					$("#courierForm").submit();
				}
			});
		});
	</script>


(2)、编写Action动作类代码

	//使用属性驱动封装地区关联快递员时接收的参数
	private Integer courierId;
	private Integer takeTimeId;
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	/**
	 * 定区关联快递员的操作
	 */
	@Action(value="fixedAreaAction_associationCourierToFixedArea",results={
			@Result(name="success",type="redirect",location="/pages/base/fixed_area.html")
	})
	public String associationCourierToFixedArea(){
		service.associationCourierToFixedArea(model.getId(), courierId, takeTimeId);
		return SUCCESS;
	}

(3)、编写Service层代码

	@Autowired
	private CourierDao courierDao;
	
	@Autowired
	private TakeTimeDao takeTimeDao;

	/**
	 * 定区关联快递员的操作
	 */
	public void associationCourierToFixedArea(String id, Integer courierId, Integer takeTimeId) {
		//根据定区id获取定区对象
		FixedArea fixedArea = dao.findOne(id);
		//根据快递员id获取的快递员对象
		Courier courier = courierDao.findOne(courierId);
		//根据收派时间id获取收派时间对象
		TakeTime takeTime = takeTimeDao.findOne(takeTimeId);
		//定区关联快递员操作
		fixedArea.getCouriers().add(courier);
		//快递员关联收派时间
		courier.setTakeTime(takeTime);
	}

二、前台项目bos_fore搭建

1、前台系统的功能

前台系统主要提供网上用户、在线用户可以进行注册、登录、在线下单、查询物流信息等功能。

2、搭建前台系统maven项目,继承common-parent



3、导入准备好的前台页面资源



4、配置web.xml中spring的监听和struts2的核心控制器

<?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"
	version="2.5">
	<display-name>bos_fore</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!-- 初始化spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 配置struts2核心控制器 -->
	<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>

5、配置spring核心配置文件

<beans  xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
		xmlns:task="http://www.springframework.org/schema/task"
		xmlns:jaxws="http://cxf.apache.org/jaxws" 
		xmlns:soap="http://cxf.apache.org/bindings/soap"
		xsi:schemaLocation="
							http://www.springframework.org/schema/beans 
							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/jdbc 
							http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
							http://www.springframework.org/schema/tx 
							http://www.springframework.org/schema/tx/spring-tx.xsd
							http://www.springframework.org/schema/data/jpa 
							http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
							http://cxf.apache.org/bindings/soap 
							http://cxf.apache.org/schemas/configuration/soap.xsd
							http://cxf.apache.org/jaxws 
							http://cxf.apache.org/schemas/jaxws.xsd">
	
	<!-- 组件扫描 @Server @Controller @Repository -->
	<context:component-scan base-package="com.itheima.bos_fore.web"/>
	
</beans>

6、创建tomcat服务器,修改端口号,部署前台项目


7、启动tomcat,访问效果图


三、客户注册功能

1、客户注册页面验证码60秒倒计时效果实现

(1)、实现发送短信60秒倒计时的js代码

	<!-- 实现点击获取验证码,60秒倒计时的操作 -->
	<script type="text/javascript">
		var clock = '';
		var nums = 60;
		var btn;
		var reg = /^1[3|4|5|7|8][0-9]{9}$/;//定义手机号验证的正则表达式
		function sendCode(thisBtn) {
			//获取用户输入的手机号
			var telephone = $("#telephone").val();
			var r = reg.test(telephone);
			if(r == false){
				//输入的手机号错误
				return;
			}
			btn = thisBtn;
			btn.disabled = true; //将按钮置为不可点击
			btn.value = nums + '秒后重新获取';
			clock = setInterval(doLoop, 1000); //一秒执行一次
		}
		function doLoop() {
			nums--;
			if (nums > 0) {
				btn.value = nums + '秒后重新获取';
			} else {
				clearInterval(clock); //清除js定时器
				btn.disabled = false;
				btn.value = '重新获取验证码';
				nums = 60; //重置时间
			}
		}
	</script>


2、使用吉信通发送短信

(1)、将工具类导入前台系统

package com.itheima.bos_fore.utils;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 调用吉信通 发短信工具类
 */
public class SmsUtils {
	public static void main(String[] args) {
		String ret = sendSmsByWebService("xxx","尊敬的客户你好,您本次获取的验证码为:1234");
		System.out.println(ret);
	}
	/**
	 * 调用 WebService 协议方式发送短信
	 * 
	 * @param mobiles
	 * @param msg
	 * @return
	 */
	public static String sendSmsByWebService(String mobiles, String msg) {
		String result = "-12";
		try {
			Document doc;
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(true);
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputStream is = getSoapInputStream(userid, pass, mobiles, msg, "");
			if (is != null) {
				doc = db.parse(is);
				NodeList nl = doc.getElementsByTagName("SendMessagesResult");
				Node n = nl.item(0);
				result = n.getFirstChild().getNodeValue();
				is.close();
			}
			return result;
		} catch (Exception e) {
			System.out.print("SmsSoap.sendSms error:" + e.getMessage());
			return "-12";
		}
	}

	private static String getSoapSmssend(String userid, String pass,
			String mobiles, String msg, String time) {
		try {
			String soap = "";
			soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
					+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
					+ "<soap:Body>"
					+ "<SendMessages xmlns=\"http://tempuri.org/\">" + "<uid>"
					+ userid + "</uid>" + "<pwd>" + pass + "</pwd>" + "<tos>"
					+ mobiles + "</tos>" + "<msg>" + msg + "</msg>" + "<otime>"
					+ time + "</otime>" + "</SendMessages>" + "</soap:Body>"
					+ "</soap:Envelope>";
			return soap;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	private static InputStream getSoapInputStream(String userid, String pass,
			String mobiles, String msg, String time) throws Exception {
		URLConnection conn = null;
		InputStream is = null;
		try {
			String soap = getSoapSmssend(userid, pass, mobiles, msg, time);
			if (soap == null) {
				return null;
			}
			try {

				URL url = new URL("http://service2.winic.org:8003/Service.asmx");

				conn = url.openConnection();
				conn.setUseCaches(false);
				conn.setDoInput(true);
				conn.setDoOutput(true);
				conn.setRequestProperty("Content-Length",
						Integer.toString(soap.length()));
				conn.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				conn.setRequestProperty("HOST", "service2.winic.org");
				conn.setRequestProperty("SOAPAction",
						"\"http://tempuri.org/SendMessages\"");

				OutputStream os = conn.getOutputStream();
				OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
				osw.write(soap);
				osw.flush();
			} catch (Exception ex) {
				System.out.print("SmsSoap.openUrl error:" + ex.getMessage());
			}
			try {
				is = conn.getInputStream();
			} catch (Exception ex1) {
				System.out.print("SmsSoap.getUrl error:" + ex1.getMessage());
			}

			return is;
		} catch (Exception e) {
			System.out.print("SmsSoap.InputStream error:" + e.getMessage());
			return null;
		}
	}
	private static String userid = "czbk";
	private static String pass = "itcast2006";
}


3、客户注册页面js代码调整并提交表单

(1)、注册流程

        客户在页面输入手机号,点击获取验证码按钮

        验证手机号是否正确,如果正确,发送ajax请求,请求前台Action

        前台Action调用SmsUtils工具,通过WebService方式调用吉信通短信服务

        生成随机验证码,将随机验证码存入session中

        客户在注册页面输入收到的验证码,点击注册按钮,提交表单,请求前台Action

        在前台Action中调用CRM服务完成客户信息的保存操作

        为客户邮箱发送激活邮件,邮件中有一个激活码,将激活码存放到redis中,24小时失效

(2)、调整前台手机验证通过的js并发送请求


(3)、编写前台发送短信验证码的Action代码

package com.itheima.bos_fore.action.base;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
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.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.bos_fore.domain.Customer;
import com.itheima.bos_fore.service.CustomerService;
import com.itheima.bos_fore.utils.SmsUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 客户管理的Action
 * 
 * @author Administrator
 *
 */
@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	
	/**
	 * 调用吉信通服务,为客户发送短信
	 * @throws Exception 
	 */
	@Action(value="customerAction_sendMsg")
	public String sendMsg() throws Exception{
		//生成随机验证码
		String randomNumeric = RandomStringUtils.randomNumeric(4);
		System.out.println("生成的验证码为:" + randomNumeric);
		//将生成的验证码存入session中
		ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(), randomNumeric);
		//调用短信服务,发送短信
		String msg = "尊敬的客户你好,您本次的验证码为:" + randomNumeric;
		String ret = "";
		try {
			ret = SmsUtils.sendSmsByWebService(model.getTelephone(), msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String success = "true";
		if(StringUtils.isNotBlank(ret) && ret.length() == 16){
			//发送成功
		}else{
			//发送失败
			success = "false"; 
		}
		success = "true";
		//将结果写回浏览器
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(success);
		return NONE;
	}
	
	//使用模型驱动获取用户信息
	private Customer model = new Customer();
	
	public Customer getModel() {
		return model;
	}

4、实现用户注册功能

(1)、为注册按钮绑定点击事件


(2)、编写用户注册的Action方法

	//使用模型驱动获取用户信息
	private Customer model = new Customer();
	
	public Customer getModel() {
		return model;
	}
	
	//使用属性驱动获取页面传递的验证码
	private String checkcode;
	
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
	
	@Autowired
	private CustomerService service;

	/**
	 * 用户注册的功能
	 */
	@Action(value="customerAction_regist",results={
			@Result(name="success",type="redirect",location="/signup-success.html"),
			@Result(name="error",type="redirect",location="/signup-fail.html")
	})
	public String regist(){
		//从session中获取当前手机号的验证码
		String randomNumeric = (String) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());
		if(StringUtils.isNotBlank(randomNumeric) && StringUtils.isNotBlank(checkcode) && randomNumeric.equals(checkcode)){
			//验证成功,调用方法保存客户信息
			service.save(model);
			return SUCCESS;
		}else{
			//验证失败
			return ERROR;
		}
	}

(3)、编写crm项目中的Service方法

	/**
	 * 保存客户的方法
	 */
	public void save(Customer customer) {
		dao.save(customer);
	}

(4)、重新生成客户端代码

(5)、在spring配置文件中配置客户端调用

	<!-- 注册CRM服务的客户端代理对象,用于通过WebService访问CRM项目 -->
	<jaxws:client id="crmClient" address="http://localhost:8081/crm/webservice/customer"
				serviceClass="com.itheima.bos_fore.service.CustomerService">
	</jaxws:client>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值