springmvc中的数据回显以及jason数据交互

 数据回显

提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面。

1、springmvc默认对pojo数据进行回显。

springmvc默认支持pojo数据回显,springmvc自动将形参中的pojo重新放回request域中,request的key为pojo的类名(首字母小写)如下:

controller方法:

    @RequestMapping("/editItemSubmit")

    public String editItemSubmit(Integer id,ItemsCustom itemsCustom)throws Exception{

springmvc自动将itemsCustom放回request,相当于调用下边的代码

model.addAttribute("itemsCustom", itemsCustom);

jsp页面:


页面中的从“itemsCustom”中取数据。

 

如果key不是pojo的类名(首字母小写),可以使用@ModelAttribute完成数据回显。

@ModelAttribute作用如下:

1、绑定请求参数到pojo并且暴露为模型数据传到视图页面

此方法可实现数据回显效果。

// 商品修改提交

    @RequestMapping("/editItemSubmit")

    public String editItemSubmit(Model model,@ModelAttribute("item") ItemsCustomitemsCustom)

页面:

<tr>

    <td>商品名称</td>

    <td><input type="text"name="name" value="${item.name }"/></td>

</tr>

<tr>

    <td>商品价格</td>

    <td><input type="text"name="price" value="${item.price }"/></td>

</tr>

如果不用@ModelAttribute也可以使用model.addAttribute("item", itemsCustom)完成数据回显。

简单类型数据回显,

最简单方法是使用model。 

model.addAttribute("id", id);

2、 @ModelAttribute还可以将方法的返回值传到页面

在商品查询列表页面,通过商品类型查询商品信息。

在controller中定义商品类型查询方法,最终将商品类型传到页面。

//商品分类

    @ModelAttribute("itemtypes")

    public Map<String, String> getItemTypes(){   

       Map<String, String> itemTypes = newHashMap<String,String>();

       itemTypes.put("101", "数码");

       itemTypes.put("102", "母婴");      

       return itemTypes;

    }

 页面上可以得到itemTypes数据。

商品类型:

<select name="itemtype">

    <c:forEach items="${itemtypes }"var="itemtype">

       <option value="${itemtype.key }">${itemtype.value }</option>      

    </c:forEach>

</select>

 

Json数据交互

1.1.1 @RequestBody

作用:

@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容转换为json、xml等格式的数据并绑定到controller方法的参数上。 

本例子应用:

@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象

1.1.2 @ResponseBody

作用:

该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端

本例子应用:

@ResponseBody注解实现将controller方法返回对象转换为json响应给客户端

1.1.3 请求json,响应json实现:

1.1.3.1环境准备

Springmvc默认用MappingJacksonHttpMessageConverter对json数据进行转换,需要加入jackson的包,如下:

1.1.3.2配置json转换器

在注解适配器中加入messageConverters

 

<!--注解适配器 -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

       <property name="messageConverters">

       <list>

       <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>

       </list>

       </property>

    </bean>

 

注意:如果使用<mvc:annotation-driven/> 则不用定义上边的内容。 

1.1.3.3controller编写

// 商品修改提交json信息,响应json信息

    @RequestMapping("/editItemSubmit_RequestJson")

    public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) throws Exception {

       System.out.println(items);

       //itemService.saveItem(items);

       return items;

    }

1.1.3.4页面js方法编写:

引入 js:

<script type="text/javascript"

src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>

//请求json响应json

    function request_json(){

       $.ajax({

           type:"post",

           url:"${pageContext.request.contextPath}/item/editItemSubmit_RequestJson.action",

           contentType:"application/json;charset=utf-8",

           data:'{"name":"测试商品","price":99.9}',

           success:function(data){

              alert(data);

           }

       });

    }

1.1.4 请求为key/value,响应json实现:

表单默认请求application/x-www-form-urlencoded格式的数据即key/value,通常有post和get两种方法,响应json数据是为了方便客户端处理,实现如下:

1.1.4.1controller编写

   // 商品修改提交,提交普通form表单数据,响应json

    @RequestMapping("/editItemSubmit_ResponseJson")

    public @ResponseBody Items editItemSubmit_ResponseJson(Items items) throws Exception {

       System.out.println(items);

//     itemService.saveItem(items);

       return items;

    }

1.1.4.2页面js方法编写:

function formsubmit(){

    var user = " name=测试商品&price=99.9";

    alert(user);

      $.ajax(

       {

           type:'post',//这里改为get也可以正常执行

           url:'${pageContext.request.contextPath}/item/ editItemSubmit_RequestJson.action',

//ContentType没指定将默认为:application/x-www-form-urlencoded

           data:user,

           success:function(data){

              alert(data.name);

           }

          

       }  

    )

}

 

从上边的js代码看出,已去掉ContentType的定义,ContentType默认为:application/x-www-form-urlencoded格式。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值