您的位置:首页 > 运维架构 > Linux

Linux应用程序开发(二)---让PHP5支持java在arm linux运行

2012-02-09 00:12 543 查看

Linux应用程序开发(二)---让PHP5支持java在arm linux运行

移植环境(红色粗字体字为修改后内容,蓝色粗体字为特别注意内容)

1,主机环境:VMare下CentOS 5.5 ,1G内存。

2,集成开发环境:Elipse IDE

3,编译编译环境:arm-linux-gcc v4.4.3,arm-none-linux-gnueabi-gcc v4.5.1。

4,开发板:mini2440,2M nor flash,128M nand flash。

5,u-boot版本:u-boot-2009.08

6,linux 版本:linux-2.6.32.2

7,参考文章:

大道PHP:LAMP+Zend+开源框架整合开发与实践

前面已经介绍了thttpd+Sqlite3+PHP5到arm linux的移植过程,实现了php脚本文件能够在web 服务器上运行并能够访问后台数据库。现在这个基于php的嵌入式web服务器已经能够运行java脚本文件,而且也能够执行xajax运行库。下面就php这两方面的功能做下测试。

1,在php下运行javascript脚本测试

【1】在web服务器脚本目录下新建文件test_javascript.php,内容如下:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK" />

<title>JavaScript Hello World</title>

<!--引用JavaScript-->

<script type="text/javascript" src="sayHello.js"></script>

</head>

<body>

<!--定义文本框-->

<div align="center">用户名:<input type="text" name="user"/></div>

<!--定义两个按钮二者分别调用同一个JavaScript函数,但是参数不同-->

<div align="center">

<button onclick="check(1)">say hello</button>

<button onclick="check(2)">SAY HELLO</button>

</div>

</body>

</html>

【2】在web服务器脚本目录下和test_javascript.php同一个目录下新建文件sayhello.js,内容如下:

function helloWorld() {

var hello="hello "+document.getElementById("user").value;

alert(hello.toLowerCase());

}

function bigHelloWorld(){

var hello="hello "+document.getElementById("user").value;

alert(hello.toUpperCase());

}

function check(big){

var user=document.getElementById("user").value;

if(user==""){

alert("请输入用户名");

}else{

switch(big){

case 1:helloWorld();

break;

case 2:bigHelloWorld();

break;

default:alert("出错");

}

}

}

【3】在另一台主机上运行脚本test_javascript.php,显示如下:



点击say hello按钮,显示:



说明,上面的测试是在IE浏览器下测试的,在firefox浏览器中输入用户名的对话框可能会弹不出来。

2,在php下加入xajax运行库

【1】下载并解压xajx

xajax_0.5_minimal : http://www.xajax-project.org/en/download/
[root@localhost ~]# cd /nfsboot/rootfs/home/www/html

[root@localhost html]# unzip /root/linux-test/xajax_0.5_minimal.zip -d ./xajax

注意:需要确保运行脚本和xajax库在同一目录下,即将xajax库放置在脚本运行目录开发板/home/www/html下,不然会显示下面错误:

Error: the xajax Javascript component could not be included.
Perhaps the URL is incorrect?

URL: ../xajax/xajax_js/xajax_core.js

【2】运行测试,下面是一段helloworld.php测试程序,代码如下:

<?php

/*

File: helloworld.php

Test / example page demonstrating the basic xajax implementation.

Title: Hello world sample page.

Please see <copyright.inc.php> for a detailed description, copyright

and license information.

*/

/*

@package xajax

@version $Id: helloworld.php 362 2007-05-29 15:32:24Z calltoconstruct $

@copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson

@license http://www.xajaxproject.org/bsd_license.txt BSD License

*/

/*

Section: Standard xajax startup

- include <xajax.inc.php>

- instantiate main <xajax> object

*/

require ('xajax/xajax_core/xajax.inc.php');

$xajax = new xajax();

/*

- enable deubgging if desired

- set the javascript uri (location of xajax js files)

*/

//$xajax->configure('debug', true);

//$xajax->configure('javascript URI', '../');

$xajax->configure('javascript URI', 'xajax/');

/*

Function: helloWorld

Modify the innerHTML of div1.

*/

function helloWorld($isCaps)

