Cordova可以方便地建立跨平台的移动应用,使用jQuery Mobile做手机界面,后台使用rest提供数据交互。

首先,使用jQuery Mobile建立一个页面:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  5. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  6. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  7. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  8. </head>
  9. <body>
  10.  
  11. <script>
  12.  
  13. $.support.cors =true;
  14. $.mobile.allowCrossDomainPages=true;
  15.  
  16. function onSuccess(data, status)
  17. {
  18. data = $.trim(data);
  19. $("#info").text(data);
  20. }
  21.  
  22. function onError(data, status)
  23. {
  24. // handle an error
  25. }
  26.  
  27. $(document).ready(function() {
  28. $("#submit").click(function(){
  29.  
  30. var formData = $("#callAjaxForm").serialize();
  31.  
  32. $.ajax({
  33. type: "POST",
  34. url: "http://192.168.1.218:8080/SpringMVC/rest/add",
  35. cache: false,
  36. data: formData,
  37. dataType : 'text',
  38. success: onSuccess,
  39. error: onError
  40. });
  41.  
  42. return false;
  43. });
  44. });
  45.  
  46. </script>
  47.  
  48. <div data-role="page">
  49. <div data-role="header" data-position="fixed">
  50. <a href="#" data-role="button" data-icon="home">首页</a>
  51. <h1>欢迎访问我的主页</h1>
  52. <a href="#" data-role="button" data-icon="search">搜索</a>
  53. </div>
  54.  
  55. <div data-role="content">
  56. <form id="callAjaxForm">
  57. <div data-role="fieldcontain">
  58. <label for="name">name</label>
  59. <input type="text" name="name" id="name" value="" />
  60.  
  61. <label for="age">age</label>
  62. <input type="text" name="age" id="age" value="" />
  63. <h3 id="info"></h3>
  64. <button data-theme="b" id="submit" type="submit">Submit</button>
  65. </div>
  66. </form>
  67. </div>
  68.  
  69. <div data-role="footer" data-position="fixed" >
  70. <div data-role="navbar" >
  71. <ul>
  72. <li><a href="#" data-icon="custom">功能1</a></li>
  73. <li><a href="#" data-icon="custom">功能2</a></li>
  74. <li><a href="#" data-icon="custom">功能3</a></li>
  75. <li><a href="#" data-icon="custom">功能4</a></li>
  76. </ul>
  77. </div>
  78. </div>
  79. </div>
  80.  
  81. </body>
  82. </html>

后台使用Spring REST:

  1. package com.garfield.web.springmvc;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. import net.sf.json.JSONObject;
  9.  
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.PathVariable;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestMethod;
  14. import org.springframework.web.servlet.ModelAndView;
  15.  
  16. import com.garfield.pojo.User;
  17.  
  18. @Controller
  19. @RequestMapping("/rest")
  20. public class HelloWorldControllerAnnotation {
  21.  
  22. @RequestMapping(value = "/helloworld")
  23. public ModelAndView helloWorld() {
  24. ModelAndView mv = new ModelAndView();
  25. mv.addObject("message", "Hello Garfield !");
  26. mv.setViewName("hello");
  27. return mv;
  28. }
  29.  
  30. @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
  31. public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  32. //request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");
  33. //return "index.jsp";
  34.  
  35. User user=new User();
  36. user.setName("garfield");
  37. user.setAge("18");
  38.  
  39. JSONObject jsonObject = JSONObject.fromObject(user);
  40.  
  41. response.getWriter().write(jsonObject.toString());
  42. return null;
  43. }
  44.  
  45. @RequestMapping(value="/show",method=RequestMethod.GET)
  46. public String show(HttpServletRequest request ,HttpServletResponse response) throws IOException{
  47. response.getWriter().write("show users ....");
  48. return null;
  49. }
  50.  
  51. @RequestMapping(value="/edit/{id}",method=RequestMethod.PUT)
  52. public String edit(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  53. response.getWriter().write("edit user:"+id);
  54. return null;
  55. }
  56.  
  57. @RequestMapping(value="/remove/{id}",method=RequestMethod.DELETE)
  58. public String remove(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  59. response.getWriter().write("delete user:"+id);
  60. return null;
  61. }
  62.  
  63. @RequestMapping(value="/add",method=RequestMethod.POST)
  64. public String add(HttpServletRequest request ,HttpServletResponse response) throws IOException{
  65. User user = new User();
  66. user.setName(request.getParameter("name"));
  67. user.setAge(request.getParameter("age"));
  68.  
  69. JSONObject jsonObject = JSONObject.fromObject(user);
  70.  
  71. response.getWriter().write(jsonObject.toString());
  72.  
  73. return null;
  74. }
  75.  
  76. //多参数传递
  77. @RequestMapping(value="/test/{id}/{sql}",method=RequestMethod.GET)
  78. public String test(@PathVariable String id,@PathVariable String sql,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  79. response.getWriter().write("test,id:"+id+",sql:"+sql);
  80. return null;
  81. }
  82.  
  83. }

然后就可以和后台进行数据交互了。

注意:

  1. $.support.cors =true;
  2. $.mobile.allowCrossDomainPages=true;
  3.  
  4. 是为了跨域调用。
  5.  
  6. 运行界面示意:

附:web.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <servlet>
  7. <servlet-name>springmvc</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>springmvc</servlet-name>
  13. <url-pattern>/</url-pattern>
  14. </servlet-mapping>
  15. </web-app>

