SpringMVC环境搭建及参数传递

一、环境搭建

1.创建maven-web工程:目录结构

在这里插入图片描述

2.导入maven坐标


    5.0.2.RELEASE
  
  
    
      junit
      junit
      4.11
      test
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      javax.servlet
      servlet-api
      2.5
    
    
      javax.servlet.jsp
      jsp-api
      2.0
    
  

3.编写springmvc.xml



    
    
    
    
        
        
    
    
    

4.编写web.xml



  Archetype Created Web Application
  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    CharacterEncodingFilter
    /*
  
  
  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    DispatcherServlet
    /
  

5.控制类

@Controller
public class HelloController {
    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("hello springMvc");
        return "success";
    }
}

6.测试

访问http://localhost:8080/hello即可跳转success.jsp

二、参数绑定

SpringMVC的参数绑定只需要再Controller的方法参数上写对应的参数名字即可

1.普通参数

接收提交的参数,并绑定到控制器方法的参数上

控制器类
@Controller
public class HelloController {
    @RequestMapping(path = "/hello")
    public String sayHello(String username,String password){
        System.out.println("username:"+username+"-- password:"+password);
        return "success";
    }
}
参数提交格式

2.普通JacaBean参数

参数对象User.java
public class User {
    private String username;
    private String password;
    //getter setter 省略
 }
控制器对应方法
  @RequestMapping(path = "/user")
    public String bindParams(User user){
        System.out.println(user);
        return "success";
    }
参数提交方式

3.POJO参数(plain ordinary java object)

实体类
public class User {
    private String username;
    private String password;
    private List accounts;
    private Map accountMap;
}
public class Account {
    private int id;
    private double money;
}
表单提交格式
控制器
 @RequestMapping(path = "/user")
    public String bindParams(User user){
        System.out.println(user);
        return "success";
    }
结果集
User{username='15023814323', password='uncle924', accounts=[Account{id=123, money=213.0}], accountMap={one=Account{id=312, money=312.0}}}

4.获取原生servlet-API

与参数绑定相同格式,只需要将需要的api当作参数写在Controller参数列表即可

    @RequestMapping(path = "/servlet_api")
    public String bindParams(HttpServletRequest request){
        System.out.println(request);
        return "success";
    }

5.自定义类型转换器

转换器类

需要实现Converter接口

public class MyDateConvert implements Converter {

    @Override
    public Date convert(String source) {
        if(StringUtils.isEmpty(source)){
            throw new RuntimeException("数据不能为空");
        }
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(source);
        } catch (ParseException e) {
            throw new RuntimeException("时间字符串解析错误");
        }
    }
}

注册转换器

再springmvc.xml中添加


    
        
            
                
            
        
    
    
    

你可能感兴趣的:(SpringMVC环境搭建及参数传递)