SpringMVC学习笔记(四)
一.Controller接受网页参数.
1.使用方法的形参来接受
//使用基本类型和字符串来接受
@RequestMapping(value="/param2.do")
public String param(People p){
System.out.printlt(p.getName()+"===="+p.getAge());
return "param";
}
注意:该方法的形参一定要和网页参数名相同.而且这种方式可以自动转型.
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
}
//使用对象类型来接受
@RequestMapping(value="/param2.do")
public String param(People p){
System.out.printlt(p.getName()+"===="+p.getAge());
return "param";
}
2.使用request来接受基本类型.
@RequestMapping(value="/param.do")
public String param(HttpServletRequest request,HttpServletResponse response){
String name=request.getParameter("name");
String age=request.getParameter("age");
System.out.printlt(name+"===="+age);
return "param";
}
如果接受的类型为时间类型我们可以做如下方式来处理.
//boxing automatically
@RequestMapping("/person1")
public String toPerson(Person p){
System.out.println(p.getName()+" "+p.getAge());
return "hello";
}
//1.该方法只能适合本控制层.
@InitBinder
public void initBinder(ServletRequestDataBinder binder){
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
true));
}
//2.可以定义一个全局时间转化类.
public class DateConvert implements Converter<String, Date> {
@Override
public Date convert(String stringDate){
System.out.println("=======================_______");
//时间转化类(时间格式)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
在SpringMVC配置文件中声明该配置类
<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConvert"/>
</set>
</property>
</bean>
<!-- 第一步: 创建自定义日期转换规则 class:为时间转化类的全类名-->
<bean id="dateConvert" class="com.eduask.ykq.controller.DateConvert"/>
//3.RESTFul风格的SringMVC
3.1 RestController
@Controller
@RequestMapping("/rest")
public class RestController {
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
public String get(@PathVariable("id") Integer id){
System.out.println("get"+id);
return "/hello";
} @RequestMapping(value="/user/{id}",method=RequestMethod.POST)
public String post(@PathVariable("id") Integer id){
System.out.println("post"+id);
return "/hello";
} @RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
public String put(@PathVariable("id") Integer id){
System.out.println("put"+id);
return "/hello";
} @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
System.out.println("delete"+id);
return "/hello";
} }
3.2 form表单发送put和delete请求
在web.xml中配置
<!-- configure the HiddenHttpMethodFilter,convert the post method to put or delete -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.3 在前台可以用以下代码产生请求
<form action="rest/user/1" method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="put">
</form> <form action="rest/user/1" method="post">
<input type="submit" value="post">
</form> <form action="rest/user/1" method="get">
<input type="submit" value="get">
</form> <form action="rest/user/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="delete">
</form>
在web.xml中配置<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--这里需要注意的地方是 web.xml配置路径是不能使用.do必须使用/ 否则RESTFul风格无法接受到参数值报404
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
二.如何向网页响应数据
1.可以把数据保存在request对象中
//pass the parameters to front-end
@RequestMapping("/show")
public String showPerson(HttpServletRequest request,HttpServletResponse response){
Person p =new Person(); p.setAge(20);
p.setName("jayjay");
request.setAttribute("p",p);
return "show";
}
2.可以把数据保存在ModelAndView中 但是这种方式的方法的返回类型必须是ModelAndView
//pass the parameters to front-end
@RequestMapping("/show")
public ModelAndView showPerson(){
Person p =new Person(); p.setAge(20);
p.setName("jayjay");
ModelAndView andView=new ModelAndView("show");
andView.addObject("p",p);
return andView;
}
3.可以把数据保存在Model中
//pass the parameters to front-end
@RequestMapping("/show")
public String showPerson(Model model){
Person p =new Person(); p.setAge(20);
p.setName("jayjay");
model.addAttribute("p",p);
return "show";
}
4.可以把数据保存在Map中
//pass the parameters to front-end
@RequestMapping("/show")
public String showPerson(Map<String,Object> map){
Person p =new Person();
map.put("p", p);
p.setAge(20);
p.setName("jayjay");
return "show";
}
前台可在Request域中取到"p"
SpringMVC学习笔记(四)的更多相关文章
- SpringMVC 学习笔记(四) 处理模型数据
Spring MVC 提供了下面几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体就可以通过该对象加入模型数据 – Map及Model: ...
- SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理
SpringMVC的异常处理,SimpleMappingExceptionResolver只能简单的处理异常 当发生异常的时候,根据发生的异常类型跳转到指定的页面来显示异常信息 ExceptionCo ...
- SpringMVC 学习笔记(四)
41. 尚硅谷_佟刚_SpringMVC_返回JSON.avi SpringMVC中使用@ResponseBody注解标注业务方法,将业务方法的返回值做成json输出给页面 导包: 除了一些sprin ...
- SpringMVC学习笔记四:数据绑定
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831344.html 参考:http://www.cnblogs.com/HD/p/4107674.html ...
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻
前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
随机推荐
- 【python】python定时器
#coding:utf-8 import os import time def print_ts(message): print "[%s] %s"%(time.strftime( ...
- apache 使用 .htaccess 导致500错误
今天在win主机上配置了一个apache+mysql+php 的环境,一切看似正常了.结果将程序转移过来,打开网站的时候,出现了500错误.于是乎查原因: 首先,怀疑的是连接mysql出错了,找出配置 ...
- LVM在线扩容
我虚拟机根分区已经使用了35%,现在需要对他进行在线扩容,扩容之后使用率降到30% [root@localhost ~]# dfFilesystem 1K-blocks Used Available ...
- AMD高级应用(翻译)
Dojo now supports modules written in the Asynchronous Module Definition (AMD) format, which makes co ...
- 图片Exif 信息中Orientation的理解和对此的处理
这个问题是在用七牛上传图片后获取宽高时发现的,一张图片,用图片浏览器打开始终是竖图,但是查看属性或者用七牛获取宽高,却发现宽大于高,也就是在属性中这是个横图.这样导致客户端用该宽高来展示图片会出现问题 ...
- 在.htaccess文件中写RewriteRule无效的问题的解决
近来在Apache Rewrite 拟静态配置时,遇到个问题.写的如下: RewriteEngine onRewriteRule ^/t_(.*)/$ /test.php?id=$1 保存在httpd ...
- Oracle 记录插入时“Invalid parameter binding ”错误
出现这种错误的原因可能有一下几种: 由于OracleParameter[] parameters:中parameters的个数和对应的插入SQL语句中的冒号个数不等: 参数个数和冒号个数相等,但是如下 ...
- C++调用JAVA方法详解
C++调用JAVA方法详解 博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...
- Repeater控件使用中的一些小问题
网页上用来展示列表的数据,发现还是Repeater比GridView,DetailView之类的要灵活些,所以近期用到了就总结下遇到的一些情况,保留下来以备之后查阅,不用现问度娘了... 自己摸索的, ...
- 关于Oracle GoldenGate中Extract的checkpoint的理解 转载
什么是checkpoint? 在Oracle 数据库中checkpoint的意思是将内存中的脏数据强制写入到磁盘的事件,其作用是保持内存中的数据与磁盘上的数据一致.SCN是用来描述该事件发生的准确的时 ...