20180302.实现搜索,和搜索后关注用户的功能。

 

首先需要从前端取得用户搜索的信息。双向绑定取得数据,并跳转路径到搜索发现:
this.$http.get('/api/UserManager/Search?s='+this.search1).then((response)=>{

          for(var i in response.data.result){
              sessionStorage.setItem('search'+i,response.data.result[i]);
              sessionStorage.setItem('searchEssay'+i,response.data.result1[i]);
          }
              sessionStorage.setItem('search_times',i);
            }
        )
            .catch(function(err){
              console.log(err);
            });
            //跳转
      this.$router.push({path:'/Child3'});

servlet接受到异步请求后,使用DAO方法:

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

resp.setContentType("text/html;charset=utf-8");

            
    //1. 取到前端发表文章的表单提交的文章信息
            String s = req.getParameter("s");
            
            
            // 再次拿到会话对象
            HttpSession session = req.getSession();
            
            
            ArrayList List=new UserDAO().search(s);
            
    
    resp.setContentType("application/json");
    // 创建一个json的对象,把结果保存到result键中
    JsonObject result_obj= new JsonObject();
    JsonArray jsonArray=new JsonArray();
    JsonArray jsonArray1=new JsonArray();
    for(int i=0;i

后端DAO执行数据库搜索:

@Override

public ArrayList search(String s) {
    Connection conn=getConnection();
    String sql="";
    PreparedStatement pstmt;
    ArrayList List=new ArrayList();
    sql="SELECT * FROM essay WHERE context =? OR user_name =?";
    try{
        pstmt=conn.prepareStatement(sql);
        pstmt.setString(1,s);
        pstmt.setString(2,s);
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()){
            int id=rs.getInt("id");
            String user_name = rs.getString("user_name");
            String context = rs.getString("context");
            Timestamp time=rs.getTimestamp("time");
            List.add(new essay(id,user_name,context,time));
        }
        return List;
    }catch(SQLException e){
        e.printStackTrace();
        return null;
    }finally{
        closeConnection(conn);
    }
}

@Override
public int addFollow(String user_name, String follow_name) {
    Connection conn=getConnection();
    String sql="";
    PreparedStatement pstmt;
    sql="INSERT INTO follow VALUES(?,?)";
    try{
        pstmt=conn.prepareStatement(sql);
        pstmt.setString(1, user_name);
        pstmt.setString(2, follow_name);
        
        return pstmt.executeUpdate();
    }catch(SQLException e){
        e.printStackTrace();
        return 0;
    }finally{
        closeConnection(conn);
    }
}

你可能感兴趣的:(20180302.实现搜索,和搜索后关注用户的功能。)