淘忆项目之主页界面服务器端的修正归纳

来源:互联网 发布:淘宝网店需要交税吗 编辑:程序博客网 时间: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();

}

 

}

经过以上的三步就可以向客户端传递需要的数据了。

 

0 0
原创粉丝点击