Javaweb网上商城项目实战(13)实现首页的分类查询-版本1

原理分析

具体实现

修改根目录下index.jsp,转发至IndexServlet

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

创建分类模块的相关程序

按照上面分析的路线图分别创建以下文件:CategoryServlet、CategoryService、CategoryServiceImp、CategoryDao、CategoryDaoImp、Category。

Category.java

package com.geekerstar.store.domain;

public class Category {
	private String cid;
	private String cname;
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	
	public Category() {
		// TODO Auto-generated constructor stub
	}
	public Category(String cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}
	@Override
	public String toString() {
		return "Category [cid=" + cid + ", cname=" + cname + "]";
	}
	
}

IndexServlet.java

package com.geekerstar.store.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.geekerstar.store.domain.Category;
import com.geekerstar.store.service.CategoryService;
import com.geekerstar.store.service.serviceImp.CategoryServiceImp;
import com.geekerstar.store.web.base.BaseServlet;

public class IndexServlet extends BaseServlet {
	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//调用业务层功能:获取全部分类信息,返回集合
		CategoryService CategoryService=new CategoryServiceImp();
		List list=CategoryService.getAllCats();
		//将返回的集合放入request
		request.setAttribute("allCats", list);
		//转发到真实的首页
		return "/jsp/index.jsp";
	}
}

CategoryService.java

package com.geekerstar.store.service;

import java.util.List;

import com.geekerstar.store.domain.Category;

public interface CategoryService {

	List getAllCats() throws Exception;

}

CategoryServiceImp.java

package com.geekerstar.store.service.serviceImp;

import java.util.List;

import com.geekerstar.store.dao.CategoryDao;
import com.geekerstar.store.dao.daoImp.CategoryDaoImp;
import com.geekerstar.store.domain.Category;
import com.geekerstar.store.service.CategoryService;

public class CategoryServiceImp implements CategoryService{

	@Override
	public List getAllCats() throws Exception {
		CategoryDao CategoryDao = new CategoryDaoImp();
		return CategoryDao.getAllCats();
	}

}

CategoryDao.java

package com.geekerstar.store.dao;

import java.util.List;

import com.geekerstar.store.domain.Category;

public interface CategoryDao {

	List getAllCats() throws Exception;

}

CategoryDaoImp.java

package com.geekerstar.store.dao.daoImp;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.geekerstar.store.dao.CategoryDao;
import com.geekerstar.store.domain.Category;

import com.geekerstar.store.utils.JDBCUtils;

public class CategoryDaoImp implements CategoryDao{

	@Override
	public List getAllCats() throws Exception {
		String sql="select * from category";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler(Category.class));
		
	}

}

修改header.jsp输出分类内容


	
  • ${c.cname}
  • 修改web.xml

    
        
        IndexServlet
        IndexServlet
        com.geekerstar.store.web.servlet.IndexServlet
      
      
        IndexServlet
        /IndexServlet
      
    

    接下来运行一下,结果正常,可以看到导航栏已经输出了分类项,弊端:当访问首页是可以看到全部分类信息,但是如果访问其它页面,看不到分类信息

    源码下载

    [dm href=’https://www.jikewenku.com/product/1700.html’]下载地址[/dm]

    导航目录

    [dm href=’https://www.jikewenku.com/project/2786.html’]查看导航[/dm]

    本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
    极客文库 » Javaweb网上商城项目实战(13)实现首页的分类查询-版本1

    Leave a Reply

    欢迎加入「极客文库」,成为原创作者从这里开始!

    立即加入 了解更多