SpringMVC之参数绑定

springmvc的参数绑定

一、默认参数绑定

​ 也就是那些httpServletRquest、httpServletResponse、httpSession、Model之类的参数,在controller方法上可以有可以没有(作为方法的输入参数写出来),你需要什么就加什么

eg:

@RequestMapping("/test")
public void test()public void test(HttpRequest httpRequest, HttpSession httpSession, HttpResponse httpResponse){

}

二、基本参数类型绑定

​ String, double, float, integer, long, boolean类似于默认参数绑定,作为controller方法的输入参数

三、pojo参数

这个要求页面的input框的name属性值必须等于pojo的属性参数

四、Vo参数

页面上的input框的name属性值必须等于vo中的属性.属性.属性…….

五、自定义转换器converter

​ SpringMVC是没法将String自动转化成Date类型,所以我们写一个转换器converter来转化

方法是写一个类实现Converter接口

在SpringMvc.xml文件中配置自定义转换器,并且将其配置到注解驱动上

类DateConverter

public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            returnsimpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        returnnull;
    }
}




配置Converter方式1
<!-- 加载注解驱动 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 转换器配置 -->
<bean id="conversionService"
    class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="这里添加DateConverter的类路径"/>
        </set>
    </property>
</bean>

配置Converter方式2

<!-- 转换器配置 -->
<bean id="conversionService"
    class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="这里添加DateConverter的类路径"/>
        </set>
    </property>
</bean>
<!-- 自定义webBinder -->
<bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="conversionService" ref="conversionService"/>
</bean>
<!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 注解处理器映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

六、数组的绑定

​ 在这里我们使用例子讲解吧,测试类为简单的User类

jsp页面

<form action="/array" method="post">
    <c:forEach items="${listUser}" var = "user">
        id:<input type="text" name="id" value="${user.id}"/></br>
        password:<input type="password" name="password " value="${user.password}"/><</br>
    sex:<input type="text" name="sex" value="${user.sex}"/></br>
    </c:forEach>
    <input type="submit" value="提交"/>
</form>

controller.java

@RequestMapping("/array")
public void array(Interger[] id){
// 这个id必须和form中的name属性保持一致
  for(int i: id){
    System.out.println(i);
  }
}

七、List参数绑定

​ 举个例子,创建一个vo类, 用于封装List属性

UserVo.java

public class UserVo(){
  private List<User> userList;
  public List<User> getUserList(){
    return userList;
  }
  public void setUserList(List<User> userList){
    this.userList = userList;
  }
}

Controller(用于向页面传参)

@RequestMapping("/user")
public ModelAndView select(){
  List<User> userList = userService.selectAllUser();
  ModelAndView model = new ModelAndView();
  model.addObject("userList", userList);
  model.setViewName("list.jsp");
  return model;
}

jsp

<form action="/list" method="post">
    <c:forEach items="${userList}" var="user" varStatus="status">
        id:<input type="text" name="userList[${status.index}].id" value="${user.id}"/></br>
        password:<input type="password" name="serList[${status.index}].password " value="${user.password}"/><</br>
        sex:<input type="text" name="serList[${status.index}].sex" value="${user.sex}"/></br>
    </c:forEach>
<%--解释下name属性 userList[${status.index}].id 
    userList表示UserVo里面封装List
    status.index 表示下标
    id表示User中的属性id--%>
</form>

list

@RequestMapping("/list")
public void list(UserVo vo){

  System.out.println(userVo.getUserList());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值