php前端和java后端数据调用流程
前端
1、新建php页面title.php
javascript
<title>标题</title>
<td width="30%" class="form-key">标题内容</td>
<td width="70%">
<input type="text" class="form-control" name="title" id="title" style="width:100%">
</td>
<?php include_once 'jstpl/title_tpl.php';?>
<script src="<?php echo $root_path ?>~main/js/title.js?v=<?php echo $v?>" type="text/javascript"></script>
2、新建js代码文件title.js
javascript
var dataList=null;
function search(){
var params=getSearchCondition();
var ret=execjava("titleBean.searchTitle", params, "json");
//数据
dataList=ret.data;
//模板渲染
var titleHtml=template.render('title_Tbl',{
dataList:dataList
});
$("#title_Tbl").html(titleHtml);
}
function getSearchCondition(){
var title=$("#title").val();
var params={
"title":title
};
return params;
}
3、新建php页面title_tpl.php
javascript
<script id="title_Tbl" type="text/html">
<td><%=dataList[i].title%></td>
</script>
后端
1、新建java文件TitleBean.java
javascript
package ...
import java.util.*;
@Controller
public class TitleBean{
private static final Logger logger=LoggerFactory.getLogger(TitleBean.class);
@Autowired
private TitleService titleService;
public Map<String,Object> searchTitle(){
Map<String,Object> ret=new HashMap<String,Object>();
Map<String,Object> params=new HashMap<String,Object>();
try{
String title=dform.getString("title");
params.put("title",title);
ret= titleService. searchTitle(params);
}catch(Exception e){
logger.error("获取标题出错!",e);
ret.put("state","500");
ret.put("receipt","获取标题出错");
}
return ret;
}
2、新建java文件TitleService.java
javascript
package ...
import java.util.*;
public interface TitleService{
Map<String,Object> searchTitle(Map<String,Object> params);
}
3、新建java文件TitleServiceImpl.java
javascript
package ...
import java.util.*;
public class TitleServiceImpl implements TitleService{
private static final Logger logger=LoggerFactory.getLogger(TitleServiceImpl.class);
@Override
public Map<String,Object> searchTitle(Map<String,Object> params){
Map<String,Object> ret = new HashMap<String,Object>();
try{
List<Map<String,Object>> dataList=dao.selectList("title.selectTitle",params);
ret.put("data",dataList);
ret.put("state","200");
ret.put("receipt","获取标题列表成功");
}catch (Exception e){
logger.error("获取标题列表失败",e);
ret.put("state","500");
ret.put("receipt","获取标题列表出错");
}
return ret;
}
4、sql语句,新建xml文件title.xml
javascript
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "..//mybatis.org//DTD Mapper 3.0//EN" http://mybatis.org/dtd/mybatis-3-mapper.dtd>
<mapper namespace="title">
<seclect id="selectTitle" resultType="hashmap">
SELECT id, title FROM test_title order by id desc
</select>
</mapper>
数据库
新建数据库,名称为Test,新建基本表,名称为test_title
表中新建字段title,varchar(100)