• 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html
  • 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html

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

项目实战 Geekerstar 11个月前 (05-19) 327次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

原理分析

具体实现

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

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

创建分类模块的相关程序

按照上面分析的路线图分别创建以下文件: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<Category> 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<Category> 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<Category> 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<Category> 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<Category> getAllCats() throws Exception {
		String sql="select * from category";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Category>(Category.class));
		
	}

}

修改header.jsp输出分类内容

<C:forEach item="${allCats}" var="c">
	<li><a href="#">${c.cname}</a></li>
</C:forEach>

修改web.xml

<servlet>
    <description></description>
    <display-name>IndexServlet</display-name>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>com.geekerstar.store.web.servlet.IndexServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>IndexServlet</servlet-name>
    <url-pattern>/IndexServlet</url-pattern>
  </servlet-mapping>

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

源码下载

下载地址

导航目录

查看导航
丨极客文库, 版权所有丨如未注明 , 均为原创丨
本网站采用知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议进行授权
转载请注明原文链接:Javaweb网上商城项目实战(13)实现首页的分类查询-版本1
喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

您必须 登录 才能发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00