Cordova+jQuery Mobile+Spring REST

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

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

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>

  <script>

        $.support.cors =true;
        $.mobile.allowCrossDomainPages=true;

        function onSuccess(data, status)
        {
            data = $.trim(data);
            $("#info").text(data);
        }

        function onError(data, status)
        {
            // handle an error
        }        

        $(document).ready(function() {
            $("#submit").click(function(){

                var formData = $("#callAjaxForm").serialize();

               $.ajax({
                    type: "POST",
                    url: "http://192.168.1.218:8080/SpringMVC/rest/add",
                    cache: false,
                    data: formData,
                    dataType : ‘text‘,
                    success: onSuccess,
                    error: onError
                });

                return false;
            });
        });

    </script>

<div data-role="page">
  <div data-role="header" data-position="fixed">
    <a href="#" data-role="button" data-icon="home">首页</a>
    <h1>欢迎访问我的主页</h1>
    <a href="#" data-role="button" data-icon="search">搜索</a>
  </div>

  <div data-role="content">
            <form id="callAjaxForm">
                <div data-role="fieldcontain">
                    <label for="name">name</label>
                    <input type="text" name="name" id="name" value=""  />

                    <label for="age">age</label>
                    <input type="text" name="age" id="age" value=""  />
                    <h3 id="info"></h3>
                    <button data-theme="b" id="submit" type="submit">Submit</button>
                </div>
            </form>
        </div>

  <div data-role="footer" data-position="fixed" >
  <div data-role="navbar" >
  <ul>
       <li><a href="#" data-icon="custom">功能1</a></li>
      <li><a href="#" data-icon="custom">功能2</a></li>
      <li><a href="#" data-icon="custom">功能3</a></li>
      <li><a href="#" data-icon="custom">功能4</a></li>
     </ul>
      </div>
  </div>
</div>

</body>
</html>

后台使用Spring REST:

package com.garfield.web.springmvc;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.garfield.pojo.User;

@Controller
@RequestMapping("/rest")
public class HelloWorldControllerAnnotation {

    @RequestMapping(value = "/helloworld")
    public ModelAndView helloWorld() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "Hello Garfield !");
        mv.setViewName("hello");
        return mv;
    }

    @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
    public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
        //request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");
        //return "index.jsp";

        User user=new User();
        user.setName("garfield");
        user.setAge("18");

        JSONObject jsonObject = JSONObject.fromObject(user);           

        response.getWriter().write(jsonObject.toString());
        return null;
    }

    @RequestMapping(value="/show",method=RequestMethod.GET)
    public String show(HttpServletRequest request ,HttpServletResponse response) throws IOException{
        response.getWriter().write("show users ....");
        return null;
    }

    @RequestMapping(value="/edit/{id}",method=RequestMethod.PUT)
    public String edit(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
        response.getWriter().write("edit user:"+id);
        return null;
    }

    @RequestMapping(value="/remove/{id}",method=RequestMethod.DELETE)
    public String remove(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
        response.getWriter().write("delete user:"+id);
        return null;
    }

    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(HttpServletRequest request ,HttpServletResponse response) throws IOException{
        User user = new User();
        user.setName(request.getParameter("name"));
        user.setAge(request.getParameter("age"));

        JSONObject jsonObject = JSONObject.fromObject(user);           

        response.getWriter().write(jsonObject.toString());

        return null;
    }

    //多参数传递
    @RequestMapping(value="/test/{id}/{sql}",method=RequestMethod.GET)
    public String test(@PathVariable String id,@PathVariable String sql,HttpServletRequest request ,HttpServletResponse response) throws IOException{
        response.getWriter().write("test,id:"+id+",sql:"+sql);
        return null;
    }

}

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

注意:

$.support.cors =true;
$.mobile.allowCrossDomainPages=true;

是为了跨域调用。

运行界面示意:

附: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">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-servlet.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<!--看到下面的beans这个元素标签没有,必须有标签的声明-->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    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.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-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/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 扫描定义的bean -->
    <context:component-scan base-package="com.garfield.web.springmvc" />

    <!-- 处理器 -->
    <bean name="/hello" class="com.garfield.web.springmvc.HelloWorldController"/>

    <!-- 下面的配置用于非注解 -->
    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!-- 注解配置 -->
    <bean     class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <!-- ViewResolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
				
时间: 2024-08-01 03:25:26

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

JQuery Mobile+cordova构建一个Android项目

1.安装Android开发环境 Android开发环境的安装,现在主要是由于不能访问谷歌站点,在windows下在host文件中添加一个对应的74.125.195.190 dl-ssl.google.com,dl-ssl.google.com对应的ip可能改变.用的时候搜索最新的IP地址就行了. 然后用eclipse或者myeclipse更新站点信息:https://dl-ssl.google.com/android/eclipse. 具体教程网上很多,我比较喜欢的是:http://jingya

课程分享】基于Springmvc+Spring+Mybatis+Bootstrap+jQuery Mobile +MySql教务管理系统

