ssm搜索+登录安全+解决中文乱码

在上一篇基础上添加或修改代码
搜索
“IUserDao”修改findAll语句

List<User> findAll(@Param("searchname") String searchname);

“IUserService”修改findAll语句

List<User> findAll(int pageNum,String searchname,int size);

“UserService”修改findAll语句

@Override
    public List<User> findAll(int pageNum,String searchname,int size) {
        PageHelper.startPage(pageNum,size);
        return userDao.findAll(searchname);
    }

“UserController”修改findAll语句

@RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "5") int size,
                                String searchname, HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        String username= (String) session.getAttribute("username");
        if (username==null){
            modelAndView.setViewName("failure.jsp");
            return modelAndView;
        }
        if (searchname!=null){
            session.setAttribute("searchname",searchname);
        }else {
            searchname= (String) session.getAttribute("searchname");
        }
        PageInfo<User> pageInfo=new PageInfo<>(userService.findAll(pageNum,searchname,size));
        modelAndView.setViewName("allUser.jsp");
        modelAndView.addObject("pageInfo",pageInfo);
        return modelAndView;
    }

“allUser.jsp”在“新增”后添加搜索语句

<form action="/findAll.do" method="post">
            <input name="searchname" type="text"/>
            <input type="submit" value="搜索"/>
        </form>

登录安全(不可凭借网址进入列表页或对列表页进行“增删改查”等操作)
在“zhongruan”下建包“filter”,并在“filter”下建“LoginFilter.java”,代码如下

package com.zhongruan.filter;

        import javax.servlet.*;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import javax.servlet.http.HttpSession;
        import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        HttpSession session=request.getSession();
        if (session.getAttribute("username")==null&&
                request.getRequestURI().indexOf("/login.do")==-1){
            response.sendRedirect("index.jsp");
        }else {
            filterChain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}

修改“UserController”中的"/login.do"和“delete.do”块代码

@RequestMapping("/login.do")
public String login(User user,HttpSession session){
    Boolean flag=userService.login(user.getUsername(),user.getPassword());
    if (flag){
        session.setAttribute("username",user.getUsername());
        return "redirect:/findAll.do";
    }else {
        return "failure.jsp";
    }

}
@RequestMapping("delete.do")
    public String deleteById(int id,HttpSession session){
        String username= (String) session.getAttribute("username");
        if (username==null){
            return "failure.jsp";
        }
        userService.deleteById(id);
        return "redirect:/findAll.do";
    }

在“web.xml”中增加“filter”代码块

<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.zhongruan.filter.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>

解决中文乱码
在“web.xml”中增加“filter”代码块

<!-- 解决中文乱码过滤器 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值