springmvc学习小结二
来源:互联网 发布:fiiish旅行数据 编辑:程序博客网 时间:2024/05/29 19:52
controller返回值
1. 返回modelAndView,可以通过model.setObject,model.setname将值转发到相应视图
2. 返回void
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
使用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()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。
由于新发起一个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中的方法形参的名称一致时候会绑定
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']"/>
controller 中方法形参定义
public String test(Demo demo){
.....
}
- springmvc学习小结二
- springmvc学习小结(二)
- 小结SpringMVC(二)
- springmvc学习小结
- springmvc学习小结
- Springmvc学习小结
- SpringMVC学习小结
- Springmvc学习小结
- 【SpringMVC】学习小结
- springmvc学习笔记(22)-springmvc开发小结
- springmvc学习笔记(22)-springmvc开发小结
- Springmvc学习小结(一)
- SpringMVC学习之前期小结
- SpringMVC学习笔记二
- springMVC学习(二)
- SpringMVC学习总结(二)
- SpringMVC学习(二)
- springmvc 学习(二)
- Android中Activity之间传递数据详解
- 协同过滤推荐算法的原理及实现
- intellij idea 破解
- springboot+swagger2说明
- oracle 创建用户和赋予权限
- springmvc学习小结二
- MySQL小误区:关于set global sql_slave_skip_counter=N 命令的一些点
- android 主线程和子线程之间的消息传递
- 通过configSource提高web.config配置灵活性
- EventBus使用详解-2-用RxJava实现事件总线(Event Bus)
- 微信小程序开发之录音机 音频播放 动画 (真机可用)
- MFC主窗口可拖入文件
- 机器学习 之 LBP特征
- android网络编程之HttpUrlConnection的讲解--POST请求