写在前面

  • 自己也是一个java和java web的菜鸟。之前没有接触过java web方面的开发
  • 想通过一个小项目,来熟悉struts2的开发流程
  • 一个有趣的想法源于教研室项目上的一个功能实现–自己主动识别运营商,去做不同的处理。项目上採用的是IP地址库的方式,在本地做处理。这里为了简单就採用了淘宝提供的接口服务
  • 已经将该项目作为开源项目放在:IP地址仓库 欢迎大家前来点赞

能够学到什么

  • struts2的基本执行流程
  • HttpClient和org.json库的使用
  • 前端採用了bootstrap和ajax,做到了网页的自适应,后端返回json数据和前端交互

步入正题

  • struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- 设置常量实现动态调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 名字必须唯一相应模块 -->
<package name="hello" extends="json-default">
<default-action-ref name="index" />
<action name="index" class="action.IPSearchAction" method="index">
<!--为了安全将其放在、WEB-INF/pages 不明确的也可放在webRoot 根文件夹下-->
<result name="success">/WEB-INF/pages/ip.jsp </result>
</action> <action name="search" class="action.IPSearchAction" method="search">
<!--为了安全将其放在、WEB-INF/pages 不明确的也可放在webRoot 根文件夹下-->
<result name="success" type="json">
<param name="root">responseMap</param>
</result>
</action>
</package> </struts>
  • 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<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>
<welcome-file-list>
<welcome-file>/WEB-INF/pages/ip.jsp</welcome-file>
</welcome-file-list>
</web-app>
  • action文件
