JavaWeb综合案例第三天(在分页展示的基础上,使用数据库的Like关键字完成搜索功能)

一、在header.html中给搜索按钮注册点击事件

二、打开route_list.html,接收从header.html通过地址栏传递过来的cid和word

三、打开DispatherServlet,将用户请求的route/findPage.do分发到具体的java类

package com.bianyiit.web.servlet;

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 java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 用户模块
 * /user/login
 * /user/regist
 * 分类模块
 * /category/findAll
 * /category/add
 * 产品模块
 *
 */
@WebServlet(value = "*.do",loadOnStartup = 1)
/*接收请求,处理请求*/
public class DispatherServlet extends HttpServlet {
    Properties properties=new Properties();

    Map<Object,Object> map=new HashMap<>();
    @Override
    public void init() throws ServletException {
        try {
            properties.load(DispatherServlet.class.getClassLoader().getResourceAsStream("url.properties"));
            for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                Object key = entry.getKey();
                String className = (String)entry.getValue();
                Object value = Class.forName(className).newInstance();
                map.put(key,value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取方法名login.do
        String requestURI = request.getRequestURI();
        int index=requestURI.lastIndexOf("/")+1;
        String methodName = requestURI.substring(index,requestURI.lastIndexOf("."));
        //获取模块名user
        //模块名user--模块对象com.bianyiit.web.controller.UserController
        int index1 = requestURI.indexOf("/") + 1;
        String modelName = requestURI.substring(index1,(index-1));
        //从map中取出模块名反射获取的模块对象
        //模块对象UserController中有很多个方法,只有方法名login可以找到唯一的方法
        Object modelController = map.get(modelName);
        try {
            //通过模块对象获取对应方法对象
            //用户传过来的方法名是login,获取的就是login方法
            //用户传过来的方法名是regist,获取的就是regist方法
            Method method = modelController.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //login()
            method.invoke(modelController,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

四、打开RouteController类,接收Servlet分发过来的用户请求

package com.bianyiit.web.controller;

import com.bianyiit.domain.PageBean;
import com.bianyiit.domain.Route;
import com.bianyiit.services.RouteService;
import com.bianyiit.services.RouteServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class RouteController {
    public void findPage(HttpServletRequest request, HttpServletResponse response)throws IOException {
        response.setContentType("application/json;charset=utf-8");
        int cid = -1;
        int currenPage = 1;
        int pageSize = 9;
        try {
            cid = Integer.parseInt(request.getParameter("cid"));
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        currenPage = Integer.parseInt(request.getParameter("currentPage"));
        pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String wd = request.getParameter("wd");
        try {
            wd=new String(wd.getBytes("ISO8859-1"),"UTF-8");
        } catch (Exception e) {

        }
        RouteService routeService=new RouteServiceImpl();
        PageBean<Route> pageBean=routeService.findPage(cid,currenPage,pageSize,wd);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(response.getWriter(),pageBean);
    }
}

五、创建RouteService接口

package com.bianyiit.services;

import com.bianyiit.domain.PageBean;
import com.bianyiit.domain.Route;

public interface RouteService {
    PageBean<Route> findPage(int cid, int currenPage, int pageSize,String wd);
}

六、创建RouteServiceImpl类去实现RouteService接口,并重新写里面的抽象方法

package com.bianyiit.services;

import com.bianyiit.dao.RouteDao;
import com.bianyiit.dao.RouteDaoImpl;
import com.bianyiit.domain.PageBean;
import com.bianyiit.domain.Route;

import java.util.List;

public class RouteServiceImpl implements RouteService {
    @Override
    public PageBean<Route> findPage(int cid, int currenPage, int pageSize,String wd) {
        PageBean<Route> pageBean=new PageBean<>();
        pageBean.setCurrentPage(currenPage);
        pageBean.setPageSize(pageSize);
        RouteDao routeDao=new RouteDaoImpl();
        int count = routeDao.findCountByCid(cid,wd);
        pageBean.setTotalCount(count);
        int totalPage=count%pageSize==0?count/pageSize:count/pageSize+1;
        pageBean.setTotalPage(totalPage);
        int start=(currenPage-1)*pageSize;
        List<Route> routeList = routeDao.fingList(cid,start,pageSize,wd);
        pageBean.setList(routeList);
        return pageBean;
    }
}

七、创建 RouteDao接口

package com.bianyiit.dao;

import com.bianyiit.domain.Route;

import java.util.List;

public interface RouteDao {
    int findCountByCid(int cid,String wd);

    List<Route> fingList(int cid, int start, int pageSize,String wd);
}

八、创建RouteDaoImpl类去实现RouteDao接口,并重写里面的抽象方法

package com.bianyiit.dao;

import com.bianyiit.domain.Route;
import com.bianyiit.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class RouteDaoImpl implements RouteDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int findCountByCid(int cid,String wd) {
        if(!wd.equals("null")&& cid!=-1) {
            String sql="select count(rid) from tab_route where cid=? and rname like ?";
            return jdbcTemplate.queryForObject(sql, Integer.class, cid, "%" + wd + "%");
        }else if(!wd.equals("null")&& cid==-1){
            String sql="select count(rid) from tab_route where rname like ?";
            return jdbcTemplate.queryForObject(sql, Integer.class, "%" + wd + "%");
        }else if(wd.equals("null")&& cid!=-1){
            String sql="select count(rid) from tab_route where cid = ?";
            return jdbcTemplate.queryForObject(sql, Integer.class, cid);
        }else {
            String sql="select count(rid) from tab_route";
            return jdbcTemplate.queryForObject(sql, Integer.class);
        }
    }

    @Override
    public List<Route> fingList(int cid, int start, int pageSize,String wd) {
        if(!wd.equals("null")&& cid!=-1){
            String sql="select * from tab_route where cid=? and rname like ? limit ?,?";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class), cid,"%"+wd+"%", start, pageSize);
        }else if(!wd.equals("null")&& cid==-1){
            String sql="select * from tab_route where rname like ? limit ?,?";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class),"%"+wd+"%", start, pageSize);
        }else if(wd.equals("null")&& cid!=-1){
            String sql="select * from tab_route where cid=? limit ?,?";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class),cid, start, pageSize);
        }else {
            String sql="select * from tab_route where limit ?,?";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class),start, pageSize);
        }
    }
}

九、效果展示

主页效果:

在首页上不输入任何关键字,点击搜索

在首页上输入丽江,点击搜索

点击国内游,显示国内游的分页数据

在国内游界面,输入张家界,点击搜索按钮

十、最后注意:我们在点击首页传递过去的cid是null,点击其它分类数据传递过去的cid是相应的数据,当我们没有在搜索框输入任何关键字,传递过去的wd是null,输入关键字之后传递过去的wd就是我们在搜索框输入的搜索的内容,所以数据库使用like模糊查询时,需要考虑一共四种情况

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值