SpringMVC的请求(上)

本文阅读 3 分钟
首页 代码,Java 正文

文章目录 文章目录 @TOC 一、获得请求参数 二、获得基本类型参数 三、获得pojo类型参数 四、获得数组类型参数 五、获得集合类型参数 六、请求数据乱码 七、参数绑定注解 八、获得Restful风格参数 九、自定义类型转换器 十、获得Servlet相关的API 十一、获得请求头 十二、@CookieValue

img

img

@RequestMapping(value = "/quick11")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save11(String userName,int age) throws IOException { 
        System.out.println(userName);
        System.out.println(age);
 //http://localhost:8080/SpringMVC/user/quick11?userName=jack&age=17
    }

img

@RequestMapping(value = "/quick12")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save12(User user) throws IOException { 
        System.out.println(user);
//http://localhost:8080/SpringMVC/user/quick12?username=jack&age=22
    }

img

@RequestMapping(value = "/quick13")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save13(String [] strs) throws IOException { 
        System.out.println(Arrays.asList(strs));
 //http://localhost:8080/SpringMVC/user/quick13?strs=111&strs=222&strs=333

    }

img form.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 86166
  Date: 2022/6/29
  Time: 17:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/quick14" method="post">
    <input type="text" name="userList[0].username">
    <input type="text" name="userList[0].age">
    <input type="text" name="userList[1].username">
    <input type="text" name="userList[1].age">
    <input type="submit" value="提交">

</form>

</body>
</html>

VO:

package com.study.domain;

import java.util.List;

public class VO { 
    private List<User> userList;

    public List<User> getUserList() { 
        return userList;
    }

    public void setUserList(List<User> userList) { 
        this.userList = userList;
    }

    @Override
    public String toString() { 
        return "VO{" +
                "userList=" + userList +
                '}';
    }
}
@RequestMapping(value = "/quick14")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save14(VO vo) throws IOException { 
        System.out.println(vo);
    }

img

<!-- 配置全局过滤filter-->
    <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>
@RequestMapping(value = "/quick14")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save14(VO vo) throws IOException { 
        System.out.println(vo);
    }

当请求参数名称与Controller的业务方法参数不一致时,需要通过@RequestParam注解显示绑定。 img

@RequestMapping(value = "/quick15")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save15(@RequestParam(value = "name",required = false,defaultValue = "jack")String userName) throws IOException { 
        System.out.println(userName);

    }

img img

//http://localhost:8080/SpringMVC/user/quick16/jack
    @RequestMapping(value = "/quick16/{username}")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save16(@PathVariable("username") String userName) throws IOException { 
        System.out.println(userName);

    }

img 1.实现DateConverter接口:

package com.study.converter;
import org.springframework.core.convert.converter.Converter;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter implements Converter<String,Date> { 

    @Override
    public Date convert(String dateStr) { 
        //将日期字符串转换为日期对象 返回
        Date date=null;
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        try { 
            date = d.parse(dateStr);
        } catch (ParseException e) { 
            e.printStackTrace();
        }

        return date;
    }
}

2.声明转换器:

<!--声明转换器-->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.study.converter.DateConverter"></bean>
            </list>
        </property>
    </bean>

3.在annotation-driven中引用转换器

<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
@RequestMapping(value = "/quick17")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save17(Date date) throws IOException { 
        System.out.println(date);

    }

img

@RequestMapping(value = "/quick18")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save18(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { 
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);

    }

img

@RequestMapping(value = "/quick19")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save19(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException { 
        System.out.println(user_agent);


    }

img

@RequestMapping(value = "/quick20")
    @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
    public void save20(@CookieValue(value = "JSESSIONID",required = false) String jsessionId) throws IOException { 
        System.out.println(jsessionId);


    }
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125530207
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复