springmvc学习小结二

来源:互联网 发布:fiiish旅行数据 编辑:程序博客网 时间:2024/05/29 19:52

controller返回值

1. 返回modelAndView,可以通过model.setObject,model.setname将值转发到相应视图

2. 返回void

controller方法形参上可以定义requestresponse,使用requestresponse指定响应结果:

使用request转向页面,如下:

request.getRequestDispatcher("页面路径").forward(request, response);

通过response页面重定向:

response.sendRedirect("url")

通过response指定响应结果,例如响应json数据如下:

response.setCharacterEncoding("utf-8");

response.setContentType("application/json;charset=utf-8");

response.getWriter().write("json串");

3. 返回字符串

* controller返回逻辑视图名,通过视图解析器解析为物理视图地址

* Contrller方法返回结果重定向到一个url地址,如下商品修改提交后重定向到商品查询方法,参数无法带到商品查询方法中。

redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的requestresponse

由于新发起一个request原来的参数在转发时就不能传递到下一个url,如果要传参数可以/item/queryItem.action后边加参数,如下:

/item/queryItem?...&…..

controller方法返回结果重定向到一个url地址,例如 return “redirect:/item/queryItem.action”,但是由于重定向之前请求的参数无法携带到下一个action,如果要

传递参数可以使用/item/queryItem?name=&age=

* controller 返回结果是转发到一个url地址,例如 return “forward:/item/item.action”,转发和之前的请求共用一个request和response,因而之前请求的参数转发后也能读取到


参数绑定:

@requestParam用于绑定单个请求参数

value: 参数名字,即入参的请求参数名字,如value=“item_id”,表示请求的参数区中的名字为item_id的参数值将传入

required:是否必须,默认是true,表示请求中要有相应的参数,否则将报TTp Status 400- Requeired Integer parameter XXX is not present

default Value;默认值,表示如果请求中没有同名的参数时候的默认值

例如:public String editItem (@requestParam(value="item_id",required="true")String id ){

}

形参名称为id,但是这里使用value=" item_id"限定请求的参数名为item_id,所以页面传递的值必须为item_id

注意:如果请求参数中没有item_id将跑出异常:

HTTP Status 500 - Required Integer parameter 'item_id' is not present

这里通过required=true限定item_id参数为必需传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传item_id参数值


简单类型: 当请求的参数和controller中的方法形参的名称一致时候会绑定

pojo:

1. 简单的pojo类型:

如果请求参数和pojo类型的属性一致,在controller的方法形参中使用pojo类型接受

<input type="text" name="age"/>

<input type='text" name ="height"/>

public String editPerson(Person p){

.......

}

2.包装pojo:

如果采用类似struts中对象.属性的方式命名,需要将pojo对象作为一个包装对象的属性,action中以该包装对象作为形参。

包装对象定义如下:

public class ProductQueryVo{

       private Product product;

}

jsp页面上使用<input type="text" name="product.name"/> <input type="text" name="product.price"/>

action中使用public String product(ProductQueryVo vo){

}

这样也能将参数传入

3. modelAttribute

@ModelAttribute作用如下:

1、绑定请求参数到pojo并且暴露为模型数据传到视图页面此方法可实现数据回显效果。

@requestMapping("/test")

public String test(@modelAttribute Item item){

}

在jsp页面使用el表达式获取

<input typ="text" name="" value="${item.price}">

2.将方法的返回值返回为模型数据返回到jsp页面

@ModelAttribute("t")

public Map<String,String > tsst(){

}

jsp页面上

<c:foreach item="${t}" items="map">

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

</foreach>

集合类:

字符串->>> public String test(String[]id)

list:

pojo中有list属性,并且list中存放对象,例如

public class Demo{

   private List<Person> list;

//必须有set/get方法

}

如果action方法中的形参为demo

jsp页面需要使用

<input type="text" name="list[1].name" />

<input type="text" name="list[1].age" />

实现参数绑定

Map:

包装类中定义map对象,并生成get,set方法

public class demo{

    private Map<String,String> map = new hashMap<String,String>();

//set,get方法

}

页面定义如下:

<input type="text" name="map['name']"/>

<input type="text' name ="map['age']">

controller 中方法形参定义

public String test(Demo demo){

.....

}

0 0
原创粉丝点击