15_CXF和Spring开发手机号查询网站
【整体分析】

【生成客户端代码】
wsdl网址: http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

生成的客户端代码

【工程截图(已拷入客户端生成代码)】

【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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- 公网手机号查询客户端
正式开发时将address在单独的配置文件配置
serviceClass:portType的类路径
id:spring容器中bean的id
-->
<jaxws:client id="mobileClient"
address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
serviceClass="cn.com.webxml.MobileCodeWSSoap">
</jaxws:client> <!-- 配置本系统的手机号码查询service -->
<bean id="mobileService" class="cn.Higgin.ws.mobile.service.MobileServiceImpl">
<!-- 注入查询公网手机号的客户端bean -->
<property name="mobileClient" ref="mobileClient" />
</bean> <!-- 发布手机号查询服务 -->
<jaxws:server address="/mobile" serviceClass="cn.Higgin.ws.mobile.service.MobileService">
<jaxws:serviceBean>
<ref bean="mobileService"/>
</jaxws:serviceBean>
</jaxws:server> </beans>
关系:

注意:

其中的红框部分的address代码在 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL 找到:

而serviceClass则是在导入的客户端代码的MobileCodeWSSoap接口中:

【MobileService.java】(即SEI)
package cn.Higgin.ws.mobile.service; import javax.jws.WebService;
/**SEI
* 手机号查询的Service
*/
@WebService
public interface MobileService { public String queryMobile(String code); }
【MobileServiceImpl.java】
package cn.Higgin.ws.mobile.service; import cn.com.webxml.MobileCodeWSSoap; /**
* SEI实现类
*/
public class MobileServiceImpl implements MobileService{ //注入 查询公网手机号的客户端bean(该bean在spring配置文件配置,通过spring注入)
private MobileCodeWSSoap mobileClient; @Override
public String queryMobile(String code) {
/*
* 调用公网的WebService
* 第一个参数:手机号码至少前7位
* 第二个参数:用户id,非商业用户为空字符串
*/
return mobileClient.getMobileCodeInfo(code, "");
}
//get/Set方法
public MobileCodeWSSoap getMobileClient() {
return mobileClient;
} public void setMobileClient(MobileCodeWSSoap mobileClient) {
this.mobileClient = mobileClient;
} }
【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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebService_Spring_Mobile_Server</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容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- cxf的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 本系统webservice的路径必须以/ws/开头 -->
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping> <!-- post乱码处理 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation指定 springmvc的全局配置文件 如果 contextConfigLocation不指定,默认找配置文件名称:servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> </web-app>
【springmvc.xml】
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- 组件扫描 只扫描action -->
<context:component-scan base-package="cn.Higgin.ws.mobile.action" /> <!-- 实现了处理器映射器和适配器 -->
<mvc:annotation-driven /> <!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图的后缀 -->
<property name="suffix" value=".jsp" />
</bean> </beans>
【MobileAction.java】
package cn.Higgin.ws.mobile.action; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import cn.Higgin.ws.mobile.service.MobileService; @Controller
public class MobileAction { @Autowired
private MobileService mobileService; @RequestMapping("/queryMobile")
public String queryMobile(Model model,String code) throws Exception{
//调用service查询手机号
String result=null;
if(code!=null && !code.trim().equals("")){
result =mobileService.queryMobile(code);
}
model.addAttribute("result",result); //返回逻辑视图名
return "queryMobile";
}
}
【queryMobile.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="queryMobile.action" method="post">
手机号归属地查询:<input type="text" name="code" />
<br/>
查询结果:${result}
<br/>
<input type="submit" value="查询手机号归属地"/>
</form>
</body>
</html>
【运行结果】

