spring MVC学习笔记(三)参数绑定

配置文件介绍

    <!--开启注解扫描-->
    <context:component-scan base-package="com.hw"></context:component-scan>

    <!-- 加载注解驱动 选择最新的注解处理器映射器和注解处理器适配器-->
    <mvc:annotation-driven/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <!-- jsp前缀 -->
        <property name="prefix" value="/static/"/>
        <!-- jsp后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>

默认的参数绑定有如下的方式

HttpServletRequest
HttpServletResponse
HttpSession
Model
即方法可以写成如下的方式

@RequestMapping("/***")
public String test(HttpServletRequest request, HttpSession httpSession, HttpServletResponse reponse, Model model){}

其中的参数按需添加,用到什么添加什么
而返回的String是去掉视图的前缀和后缀,视图的前缀和后缀会由视图解析器自动补齐
spring MVC可以支持基本数据类型String,还有pojo数据的接收,对于pojo类型的数据接受需要页面的input框的name属性和pojo类的属性一致

spring MVC传参乱码问题的解决

在web.xml中添加如下配置

    <!--统一请求编码-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <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>

List参数绑定
首先定义一个vo,用来接收浏览器传来的数据

public class QueryVo {
    private List<Item> itemList;

    //get 和 set方法省略
}

设计JSP页面

<form action="${pageContext.request.contextPath }/updateAll.do" method="post">
    查询条件:
    <table width="100%" border=1>
        <tr>
            <td><input type="submit" value="修改全部"/></td>
        </tr>
    </table>
    商品列表:
    <table width="100%" border=1>
        <tr>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemList }" var="item" varStatus="status">
            <%--<tr>
                <td>${item.name }</td>
                <td>${item.price }</td>
                <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td>${item.detail }</td>

                <td><a href="${pageContext.request.contextPath }/itemEdit.do?id=${item.id}">修改</a></td>

            </tr>--%>
            <tr>
                <td><input name="ids" value="${item.id}" type="checkbox"></td>
                <td>
                    <input name="itemList[${status.index}].id" value="${item.id}" type="hidden">
                    <input name="itemList[${status.index}].name" value="${item.name }" type="text">
                </td>
                <td><input name="itemList[${status.index}].price" value="${item.price }" type="text"></td>
                <td><input name="itemList[${status.index}].createtime"
                           value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"
                           type="text"></td>
                <td><input name="itemList[${status.index}].detail" value="${item.detail }" type="text"></td>
                <td><a href="${pageContext.request.contextPath }/itemEdit.do?id=${item.id}">修改</a></td>
            </tr>
        </c:forEach>

    </table>
</form>

设计Controller

@Controller
public class ItemController {
    @Resource
    ItemService itemService;

    @RequestMapping("/itemList")
    public ModelAndView itemsList()throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        List itemList=itemService.getItems();
        modelAndView.addObject("itemList",itemList);
        modelAndView.setViewName("itemList");
        return modelAndView;
    }
    @RequestMapping("/itemEdit")
    public String editItem(Model model,Integer id)throws Exception{
        model.addAttribute("item",itemService.getItemById(id));
        return "editItem";
    }
    @RequestMapping("/updateitem")
    public String updateItem(Item item){
        itemService.updateById(item);
        return "success";
    }

    @RequestMapping("/updateAll")
    public String updateAll(QueryVo queryVo){
        System.out.println(queryVo.getItemList());
        return "success";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值