springmvc(三)---方法参数绑定

一、springmvc默认支持的方法参数绑定

1、HttpServletRequest:通过request对象请求信息

2、HttpServletResponse:通过response对象响应信息

3、HttpSession:通过session对象得到存放在session中的对象信息

4、Model/ModelMap:将数据放到request域中

二、简单类型的绑定

1、第一种方式:不加@RequestParam,参数名必须与request传入参数名称一致

例:

a、jsp界面

<form action="${pageContext.request.contextPath }/addBlog1.action" method="post">
姓名:<input type="text" name="username"/>
<input type="submit" value="提交"/>
</form>

b、controller端接收参数

@RequestMapping("/addBlog1.action")
public String addBlog1(String username){

System.out.println(username);
//重定向到blog列表
return "redirect:/queryBlog.action";

}

很容易发现如果输入中文就会有乱码

springmvc解决post乱码的方式:

在web.xml中添加过滤器

<!-- 解决post乱码 ,在spring-web包中 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- 将编码格式改为UTF-8 -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2、第二种方式:加@RequestParam,request传入的参数名可以不与方法参数一致

@RequestMapping("/addBlog2.action")
public String addBlog2(@RequestParam(value="username") String name){
System.out.println(name);
//重定向到blog列表
return "redirect:/queryBlog.action";
}

三、pojo对象的绑定

1、绑定pojo对象中的简单类型

a、jsp界面

<form action="${pageContext.request.contextPath }/addBlog3.action" method="post">
标题:<input type="text" name="title"/>
内容:<input type="text" name="content"/>
<input type="submit" value="提交pojo对象1"/>
</form>

b、controller接收参数

@RequestMapping("/addBlog3.action")
public String addBlog3(Blog blog){
System.out.println(blog.getTitle());
System.out.println(blog.getContent());
//重定向到blog列表
return "redirect:/queryBlog.action";
}

2、绑定pojo对象的复杂对象

a、加入User类

package top.einino.pojo;

public class User {
    private int id;
    private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}

}

b、在Blog类中加入User属性

c、jsp界面

<form action="${pageContext.request.contextPath }/addBlog4.action" method="post">
标题:<input type="text" name="title"/></br>
内容:<input type="text" name="content"/></br>
博主姓名:<input type="text" name="user.username"/></br>
<input type="submit" value="提交pojo对象2"/>
</form>

d、controller接收参数

//pojo对象绑定复杂对象
@RequestMapping("/addBlog4.action")
public String addBlog4(Blog blog){
System.out.println(blog.getTitle());
System.out.println(blog.getContent());
 System.out.println(blog.getUser().getUsername());
//重定向到blog列表
return "redirect:/queryBlog.action";
}

四、自定义日期类型绑定

1、添加日期类型转换

package top.einino.controller.convert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class StringToDateConverter implements Converter<String, Date>{

@Override
public Date convert(String source) {

 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
     parse = simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}

}

2、springmvc.xml添加日期类型转换注册

a、    <mvc:annotation-driven conversion-service="formattingConversionService"></mvc:annotation-driven>

b、
<!-- 自定义参数绑定 -->
<bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<!-- 日期类型转换 -->
<bean class="top.einino.controller.convert.StringToDateConverter"></bean>
</list>
</property>
</bean>

c、jsp界面

<form action="${pageContext.request.contextPath }/addBlog5.action" method="post">
创作时间:<input type="text" name="createTime"/></br>
<input type="submit" value="提交日期类型绑定"/>
</form>

d、controller接收参数

//pojo对象绑定日期类型,并实现自定义日期类型转换
@RequestMapping("/addBlog5.action")
public String addBlog5(Blog blog){
   System.out.println(blog.getCreateTime());
//重定向到blog列表
return "redirect:/queryBlog.action";
}

五、集合类型绑定

1、数组

a、jsp页面

<script type="text/javascript">
    function allSelect(allselect){
        var checks = document.getElementsByName("blog_id");
        for(var i=0; i<checks.length;i++){
            checks[i].checked = allselect.checked;
        }
    }

    function deleteBlogs(){
        if(window.confirm("确定要删除所选博文么")){
            var form1 =  document.getElementById("form1");
            form1.submit();
        }
    }