springmvc-servlet.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--看到下面的beans这个元素标签没有,必须有标签的声明-->
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  15.  
  16. <!-- 扫描定义的bean -->
  17. <context:component-scan base-package="com.garfield.web.springmvc" />
  18.  
  19. <!-- 处理器 -->
  20. <bean name="/hello" class="com.garfield.web.springmvc.HelloWorldController"/>
  21.  
  22. <!-- 下面的配置用于非注解 -->
  23. <!-- HandlerMapping -->
  24. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
  25. <!-- HandlerAdapter -->
  26. <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
  27.  
  28. <!-- 注解配置 -->
  29. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  30. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
  31.  
  32. <!-- ViewResolver -->
  33. <bean
  34. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  35. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  36. <property name="prefix" value="/WEB-INF/jsp/" />
  37. <property name="suffix" value=".jsp" />
  38. </bean>
  39.  
  40. </beans>
  1.  

Cordova+jQuery Mobile+Spring REST的更多相关文章

  1. JQuery Mobile + Cordova 实战一

    好的,今天给大家继续讲解 JQM 和 Cordova 的结合吧.Cordova 和 Phonegap 反正是一个东西,大家就当做一个是旧版(Phonegap)的一个是新版(Cordova)的就行.不同 ...

  2. JQuery Mobile+cordova构建一个Android项目

    1.安装Android开发环境     Android开发环境的安装,现在主要是由于不能访问谷歌站点,在windows下在host文件中添加一个对应的74.125.195.190 dl-ssl.goo ...

  3. jQuery Mobile学习日记之HelloWorld

    这里是本人学习jQuery Mobile的过程,主要用于记录,过程中有不对的地方或不严谨的地方,欢予以指出纠正,非常感谢! 1.首先是下载安装以下文件: [Opera Mobile Emulator] ...

  4. PhoneGap与Jquery Mobile组合开发android应用的配置

    PhoneGap与Jquery Mobile结合开发android应用的配置 由于工作需要,用到phonegap与jquery moblie搭配,开发android应用程序. 这些技术自己之前也都没接 ...

  5. 小白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连载四(场景切换)

    作为一款真正有使用价值的应用,首先应该至少有两个页面,通过页面的切换来实现更多的交互.比如手机人人网,打开以后先是进入登录页面,登录后会有新鲜事,然后拉开左边的面板,能看到相册.悄悄话.应用之类的其他 ...

  6. PhoneGap与Jquery Mobile结合开发android应用配置

    由于工作需要,用到phonegap与jquery moblie搭配,开发android应用程序. 这些技术自己之前也都没接触过,可以说是压根没听说过,真是感慨,在开发领域,技术日新月异,知识真是永远学 ...

  7. jQuery Mobile和PhoneGap混合开发

    其实二者并不影响,PhoneGap负责调用系统的接口,jQuery Mobile实现一些网页效果.PhoneGap开发请看上一篇文章,jQuery Mobile开发环境搭建如下:[请先阅读上一篇文章, ...

  8. PhoneGap+jQuery Mobile+Rest 访问远程数据

    最近研究Mobile Web技术.发现了一个好东西-PhoneGap! 发现用PhoneGap+jQuery Mobile是一个很完美的组合! 本实例通俗易懂.适合广大开发人群:高富帅.白富美.矮穷戳 ...

  9. JQuery Mobile 实战一

    今天我们来使用JQuery Mobile来开发一个web mobile app. 要实现的如下所示效果: 开始: 第一步:添加JS包等引用,直接去官网下载最新的JQuery Mobile 包,http ...

随机推荐

  1. 命令行修改mysql密码和远程访问

    http://jingyan.baidu.com/article/a3a3f8118cea488da2eb8a0a.html

  2. jedis 连接 redis:Could not get a resource from the pool——我的出错原因和解决办法

    windows 下安装的,本机使用 现象:刚装好开发使用好好的, 重启电脑后就报这个错 网上的所有可能都试过,没有用. 最后,放弃所有包装,用最原始的代码进行连接测试: Jedis jedis=new ...

  3. SQL-根据多个条件更新数据

    根据多个条件更新数据 UPDATE sphwph SET BKXSHL=t2.BKXSHL FROM sphwph t1,sphwph_170420 t2 --(SELECT a.* FROM dbo ...

  4. 如何屏蔽ctrl + v 粘贴事件,鼠标右键粘贴事件

    通常在自己的APP里的密码框,验证码框需要屏蔽复制,粘贴,怎么办呢? 有三种方法: 1 hook 此方法是最完全的,但由于hook是全局的,容易影响到其它代码. 2 子类化文本框, 重写OnPaste ...

  5. 通读cheerio API

    所谓工欲善其事,必先利其器,所以通读了cheerio的API,顺便翻译了一遍,有些地方因为知道的比较少,不知道什么意思,保留了英文,希望各位不吝告诉我,然后一起把这个翻译完成. ###cheerio ...

  6. GPUImage简单滤镜使用(二)

    GPUImage中,提供了许多简单的的常用的滤镜.在上一篇文章讲了如何调节图像的亮度这片文章讲一下如何通过GPUImage调节图像的对比度,饱和度,曝光度,和白平衡(美图秀秀中的色温). 原图像 调整 ...

  7. 【nodejs】理想论坛帖子下载爬虫1.08

    //====================================================== // 理想论坛帖子下载爬虫1.09 // 使用断点续传模式,因为网络传输会因各种原因中 ...

  8. Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表

    本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...

  9. MySQL常用处理方法

    1.replace into replace into t(id, update_time) values(1, now());或 replace into t(id, update_time) se ...

  10. Mybatis源码分析之Cache二级缓存原理 (五)

    一:Cache类的介绍 讲解缓存之前我们需要先了解一下Cache接口以及实现MyBatis定义了一个org.apache.ibatis.cache.Cache接口作为其Cache提供者的SPI(Ser ...