参数类型
- 一些默认类型的参数,这些参数在使用过程中不需要做任何的声明,框架底层会使用AOP原理自动注入进来
- 返回模型和视图对象
@RequestMapping("method02")
public ModelAndView method02() {
//模型和视图对象:包括数据以及要跳转的视图
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");
//相当于req.setAttribute(name,value);
mv.addObject("name", "Tom");
return mv;
}
- Model参数直接注入在参数列表中,可以在其中添加要绑定的数据,返回值只需要指定逻辑视图名即可
public String method03(Model model) {
model.addAttribute("name", "Jack");
return "hello";
}
- ModelMap是Model的实现类,在注入Model类型的时候,SpringMVC会自动将Model类型实例化为ModelMap类型,所以Model和ModelMap用法上是一样的
- 参数为基本类型:可以和请求参数相对应
- 布尔类型可以传递true或false,也可以传递0或1
- 注意:请求参数和方法形参需要同名
- 如果请求参数和形式参数不同名,可以使用@RequestParam进行指定,在注解中设置形式参数的名字
- 以@RequestParam指定的参数默认是必须的
- 也可以设置为不是必须的@RequestParam(name = “salary”, required = false)
//方法中的形式参数不宜超过4个,如果超过4个将参数包装成一个对象
public String method05(Integer id, String name, @RequestParam Double sal, Boolean isMan) {
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("sal:" + sal);
System.out.println("isMan:" + isMan);
return "hello";
}
- 入参类型支持pojo类型
- 需要满足请求参数的名字和pojo中属性的名字保持一致
public String method06(UserInfoParam param) {
System.out.println(param);
return "hello";
}
- 如果要将前端的参数转换为服务端的某个特定的类型,也可以自定义类型转换器来实现
- step1:编写转换器类,该类需要实现Converter接口,并实现其中的转换方法
- step2: 配置转换器
//配置转换类 在package中
@Component("dateConverter")
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String dateStr) {
//方法参数就是前端传过来的String类型的值
//方法的返回值就是转换后得到的日期对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
//配置转换器 在xml中
<!--
配置自定义类型转换器
-->
<mvc:annotation-driven conversion-service="conversions"/>
<!--
注册转换器对象
-->
<bean id="conversions" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConverter"></ref>
</set>
</property>
</bean>
- 通过@ModelAttribute标注的参数,会自动写入request域中
public String method08(@ModelAttribute("user") UserInfoParam param) {
System.out.println(param);
return "hello";
}
- 数组集合类型
- Integer[]:正确
- List:错误
- ArrayList:没有错误,但是得不到正确的结果
- User[] :错误
- List :错误
- ArrayList:错误
- 在pojo类型中,以上所有的格式都是正确可以使用的
- Tomcat7.x版本以后服务器会自动对请求参数格式进行校验,其中类似于[,],.都认为是不安全的
- 需要在Tomcat的配置文件中配置允许使用这些字符
- 在Tomcat中config的server.xml中的端口中进行配置
- relaxedQueryChars="[]|{}^\`"<>"
@RequestMapping("method09")
public String method09(Integer[] ids, QueryParam param) {
System.out.println("形参中ids:" + Arrays.toString(ids));
System.out.println("=============================");
System.out.println("pojo中的ids:" + Arrays.toString(param.getIds()));
System.out.println("pojo中的idList:" + param.getIdList());
System.out.println("pojo中的idArrList:" + param.getIdArrList());
System.out.println("=============================");
System.out.println("pojo中的users:" + Arrays.toString(param.getUsers()));
System.out.println("pojo中的userList:" + param.getUserList());
System.out.println("pojo中的userArrList:" + param.getUserArrList());
System.out.println("=============================");
return "hello";
}
方法返回值类型
void返回值
public void method01(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/hello.jsp").forward(req, res);
}
@RequestMapping("method02")
public void method02(HttpServletResponse res) throws ServletException, IOException {
res.sendRedirect("http://www.baidu.com");
}
//额外,最优使用
@RequestMapping("method03")
public String method03() throws ServletException, IOException {
//加上 redirect: 前缀就会成为一个重定向操作
return "redirect:http://www.baidu.com";
}
//其他方法
@RequestMapping("method04")
public ModelAndView method04() throws ServletException, IOException {
ModelAndView mv = new ModelAndView();
//重定向视图
RedirectView rv = new RedirectView("http://www.baidu.com");
mv.setView(rv);
return mv;
}
@RequestMapping("method05")
public void method05(HttpServletResponse res) throws ServletException, IOException {
UserInfoParam param = new UserInfoParam();
param.setUserName("Tom");
param.setAge(18);
param.setMale(true);
AddressParam addressParam = new AddressParam();
addressParam.setProvince("js");
addressParam.setCity("sz");
param.setAddress(addressParam);
res.setContentType("application/json;charset=utf-8");
PrintWriter pw = res.getWriter();
pw.print(JSON.toJSONString(param));
}
/**
* SpringMVC中提供的JSON格式返回的操作
* <p>
* 在SpringMVC中所有的返回值都是以String来对待的
* 在SpringMVC配置文件中添加了mvc:annotation-driven,相当于注册了一些内置的转换器(也包括自己编写的转换器)
* 再结合@ResponseBody注解,框架就知道需要将返回的结果以json格式进行输出---默认使用的是jackson解析工具
*/
@RequestMapping("method06")
@ResponseBody
public UserInfoParam method06() throws ServletException, IOException {
UserInfoParam param = new UserInfoParam();
param.setUserName("Tom");
param.setAge(18);
param.setMale(true);
AddressParam addressParam = new AddressParam();
addressParam.setProvince("js");
addressParam.setCity("sz");
param.setAddress(addressParam);
return param;
}
@RequestMapping("method07")
public void method07(OutputStream os) throws IOException {
//从本地读取一张图片
Resource resource = new FileSystemResource("C:\\Users\\Administrator.USER-20190301IG\\Desktop\\IO.png");
InputStream is = resource.getInputStream();
//本质:本地读取图片对应的输入流写入响应输出流中
IOUtils.copy(is, os);
//FileCopyUtils
}