SpringMvc框架及SSM框架整合

SpringMvc框架及SSM框架整合

一、SpringMvc相关知识

1、Spring和SpringMvc的关系

​ 1.1、Spring是IOC和AOP的容器框架,SpringMVC是基于Spring功能之上添加的Web框架,想用SpringMVC必须先依赖Spring。

​ 1.2、SpringMVC是一个MVC模式的WEB开发框架;Spring是一个通用解决方案, 最大的用处就是通过Ioc/AOP解耦, 降低软件复杂性, 所以Spring可以结合SpringMVC等很多其他解决方案一起使用, 不仅仅只适用于WEB开发

2、SpringMvc工作原理图

SpringMvc框架及SSM框架整合_第1张图片

二、框架搭建

1、配置文件配置
1.1、pom.xml依赖

            junit
            junit
            4.11
        
         
         
            mysql
            mysql-connector-java
            5.1.38
        
        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
        
        
            org.springframework
            spring-aop
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-aspects
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-beans
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-context
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-core
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-expression
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-tx
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-jdbc
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-web
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-webmvc
            4.3.7.RELEASE
        
        
        
            org.springframework
            spring-test
            4.3.7.RELEASE
            test
        
        
            org.mybatis
            mybatis
            3.4.4
        
        
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.7.3
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.7.3
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.7.3
        

        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            commons-io
            commons-io
            2.5
        

1.2、web.xml



    
    
    
        contextConfigLocation
        classpath:spring/applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        ce
        org.springframework.web.filter.CharacterEncodingFilter
        true
        
            encoding
            UTF-8
        
    
    
        ce
        /*
    
    
    
        ds
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:mvc/SpringMvc.xml
        
        
        1
    
    
        ds
        /
    

1.3、SpringMvc.xml




    
    

    
    
        
            
                
            
            
            
        
    
    
    
    
    
    
        
        
            /WEB-INF/jsp/
        
        
        
            .jsp
        
    

1.4、Spring核心配置文件:applicationContext.xml


    
    
    

    
    
        
        
        
        
        
        
        
    
    
    
        
        
    
    
    
    
    
        
        
        
        
        
        
    
    
    
        
        
    

1.5、MyBatis核心配置文件


    
        
        
    
    
    
        
        
        
        
        
    
    
    
   

三、SpringMvc操作

1、SpringMvc特有的注解
  1. @requestMapping()注解 (其实就是起别名),可以修饰类或者方法;@RequestMapping”请求路径映射,如果标注在某个controller的类级别上,则表明访问此类路径下的方法都要加上其配置的路径;最常用是标注在方法上,表明哪个具体的方法来接受处理某次请求。如果类上没有该注解,只是在方法之上有该注解,那么在浏览请求的时候url=localhost:8080/方法请求映射名称
  2. @RequestParam()请求参数注解,应用在方法的参数中
    @requestParam主要用于在SpringMVC后台控制层获取参数,类似一种是request.getParameter(“name”),它有三个常用参数:defaultValue = “0”, required = false, value = “isApp”;defaultValue 表示设置默认值,required 通过boolean设置是否是必须要传入的参数,true表示必须传入,如果不传则为null,如果为null,则报异常。value 值表示接收的传入的参数名称
  3. @DateTimeFormat(pattern=“yyyy-MM-dd”)注解date时间格式把国际格式改为中文格式,应用于pojo类的
    date类型属性之上
  4. 参数注解
    @PathVariable只能修饰从路径中获取的参数,用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
2、具体案例
2.1、获取请求参数几种方式
@Controller
@RequestMapping("con1")
public class TestController {
    @RequestMapping("/test1")
    public void test1(String name){//如果不使用@RequestParam,方法中的形参要和URL的参数名一致
        System.out.println(name);
        System.out.println("ssm整合1");
    }
    @RequestMapping("/test2")
    public void test2(@RequestParam(name = "name")String a){
        System.out.println(a);
        System.out.println("ssm整合2");
    }
    @RequestMapping(value = "/testPost",method = RequestMethod.POST)//post方式提交
    public void testPost(String name, int age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        System.out.println(name);
        System.out.println(age);
        System.out.println(date);
    }
    @RequestMapping(value = "/testGet",method = RequestMethod.GET)//get方式请求
    public void testGet(@RequestParam Map map){
        Set set = map.entrySet();
        for (Map.Entry entry:set){
            System.out.println(entry.getKey()+"--"+entry.getValue());
        }
    }
    //restful风格的参数获取--url中只有参数值,参数名在注解中,路径变量的注解@pathVariable
    @RequestMapping(value = "/test3/{name}/{age}/{birth}")
    public void test3(@PathVariable String name,
                      @PathVariable Integer age,
                      @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birth){
        System.out.println(name);
        System.out.println(age);
        System.out.println(birth);
    }
}

2.2、解决乱码问题


            
                
            
    
            
            
        

 
    
        ce
        org.springframework.web.filter.CharacterEncodingFilter
        true
        
            encoding
            UTF-8
        
    
    
        ce
        /*
    

 

         org.apache.tomcat.maven
         tomcat7-maven-plugin
         2.2
          
           /
           8080
          UTF-8
           
          

2.3、实现页面跳转

@RequestMapping("/test4")
    public String test4(){//页面跳转--转发
        return "test";
    }
    @RequestMapping("/test5")
    public String test5(){//重定向
        return "redirect:test4";
    }

2.4、响应正文

//响应正文
    //响应的一个字符串response.getWriter().writer("")
    @RequestMapping("/test1")
    @ResponseBody//响应正文
    public String test1(){
        return "hello!";
    }
    //响应的一个JavaBean使用json转换器
    @RequestMapping(value = "/test2",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public Object test2(){
        User user = new User();
        user.setName("韩冰");
        user.setAge(18);
        user.setBirth(new Date());
        return user.toString();
    }
    //响应字节--图片
    @RequestMapping(value = "/test3")
    public void test3(HttpServletResponse response) throws Exception {
        FileInputStream is = new FileInputStream("D:\\照片\\壁纸\\cat.jpg");
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(is,os);
    }

2.5、参数存储,存进request域对象,三种方式

@Controller
@RequestMapping("con3")
public class TestController3 {
    //Model
    @RequestMapping("/test1")
    public String test1(Model model){
        model.addAttribute("name","萧寒");
        return "test";
    }
    //ModelMap
    @RequestMapping("/test2")
    public String test2(ModelMap map){
        map.addAttribute("name","魏巍");
        return "test";
    }
    //ModelAndView  Model和jsp跳转的结合,返回值不是String
    @RequestMapping("/test3")
    public ModelAndView test3(ModelAndView mav){
        mav.addObject("name","魏文");
        mav.setViewName("test");
        return mav;
    }
}

你可能感兴趣的:(SpringMvc框架及SSM框架整合)