SpringMVC学习笔记22-1-29

SpringMVC学习笔记22-1-29,第1张

SpringMVC学习笔记22-1-29

SpringMvc

1-SpringMVC简介 1.1-是什么

一个轻量级的MVC框架

1.2-为什么

轻量,简单高效,基于响应兼容Spring用的人多 2-建立一个SpringMVC的流程 2.1-新建maven项目并添加web 框架支持 2.2-添加依赖项


    
    
        junit
        junit
        4.12
        test
    
    
    
        log4j
        log4j
        1.2.17
    
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
    
        org.apache.tomcat
        jsp-api
        6.0.14
    
    
    
        javax.servlet
        jstl
        1.2
    
    
    
        org.springframework
        spring-webmvc
        5.1.10.RELEASE
    

2.3-配置web.xml , 注册DispatcherServlet



    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        
        
        1
    

    
    
    public int id;
    public String name;
    public String desc;
}
dao层编写
public interface UserDao {
    List getUserList();
    User getUserById(@Param("id") int id);
    int insertUser(User user);
    int deleteUser(@Param("id") int id);
    int updateUser(User user);
}
mapper.xml

写完xml记得要注册




    
        select * from wuziqi.User where id = #{id};
    
    
        insert into wuziqi.User(name, `desc`)
        values (#{name},#{desc});
    
    
        update wuziqi.User
        set  name=#{name},desc=#{desc}
        where id=#{id};
    
    
        delete wuziqi.User where id=#{id};
    

service业务层编写 接口
public interface UserServlet {
    List getUserList();
    User getUserById(int id);
    int insertUser(User user);
    int deleteUser(int id);
    int updateUser(User user);
}
实现
public class UserServletImpl implements UserServlet{

    private UserDao userDao;
    @Override
    public List getUserList() {
        return userDao.getUserList();
    }

    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }

    @Override
    public int insertUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return userDao.deleteUser(id);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }
}
9.5-spring整合 spring-dao.xml整合mybatis/dao层



    
    
    

    
    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
    
        
        
        
        
    


spring-service.xml整合业务层



   
   

   
   

   
   
       
       
   


9.6-springMVC整合 web.xml
   
       DispatcherServlet
       org.springframework.web.servlet.DispatcherServlet
       
           contextConfigLocation
             
           classpath:applicationContext.xml
       
       1
   
   
       DispatcherServlet
       /
   

   
   
       encodingFilter
       
          org.springframework.web.filter.CharacterEncodingFilter
       
       
           encoding
           utf-8
       
   
   
       encodingFilter
       /*
   
   
   
   
       15
   
applicationContext.xml



    
    
    

10-拦截

什么是拦截器

就是拦截请求,检查。比如不登录不能进入某些页面

web.xml一致

在spring配置里加入


    
        
        
        
        
    

拦截器需要实现HandlerInterceptor接口

有三个方法,对应三个阶段

public class firstIntercepter implements HandlerInterceptor {
    //请求前 true准入,false禁止
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return "msg".equals(request.getParameter("msg"));
    }
    // 执行后
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
    // 清理工作
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

测试controller

@Controller
public class testCrontroller {
    @RequestMapping("/aaa")
    @ResponseBody
    public String test(){
        return "ok!!";
    }
    @RequestMapping("/bbb")
    public String gotoo(){
        return "aaa";
    }
}

拦截了aaa,假如msg不是msg会被拦截

访问bbb,会天跳转到aaa.jsp提交表单,之后跳转到aaa,只有当msg合规时才会提示ok

11-文件 *** 作

暂略

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5719721.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存