淘忆项目之主页界面服务器端的修正归纳
来源:互联网 发布:淘宝网店需要交税吗 编辑:程序博客网 时间:2024/05/01 10:05
淘忆项目之主页界面服务器端的修正归纳
服务器端需要传递给客户端信息,传给的数据有用户的头像和名字,物件的图片,发布时间,收藏的数量,留言的数量。
采用mvc 模式书写服务器端的代码。
第一步:建立以下文件。
建立三个包,三个文件。
第二步:书写HomeItemService.java的内容。
package com.elaine.homeItem.service;
import java.util.List;
import java.util.Map;
public interface HomeItemService {
public List<Map<String, Object>> listThing();
}
第三步:书写HomeItemDao.java的内容。
package com.elaine.homeItem.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.elaine.homeItem.service.HomeItemService;
import com.elaine.jdbc.JdbcUtils;
public class HomeItemDao implements HomeItemService{
JdbcUtils jdbcUtils;
public HomeItemDao() {
// TODO Auto-generated constructor stub
jdbcUtils=new JdbcUtils();
}
@Override
public List<Map<String, Object>> listThing() {
// TODO Auto-generated method stub
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
jdbcUtils.getConnection();
String lineSql="select * from thingInfo"; //首先获取所有的物件的信息
int lineNum=jdbcUtils.getLineNum(lineSql);//获取一共有多少行的行数
for(int id=1;id<=lineNum;id++){
String sql="select * from thingInfo where (1=1)";
StringBuffer buffer = new StringBuffer(sql);
List<Object> params = new ArrayList<Object>();
buffer.append("and id like ?");
params.add("%" + id + "%");
Map<String, Object> map=jdbcUtils.findSimpleResult(buffer.toString(), params);
String userId=(String) map.get("userId");
String userSql="select *from userInfo where userId=?"; //获取user的信息
List<Object> params1=new ArrayList<Object>();
params1.add(userId);
Map<String, Object> map1 = jdbcUtils.findSimpleResult(userSql, params1);
String username=(String) map1.get("username");
String userHead=(String) map1.get("userHead");
map.put("username", username);
map.put("userHead", userHead);//将姓名和头像的图片地址传给map
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
jdbcUtils.releaseConn();
}
return list;
}
}
第四步:书写HomeItemAction.java的内容。
package com.elaine.homeItem.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.elaine.homeItem.dao.HomeItemDao;
import com.elaine.homeItem.service.HomeItemService;
import com.elaine.tools.jsonTool;
public class HomeItemAction extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private HomeItemService service;
/**
* Constructor of the object.
*/
public HomeItemAction() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
List<Map<String, Object>> list = service.listThing();
String jsonString = jsonTool.creataJsonString("thing", list);
out.print(jsonString);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
service=new HomeItemDao();
}
}
经过以上的三步就可以向客户端传递需要的数据了。
- 淘忆项目之主页界面服务器端的修正归纳
- 淘忆项目之分享界面服务器端的修正归纳
- 淘忆项目之注册界面的修正归纳
- 淘忆项目之登录界面的修正归纳
- 淘忆项目之忘记密码界面的修正归纳
- 淘忆项目之主界面的修正归纳
- 淘忆项目之主题界面的修正归纳
- 淘忆项目之主题详情界面的修正归纳
- 淘忆项目之分享界面客户端的修正归纳
- 淘忆项目之主页界面客户端的整理归纳
- 淘忆项目之欢迎界面的归纳分享
- 淘忆项目之引导界面的分享归纳
- 淘忆项目之服务器端的初始化
- 安装IE7 主页设置无效的修正
- 安卓项目之淘忆2.0的代码实现之主页底部的导航栏功能
- 服务器端修正(1)
- 优秀的项目介绍主页
- 美团(三)之登录到主页面间的加载界面
- NC65主子表单据保存出错
- 编程之美
- 百度云计算部门面试
- 红黑树
- 斯坦福公开课Machine Learning笔记(一)--Linear Regression
- 淘忆项目之主页界面服务器端的修正归纳
- 知识点总结
- 去除tableview cell起始15pt的两种方法
- 1112: [POI2008]砖块Klo
- 重装win10关机不断电的解决办法
- 最佳循环&&嵌套&&数组
- Hadoop新手学习指导
- jmeter中不用time函数,用Random函数设置正确的时间格式
- Hello Mr.J——shiro+springmvc