SpringMvc使用以及ssm整合

SpringMvc框架使用

  • 导包
  • 配置web.xml文件



     
    


    
    

    
    

    
    
        
        
    
    
    

配置springmvc.xml




     
    


    
    

    
    
    
    
    
    

实现一个Controller

@Controller
public class ItemController {


        @RequestMapping(value = "/item/itemlist.action",method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView itemList(){
        ModelAndView mav=new ModelAndView();
        //mav.setViewName("/WEB-INF/jsp/itemList.jsp");
        //配置了视图解析器后
        mav.setViewName("itemList");
        return mav;
    }
}

访问http://localhost:8080/item/itemlist.action

参数绑定

//直接方法上写提交上来的参数名字可以直接获得
  @RequestMapping(value = "/item/paramer.action")
    public ModelAndView getParamer(@RequestParam(value = "age",required = false,defaultValue = "1") int age,Integer id, String name){
        System.out.println(id);
        System.out.println(name);
        List itemselect = itemService.getItemselect();
        ModelAndView mav=new ModelAndView();
        mav.setViewName("itemList");
        return mav;
    }

对象绑定

@RequestMapping(value = "/item/user.action")
    public ModelAndView getUser(User user){
        System.out.println(user);
        ModelAndView mav=new ModelAndView();
        mav.setViewName("itemList");
        return mav;
    }

//http://localhost:8080/item/user.action?age=12&name=xiaohon(自动封装一个User)

在类上处理映射

@RequestMapping(value = "/item")
public class ItemController {
//多个url映射到同一个方法上
@RequestMapping(value ={ "/paramer.action"," "/paramexxxr.action""})
    public ModelAndView getParamer(){
        
    }
}

自定义参数绑定

如果提交了一个date为2017:12-02 15_22-22,springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。

   @RequestMapping(value = "/item/user.action")
    public ModelAndView getUser(User user,Date date){
        System.out.println(user);
        ModelAndView mav=new ModelAndView();
        mav.setViewName("itemList");
        return mav;
    }
public class DateConver implements Converter{

    @Override
    public Date convert(String s) {
        try {
            if (null!=s){
                DateFormat format=new SimpleDateFormat("yyyy:MM-dd HH_mm-ss");
                return format.parse(s);
            }
        }catch (Exception e){

        }
        return null;
    }
}


    
     
    
        
        
            
                
            
        
    

PathVariable

@RequestMapping(value ="/itemEdit/{id}.action")
    public void test(@PathVariable Integer id){
}

Controller的返回

  • ModelAndView
  • String
public String testController(Model model){

//return "forward:path";//path代表转发的地址
return "redirect:path";//path代表重定向的地址

}
  • Void(返回这种结果的时候可以在Controller方法的形参中定义HTTPServletRequest和HTTPServletResponse对象进行请求的接收和响应)

异常处理器

当发生异常了,捕获到异常信息

public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(javax.servlet.http.HttpServletRequest httpServletRequest,
                                         javax.servlet.http.HttpServletResponse httpServletResponse,
                                         Object o,
                                         Exception e) {
//        发生异常的地方sevice 方法
        //可以记录到日志
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("error","发生错误");
        modelAndView.setViewName("error");

        return modelAndView;
    }
}

在springmvc.xml中注册

   
    

访问进入异常界面,在上方能捕获到

@RequestMapping(value = "/item/itemlist.action",method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView itemList(){
        List itemselect = itemService.getItemselect();
        ModelAndView mav=new ModelAndView();
        mav.setViewName("itemList");
        int i=1/0;
        return mav;
    }

图片上传

  @RequestMapping(value = "update.action")
    public void file(MultipartFile file) throws IOException {
        file.transferTo(new File("D:\\"));
    }

在springmvc中配置

 
    
        
        
    

json交互

客户端提交json数据,服务器返回json数据

 @RequestMapping(value = "/json.action")
    @ResponseBody
    //客户端提交json数据,自动解析成User
    public User json(@RequestBody  User user){
         return user;
    }

如果提示415错误的话参考下面文章
http://blog.csdn.net/tiantiandjava/article/details/46125141

拦截器

springmvc.xml配置

   
    
        
        
            
            
            
            
        
        
            
            
            
            
        
    
public class interceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("方法前1");
        //是否拦截
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("方法后1");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("页面渲染后1");
    }
}

SpringMvc 流程

SpringMvc使用以及ssm整合_第1张图片
这里写图片描述

架构流程

⦁ 用户发送请求至前端控制器DispatcherServlet
⦁ DispatcherServlet收到请求调用HandlerMapping处理器映射器。
⦁ 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
⦁ DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
⦁ 执行处理器(Controller,也叫后端控制器)。
⦁ Controller执行完成返回ModelAndView
⦁ HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
⦁ DispatcherServlet将ModelAndView传给ViewReslover视图解析器
⦁ ViewReslover解析后返回具体View
⦁ DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
⦁ DispatcherServlet响应用户

处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件。

整合mybatis

web.xml





    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

  
  
    encoding
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        UTF-8
    
  
  
  
    encoding
    *.action
  
  

    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
      
        
            contextConfigLocation
            classpath:springmvc.xml
        
    
    
        springmvc
        
        *.action
    


springmvc与struts2不同

⦁ springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过滤器。
⦁ springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),struts2是基于类开发,传递参数是通过类的属性,只能设计为多例。
⦁ Struts采用值栈存储请求和响应的数据,通过OGNL存取数据, springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过request域传输到页面。Jsp视图解析器默认使用jstl。

SSM整合

配置sqlMapConfig.xml





    
    
        
    


配置applicationContext-dao.xml




    
    



    
    
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
        
    





配置applicationContext-service.xml




    
    


配置SpringMvc.xml



    
    
    
        
    
    
    
    
    
    
    
    
    
    
        
        
        
        
    

    

配置web.xml



    ssm
    
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
    


    
    
        contextConfigLocation
        classpath:spring/applicationContext-*.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    


    
    
        encoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encoding
        /*
    
    
    
        ssm
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        
        
        
        1
    
    
        ssm
        
        /
    



原文链接:http://blog.csdn.net/qq_22329521/article/details/75116264

你可能感兴趣的:(SpringMvc使用以及ssm整合)