【SpringMVC】响应和请求

本文详细介绍了SpringMVC中数据响应的方式,包括页面跳转和回写数据,以及请求数据的获取,如基本类型、POJO、数组和集合参数。还讨论了参数绑定注解、Restful风格的请求、Servlet相关API的获取、请求头信息以及文件上传的处理。通过实例展示了如何处理请求数据乱码和放行静态资源等问题。
摘要由CSDN通过智能技术生成


提示:以下是本篇文章正文内容,下面案例可供参考

一、数据响应方式

1.页面跳转

1.1直接返回字符串

此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转

@RequestMapping("/quick")
public String quickMethod(//HttpServletRequest request){ 
	//向request域存储数据
	//request.setAttribute("name","zhangsan");
    return "index";
}
//转发:forward:/WEB-INF/views/index.jsp
//重定向:redirect:/index.jsp

1.2通过ModelAndView对象

@RequestMapping("/quick2")
public ModelAndView quickMethod2(){
   
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("redirect:index.jsp");
    向request域存储数据
    //modelAndView.addObject("name","lisi");
    return modelAndView;
}

2.回写数据

2.1直接返回字符串

  • 通过SpringMVC框架注入的response对象
//不需要视图跳转,业务方法返回值为void
@RequestMapping("/quick4")
public void quickMethod4(HttpServletResponse response) throws IOException {
   
    response.getWriter().print("hello world");
}
  • 回写的字符串直接返回
//通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回
@RequestMapping("/quick5")
@ResponseBody
public String quickMethod5() throws IOException {
   
    return "hello springMVC!!!";
}

2.2返回对象或集合

  • 配置注解驱动
//前提:mvc命名空间(略)
<!--mvc的注解驱动-->
<mvc:annotation-driven/>
/*作用
在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视图解析器称为 SpringMVC 的三大组件。
使用<mvc:annotation-driven>自动加载 RequestMappingHandlerMapping(处理映射器)和
RequestMappingHandlerAdapter( 处 理 适 配 器 ),可用在Spring-xml.xml配置文件中使用
<mvc:annotation-driven>替代注解处理器和适配器的配置。
同时使用<mvc:annotation-driven>默认底层就会集成jackson进行对象或集合的json格式字符串的转换。
*/

/*代替了在方法上添加@ResponseBody和如下配置
<bean class="org.springframework.web.servlet.mvc.method.annotation
             .RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json
                       .MappingJackson2HttpMessageConverter">
            </bean>
        </list>
    </property>
</bean>

  • 代码示例
@RequestMapping("/quick8")
@ResponseBody
public User quickMethod8() throws IOException {
   
    User user = new User();
    user.setUsername("zhangsan");
    user.setAge(18);
    return user;
}

二、获得请求数据

1.基本类型参数

  • Controller中的业务方法的参数名称要与请求参数的name一致,参数值会自动映射匹配
http://localhost:8080/itheima_springmvc1/quick9?username=zhangsan&age=12
@RequestMapping("/quick9")
@ResponseBody
public void quickMethod9(String username,int age) throws IOException {
   
    System.out.println(username);
    System.out.println(age);
}

2.POJO类型参数

  • Controller中的业务方法的POJO参数的属性名与请求参数的name一致,参数值会自动映射匹配
http://localhost:8080/itheima_springmvc1/quick9?username=zhangsan&age=12
public class User {
   
    private String username;
    private int age;
    getter/setter…
}
@RequestMapping("/quick10")
@ResponseBody
public void quickMethod10(User user) throws IOException {
   
    System.out.println(user);
}

3.数组类型参数

  • Controller中的业务方法数组名称与请求参数的name一致,参数值会自动映射匹配
http://localhost:8080/itheima_springmvc1/quick11?strs=111&strs=222&strs=333
@RequestMapping("/quick11")
@ResponseBody
public void quickMethod11(String[] strs) throws IOException {
   
    System.out
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值