{

if ($isCaps)

$text = 'HELLO WORLD!';

else

$text = 'Hello World!';

$objResponse = new xajaxResponse();

$objResponse->assign('div1', 'innerHTML', $text);

return $objResponse;

}

/*

Function: setColor

Modify the style.color of div1

*/

function setColor($sColor)

{

$objResponse = new xajaxResponse();

$objResponse->assign('div1', 'style.color', $sColor);

return $objResponse;

}

/*

Section: Register functions

- <helloWorld>

- <setColor>

*/

$reqHelloWorldMixed =& $xajax->registerFunction('helloWorld');

$reqHelloWorldMixed->setParameter(0, XAJAX_JS_VALUE, 0);

$reqHelloWorldAllCaps =& $xajax->registerFunction('helloWorld');

$reqHelloWorldAllCaps->setParameter(0, XAJAX_JS_VALUE, 1);

$reqSetColor =& $xajax->registerFunction('setColor');

$reqSetColor->setParameter(0, XAJAX_INPUT_VALUE, 'colorselect');

/*

Section: processRequest

This will detect an incoming xajax request, process it and exit. If this is not a xajax request, then it is a request to load the initial contents of the page(HTML).

Everything prior to this statement will be executed upon each request (whether it is for the initial page load or a xajax request. Everything after this statement will be executed only when the page is first loaded.

*/

$xajax->processRequest();

echo '<?xml version="1.0" encoding="UTF-8"?>';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>xajax example</title>

<?php

// output the xajax javascript. This must be called between the head tags

$xajax->printJavascript();

?>

<script type='text/javascript'>

/* <![CDATA[ */

window.onload = function() {

// call the helloWorld function to populate the div on load

<?php $reqHelloWorldMixed->printScript(); ?>;

// call the setColor function on load

<?php $reqSetColor->printScript(); ?>;

}

/* ]]> */

</script>

</head>

<body style="text-align:center;">

<div id="div1"> </div>

<br/>

<button onclick='<?php $reqHelloWorldMixed->printScript(); ?>' >Click Me</button>

<button onclick='<?php $reqHelloWorldAllCaps->printScript(); ?>' >CLICK ME</button>

<select id="colorselect" name="colorselect"

onchange='<?php $reqSetColor->printScript(); ?>;'>

<option value="black" selected="selected">Black</option>

<option value="red">Red</option>

<option value="green">Green</option>

<option value="blue">Blue</option>

</select>

</body>

</html>

运行此脚本,显示如下:



单击大小写按钮,"hello world"的字体会发生变化,在右侧选择不同颜色"hello world"的字体颜色发生相应变化。

【3】在PHP脚本之中加入xajax支持的步骤

(1)包含xajax类库:

  require_once("xajax.inc.php");

(2)实例化xajax 对象:

  $xajax = new xajax();

(3)注册你需要通过xajax调用的PHP函数的名称:

  $xajax->registerFunction("myFunction");

(4)编写注册的PHP函数,并且在函数之中使用xajaxResponse 对象返回XML指令:

function myFunction($arg)

{

 // 对$arg做一些基本处理例如从数据库检索数据

 // 然后把结果赋值给变量,例如$newContent

 // 实例化xajaxResponse 对象

 $objResponse = new xajaxResponse();

 // 添加指令到响应之中,用于指派

 //指定元素(例如id="SomeElementId")的innerHTML属性的新的内容

 $objResponse->addAssign("SomeElementId","innerHTML", $newContent);

 //返回xajaxResponse 对象生成的XML响应

 return $objResponse->getXML();

}

(5)在你的脚本输出任何信息之前,调用xajax用于接管请求:

  $xajax->processRequests();

(6)在页面的 <head></head> 标签之间,告诉xajax生成所必需的Javascrīpt:

  <?php $xajax->printJavascrīpt(); ?>

(7)在程序中,从Javascrīpt事件或者函数调用前面注册的函数:

  <div id="SomeElementId"></div>

  <button ōnclick="xajax_myFunction(SomeArgument);">

  就这么简单。xajax 会处理其他所有的事情。你所要做的主要工作就是编写PHP函数,然后从函数之中返回xajax的XML响应。而后者通过xajaxResponse类可以非常简单的生成.。

到此为止,thttpd+sqlite3+php组合构建的嵌入式服务平台测试完成了,可以在上面进行具体应用开发了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