SpringMVC学习(二)
springMvc中默认支持的参数类型
有:HttpServletRequest、HttpServletResponse、HttpSession、Model(包含ModelMap–Model的一个子类),这些数据类型在controller方法中可以加入也可以不加, 看是否需要用到。
看注释学习吧
@RequestMapping("/itemEdit")//匹配浏览器页面URL
public String itemEdit(HttpServletRequest reuqest, Model model) throws Exception{
/**
*获取页面传过来的id(下面是页面编辑代码,路径里有URL也有传值(?之后部分))
*<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
*/
String idStr = reuqest.getParameter("id");
Items items = itmesService.findItemsById(Integer.parseInt(idStr));
//Model模型:模型中放入了返回给页面的数据
//model底层其实就是用的request域来传递数据,但是对request域进行了扩展.
model.addAttribute("item", items);
//如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
return "editItem";//这里因为在springMVC中配置了视图解析器,所以路径名省略写
}
视图解析器配置点击 配置视图解析器
post请求乱码问题
在web.xml中配置
这里的filter是由spring提供的
<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>
get请求乱码问题
两种解决方案
1) 修改Tomcat的核心配置文件:
在其端口参数部分加上 URIEncoding="utf-8" 即:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
2)对参数进行重新编码:
String userName=new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
参数绑定
参数绑定即从请求中接收参数
- springMvc中默认支持的参数类型
这一点在上面已经讲过,不再复述。 - springMvc可以直接接收基本数据类型
基本数据类型:string,double,float,integer,long,boolean
springMvc可以直接接收基本数据类型,包括string.spirngMvc可以帮你自动进行类型转换,但是controller方法接收的参数的变量名称必须要等于页面上input框的name属性值 springMvc可以接收pojo类型
要求页面上input框的name属性名称必须等于pojo的属性名称。自定义转换器
作用:由于springMvc无法将string自动转换成date所以需要自己手动编写类型转换器
1)需要编写一个类实现Converter接口
/**
7. S - source:源
8. T - target:目标
*/
public class CustomGlobalStrToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(source);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
2)在springMvc.xml中配置自定义转换器
<!-- 配置自定义转换器
注意: 一定要将自定义的转换器配置到注解驱动上
-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 指定自定义转换器的全路径名称 -->
<bean class="cn.itheima.controller.converter.CustomGlobalStrToDateConverter"/>
</set>
</property>
</bean>
3)在springMvc.xml中将自定义转换器配置到注解驱动上
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
5.也可以接收vo类型
页面上input框的name属性值必须要等于vo中的属性.属性.属性….
1)此时vo中代码:
public class QueryVo {
//商品对象
private Items items;
//订单对象...
//用户对象....
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
}
2)jsp中代码:
<!-- 如果Controller中接收的是Vo,那么页面上input框的name属性值要等于vo的属性.属性[.属性.....] -->
<td>商品名称:<input type="text" name="items.name"/></td>
<td>商品价格:<input type="text" name="items.price"/></td>
3)controller中代码:
//如果Controller中接收的是Vo,那么页面上input框的name属性值要等于vo的属性.属性.属性.....
@RequestMapping("/search")
public String search(QueryVo vo) throws Exception{
//操作
return "返回的页面";
}
高级参数绑定
1.数组类型的参数绑定
2.List类型的绑定
不想细化写了,参考小雪雪的吧:高级参数绑定
354

被折叠的 条评论
为什么被折叠?