输入任意正确格式的手机号码,即可查询出对应的查询结果。
15_CXF和Spring开发手机号查询网站的更多相关文章
- spring boot之从零开始开发自己的网站
概述 首先要感谢两位大神,该项目的想法来源自tale和MyBlog,本项目的想法. 做了一些改造,增加了一些功能和一些代码的重构,并且更换了博客主题. 关于项目,对于开发的练手项目,能够工程化,严谨一 ...
- Spring开发环境搭建教程
Spring开发环境搭建 JDK7以上版本 eclispe for j2ee 4.0以上版本 Spring frameWorks 3.0以上版本 至于前两个我们就不介绍,直接百度就可以了,对于Spri ...
- 搭建Spring开发环境并编写第一个Spring小程序
搭建Spring开发环境并编写第一个Spring小程序 2015-05-27 0个评论 来源:茕夜 收藏 我要投稿 一.前面,我写了一篇Spring框架的基础知识文章,里面没 ...
- 简要描述如何结合struts、hibernate、spring开发Web应用?
简要描述如何结合struts.hibernate.spring开发Web应用? 解答:Struts可以将jsp页面的表单关联起来,就是把JSP页面的表单数据封装成javaBean,这样的话,在acti ...
- 学习spring2--跟我一起学Spring 3(3)–使用Spring开发第一个HelloWorld应用
http://www.importnew.com/13246.html 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 ...
- mybatis 高级映射和spring整合之查询缓存(5)
mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...
- Log2Net日志查询网站代码解析
在前面的几节中,我们介绍了Log2Net的使用方法和代码设计.使用这个组件,我们可以方便地将日志记录到数据库中,那么,我们怎么能看到这些日志呢?于是,日志查询网站应运而生.效果图如下: 该代码已开源, ...
- Django高级实战 开发企业级问答网站 ✌✌
Django高级实战 开发企业级问答网站 (一个人学习或许会很枯燥,但是寻找更多志同道合的朋友一起,学习将会变得更加有意义✌✌) 从实际需求分析开始,实现当今主流知识问答应用的功能,包括动态.文章.问 ...
- 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)
移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...
随机推荐
- 【Stage3D学习笔记续】山寨Starling(一):从事件说起
我在GitHub上新开了一个项目:https://github.com/hammerc/hammerc-study-Stage3D 山寨的Starling版本我取名叫做Scorpio2D,以后的笔记中 ...
- matlab s变换
A4=readdata('E:\mydata.TXT');[st,t,f] = st(A4(1:1000,2)); surf(t,f,10*log10(abs(st)),'EdgeColor','no ...
- Hibernate的BaseDao辅助类
1.BaseDao接口类,该类封装了一些hibernate操作数据库的一些常用的方法,包括分页查询,使用该类极大的简化了hibernate的开发 BaseDao.java package com.kj ...
- HDU 3157 Crazy Circuits(有源汇上下界最小流)
HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...
- java_spring_依赖注入(构造器)
依赖注入对象可以 手工装配(建议) 和 自动装配 package com.PersonDaoBean.test; public interface PersonDao { public abstrac ...
- Helpers\Date
Helpers\Date The Date helper is used for calculations with dates. Date::difference($from, $to, $type ...
- python--判断数据类型可不可变
内存是一块空间,可以比喻成一个比较大的房子,定义一个变量就是在大房子中建立一个小房子,判断一个数据类型可不可变,就是看在这个这个大房子中有没有新建小房子,可以通过id来判断,如果id没有变化则是不可变 ...
- iOS-label出现未知边框线的bug
在前段时间碰到了一个问题 label上出现了一个位置的右边框 仔细查看代码发现没有指定边框 而且奇怪的是只显示右边框 其他边框没有显示 需求效果图: 实际效果图: 结构图: 通过查看结构图 可 ...
- jquery中的index方法
问题描述:灵活使用jquery中的index方法 方法签名:index([selector|element]) 用法概述:P1.index(P2) //调用者P1可以为对象或集合 参数为空,返回P1 ...
- centos 7访问windows共享文件夹
1. 首先centos要能识别win7的文件系统ntfs,原版的centos是不支持NTFS格式的文件系统,因此需要安装ntfs支持软件包,我使用的是rpmforge软件库,在此处http://pkg ...