课程讲师:老牛 课程分类:Java框架 适合人群:初级 课时数量:85课时 更新程度:完成 用到技术:Springmvc+Spring+Mybatis+Bootstrap+jQueryMobile 涉及项目:PC端和手机端教务管理系统 需要更多相关资料可以联系 Q2748165793 课程大纲 技能储备 第1课springMVC概述和基础配置 第2课springMVC注解和参数传递 第3课springMVC和JSON数据 第4课springMVC上传下载 第5课springMVC 与 sprin

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

PhoneGap与Jquery Mobile结合开发android应用的配置 由于工作需要,用到phonegap与jquery moblie搭配,开发android应用程序. 这些技术自己之前也都没接触过,可以说是压根没听说过,真是感慨,在开发领域,技术日新月异,知识真是永远学不完的.就算是做java开发的,可是细分下来,专业方向还是很多的:自己没有涉及的技术还是太多了,自个需要对单个领域专业点,知识丰富点.做不了全才,那咱做个专才,在如今社会还是必须的. 好了,咱们言归正传: PhoneGap

Java企业通用平台框架:Bootstrap、HTML5、jQuery、Spring MVC、Mybatis、Hibernate、高性能、高并发

1.适配所有设备(PC.平板.手机等),兼容所有浏览器(Chrome.Firefox.Opera.Safari.IE6~IE11等),适用所有项目(MIS管理信息系统.OA办公系统.ERP企业资源规划系统.CRM客户关系管理系统.网站等). 2.快速开发,敏捷的数据持久层解决方案. 2.1.事务自动处理. 2.2.O/R Mapping基于注解,零配置XML,便于维护,学习成本低. 2.3.接口和实现分离,不需写数据持久层代码,只需写接口,自动生成添加.修改.删除.排序.分页.各种条件的查询等S

JQuery Mobile 实战一

今天我们来使用JQuery Mobile来开发一个web mobile app. 要实现的如下所示效果: 开始: 第一步:添加JS包等引用,直接去官网下载最新的JQuery Mobile 包,http://jquerymobile.com/:或者直接从CDN引用JQuery Mobile. 解压压缩包:拷贝 jquery.min.js.jquery.mobile-1.4.5.css.jquery.mobile-1.4.5.js 文件到项目中. 第二步:新建一个 html 页面.添加上面三个文件的

Spring MVC、Mybatis、Hibernate、Bootstrap、HTML5、jQuery、Spring Security安全权限、Lucene全文检索、Ehcache分布式缓存 、高性能、高并发【Java企业通用开发平台框架】

功能特点: 1.适配所有设备(PC.平板.手机等),兼容所有浏览器(Chrome.Firefox.Opera.Safari.IE6~IE11等),适用所有项目(MIS管理信息系统.OA办公系统.ERP企业资源规划系统.CRM客户关系管理系统.网站.管理后台等). 2.快速开发,敏捷的数据持久层解决方案. 2.1.事务自动处理. 2.2.O/R Mapping基于注解,零配置XML,便于维护,学习成本低. 2.3.接口和实现分离,不需写数据持久层代码,只需写接口,自动生成添加.修改.删除.排序.分

JQuery Mobile - 为什么绑定事件后会被多次执行?

JQuery Mobile 在绑定事件时候,发现会被多次执行,为什么啊? 原来,jquery click  不是替换原有的function ,而是接着添加,所以才会执行次数越来越多,怎么办才能按需实现功能?在执行正常点击事件之前,解绑事件!! JQuery对事件的绑定主要有两种方式,分别是on和bind,这两种方式分别对应的解绑方式为off和unbind,知道这些,我们就可以写代码了: 一,用on和off // off和on绑定"tap"方法 $("#changePasswo

jQuery Mobile中表单的使用体会

jQuery Mobile是手机端(移动端)页面制作用的框架,包括CSS和JavaScript,此处简单总结一下表单的书写,主要涉及CSS部分.框架提供了表单的一些样式,但在实际使用的时候,我们可能会用自己的自定义样式,这种情况下,框架提供的样式可能就不能满足我们的要求.今天项目中写登录页面的静态网页,碰到了几个问题,在这里和大家交流一下. 1 利用data-role="none" 在使用表单的时候,如果想使用自定义样式,就可以表单元素上给data-role属性赋值none,意思就是不

史上最简单的个人移动APP开发入门--jQuery Mobile版跨平台APP开发

书是人类进步的阶梯. ——高尔基 习大大要求新新人类要有中国梦,鼓励大学生们一毕业就创业.那最好的创业途径是什么呢?就是APP.<构建跨平台APP-jQuery Mobile移动应用实战>就是一本写给没钱没身份没资历的创业小白看的APP书,看完这本书你可以拥有自己的一个APP,不用花钱就能移植到其他移动平台,支持iOS,Android,Windows Phone!!!!!!!!找个最便宜的来练手吧!  小白APP交流Q群:  348632872 清华大学出版社推出的<构建跨平台APP:j