</script>

<input type="button" value="删除所选博文" οnclick="deleteBlogs()"/>

<form id="form1" action="${pageContext.request.contextPath}/deleteBlogs.action" method="post">
所有博文:
<table width="100%" border=1>
<tr>
<td><input type="checkbox" id="allselect" οnclick="allSelect(this)"/></td>
<td>博文id</td>
<td>博文标题</td>
<td>博文内容</td>
<td>博文发布时间</td>
<td>操作</td>
</tr>
<c:forEach items="${blogs }" var="blog">
<tr>
<td><input type="checkbox" name="blog_id" value="${blog.id }"/></td>
<td>${blog.id }</td>
<td>${blog.title }</td>
<td>${blog.content }</td>
<td><fmt:formatDate value="${blog.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td><a href="">修改</a>
<a href="${pageContext.request.contextPath }/deleteBlog.action?id=${blog.id}">删除</a>
</td>

</tr>
</c:forEach>

</table>
</form>

b、controller接收参数

//绑定数组
@RequestMapping("/deleteBlogs.action")
public String deleteBlogs(Integer[] blog_id){
for(Integer id:blog_id){
System.out.println(id);
}
//重定向到blog列表
return "redirect:/queryBlog.action";
}

2、绑定pojo中的list

a、在User类中加入List<Blog> blogs属性

b、jsp界面,模拟修改全部博文信息

<form id="form2" action="${pageContext.request.contextPath}/updateBlogs.action" method="post">
所有博文:
<table width="100%" border=1>
<tr>
<td><input type="checkbox" id="allselect" οnclick="allSelect(this)"/></td>
<td>博文id</td>
<td>博文标题</td>
<td>博文内容</td>
<td>博文发布时间</td>
<td>操作</td>
</tr>
<c:forEach items="${blogs }" var="blog" varStatus="status">
<tr>
<td><input type="checkbox" name="blog_id" value="${blog.id }"/></td>
    <td><input type="text" name="blogs[${status.index}].id" value="${blog.id }"/></td>
    <td><input type="text" name="blogs[${status.index}].title" value="${blog.title }"/></td>
    <td><input type="text" name="blogs[${status.index}].content" value="${blog.content }"/></td>
    <td><input type="text" name="blogs[${status.index}].createTime" value="<fmt:formatDate value="${blog.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
<td><a href="">修改</a>
<a href="${pageContext.request.contextPath }/deleteBlog.action?id=${blog.id}">删除</a>
</td>

</tr>
</c:forEach>
<tr><td colspan="6"><input type="submit" value="修改博文"/></td></tr>
</table>

</form>

c、controller接收

//绑定pojo对象中的list
@RequestMapping("/updateBlogs.action")
public String updateBlogs(User user){
List<Blog> blogs = user.getBlogs();
for(Blog blog : blogs){
System.out.println(blog.getTitle());
System.out.println(blog.getContent());
System.out.println(blog.getCreateTime());
}
//重定向到blog列表
return "redirect:/queryBlog.action";
}

3、绑定pojo中map集合

a、在User类中添加Map<String, Object>map属性

b、jsp界面

<form action="${pageContext.request.contextPath }/addUser.action" method="post">
用户id:    <input type="text" name="map['id']" /></br>
用户姓名:    <input type="text" name="map['username']" /></br>
<input type="submit" value="添加用户"/>
</form>

c、controller接收参数
//绑定map集合
@RequestMapping("/addUser.action")
public String addUser(User user){
   Map<String, Object> map = user.getMap();
Set<Entry<String, Object>> entrySet = map.entrySet();
for(Entry<String, Object> entry : entrySet){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
//重定向到blog列表
return "redirect:/queryBlog.action";
}

六、小结

本博文介绍了springmvc的五种参数绑定形式,并举例对它们进行了运用!

如果有疑问或者对本博文有何看法或建议或有问题的,欢迎评论,恳请指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值