package action;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject; public class IPSearchAction { String ip; Map<String, Object> responseMap; //查询的结果为json数据。struts2自己主动做序列化的工作 public Map<String, Object> getResponseMap() {
return responseMap;
} public void setResponseMap(Map<String, Object> responseMap) {
this.responseMap = responseMap;
} public String getIp() {
return ip;
} public void setIp(String ip) {
System.out.println("input ip is: "+ip);
this.ip = ip;
} public String search(){
setOutputValue();
return "success";
} public String index(){
return "success";
} public void setOutputValue() {
HttpClient httpclient = HttpClients.createDefault();
System.out.println("the input ip is" + ip);
URI uri = null;
try {
uri = new URIBuilder()
.setScheme("http")
.setHost("ip.taobao.com")
.setPath("/service/getIpInfo.php")
.setParameter("ip", ip)
.build();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpGet httpget = new HttpGet(uri); HttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK) { //状态==200,返回成功
String result = null;
try {
result = EntityUtils.toString(response.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
JSONObject resultJson = new JSONObject(result);
int code = resultJson.getInt("code");
String country = null;
String region = null;
String city = null;
String county = null;
String isp = null;
if(code == 0) {
country = resultJson.getJSONObject("data").getString("country");
region = resultJson.getJSONObject("data").getString("region");
city = resultJson.getJSONObject("data").getString("city");
county = resultJson.getJSONObject("data").getString("county");
isp = resultJson.getJSONObject("data").getString("isp");
System.out.println("code is: "+ code + "country is: " + country + "area is "+region+"county is "+county+
"isp is "+isp);
} responseMap = new HashMap<String, Object>();
responseMap.clear();
responseMap.put("country", country);
responseMap.put("region", region);
responseMap.put("city", city);
responseMap.put("county", county);
responseMap.put("isp", isp); }
}
}
  • 依赖的库文件



    说明:

    • project依赖org.json库,採用的是:org.json ,下载的是源码,能够打包成json.jar,更方便的使用
    • project依赖httpclient,下载地址:httpclient ,使用能够查看它提供的手冊
    • project还依赖struts2提供的某些jar包,记得加入

终于效果



PS:

一些问题记录:

參考资源

一天教你入门struts2的更多相关文章

  1. 入门struts2.0

    框架是什么? 1.应用程序的半成品. 2.可重用行公共的结构. 3.按一定规则组织的一组组件. model2 其实并不是一种全新的概念,很对人指出model2其实正好是经典的"模型(mode ...

  2. ●杜教筛入门(BZOJ 3944 Sum)

    入门杜教筛啦. http://blog.csdn.net/skywalkert/article/details/50500009(好文!) 可以在$O(N^{\frac{2}{3}})或O(N^{\f ...

  3. Git从零教你入门(4):Git服务之 gogs部署安装

    Git从零入门系列4: 先看上一篇文章: http://www.51testing.com/index.php?uid-497177-action-viewspace-itemid-3706817 今 ...

  4. 入职第一天:前端leader手把手教我入门Vue服务器端渲染(SSR)

    继前段时间西安电面之后顺利拿到了OFFER,今天(5月2号)是我入职第一天,在简短的内部培训了一上午后,前端leader让我先了解下什么是vue的服务器端渲染(SSR). SSR,英文全称叫 Serv ...

  5. struts2系列(一):struts2入门(struts2的产生、struts2的工作流程、搭建struts2开发环境)

    一. struts2的产生 struts1的缺点:                         1. ActionForm过多,而且这个ActionForm在很大程度上又和VO(POJO)重复  ...

  6. web 08 struts2入门 struts2配置 struts包

    电影网站:www.aikan66.com 项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.co ...

  7. 阿里云学生机——Mysql配置---教小白入门篇

    首先,我的学生机默认配置为:CentOS 7.2 64位 + Tomcat 8 + Jdk8 + MySQL5.7.16 扩展:Linux 如何查看 MySQL 版本号----使用命令 mysql - ...

  8. SVN入门-2分钟教你入门

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010540106/article/details/37317201   学习SVN首先我们应该知道 ...

  9. 手把手教你入门Yii2框架-1

    前言概述: 我是一名PHP开发工程师,最拿手的是版本2.0的Yii框架,在培训班里老师没教我Yii框架,只是由于我弟弟(同行)擅长Yii框架,所以我用得最多的就是Yii2.0,后台我学了ThinkPH ...

随机推荐

  1. 【转】C# WinForm中的Label如何换行

    第一种是把Label的AutoSize属性设为False,手动修改Label的大小.这样的好处是会因内容的长度而自动换行,但是当内容的长度超过所设定的大小时,多出的内容就会无法显示.因此,这种方法适合 ...

  2. ECSHOP二次开发文档【文件结构说明和数据库表分析】

    最近公司又把之前的ecshop进行二次开发,之前看过一些ecshop的东西,但是都过了很久差不多都忘完了,现在得把之前的重新捡回来,特此搜到这些文档,接下来的开发工作中会方便不少. Ecshop文件结 ...

  3. (12) OpenSSL主配置文件openssl.cnf

    1.man config 该帮助文档说明了openssl.cnf以及一些其他辅助配置文件的规范.格式及读取方式.后文中的所有解释除非特别指明,都将以openssl.cnf为例. [root@local ...

  4. RPM Package Manager

    本文大部分内容来自鸟哥的Linux私房菜,并且由作者根据自己的学习情况做了一些更改,鸟哥原文链接 1. 程序的安装方式 源代码安装:利用厂商释出的Tarball 来进行软件的安装,每次安装程序都需要检 ...

  5. 关于nagios系统下使用shell脚本自定义监控插件的编写以及没有实时监控图的问题

    关于nagios系统下shell自定义监控插件的编写.脚本规范以及没有实时监控图的问题的解决办法 在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主 ...

  6. ubuntu 宝塔安装一条龙服务

    ubuntu 安装 1, wget -O install.sh http://download.bt.cn/install/install-ubuntu.sh && sudo bash ...

  7. 关于在Safari浏览器中将网页添加到主屏幕的相关设置(自定义图标,启动动画,自定义名称)

    在ios中我们可以使用Safari浏览自带的将网页添加到主屏幕上,让我们的web页面看起来像native那样 第一步: 第二步: 第三步: 到这里还没结束:我们还要进行相关设置才能使我们的应用更像原生 ...

  8. 为什么在属性中设置private set

    引言: 属性的引入来自C#的封装机制,也就是说对象的内部数据不应该由对象实例来直接访问,我们可以使用传统的Get和Set方法,来封装字段,C#为我们提供了语法糖,也就是属性.属性包括get和set,分 ...

  9. Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape

     Android ShapeDrawable之OvalShape.RectShape.PaintDrawable.ArcShape Android图形图像基础之OvalShape.RectShap ...

  10. HDU 1874 最直接的最短路径问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...