Spring 请求参数封装 与过滤器

            封装单个字符串                  封装JavaBean对象                    封装 列表  集合类型

            过滤器                                自定义类型转换器                      获取原生ServletApi

 封装单个字符串  

<a href="params/demo1?name=wyc&password=132">参数</a>
    @RequestMapping(path = "/demo1")
    public void sayHello(String name ,String password){
        System.out.println(name+": "+password);    wyc: 123
    }   

   Spring   会自动 根据请求的参数名字  封装到方法的参数中去

封装JavaBean对象

public class Account implements Serializable {
    private Integer id;
    private String address;    
}
public class User implements Serializable {
    private String username;
    private String password;
    private Integer money;

    private Account account;  包含另一个对象的引用
}   生成get set toString 方法
     -封装Bena对象-
    <form action="params/demo2" method="post"><br/>
        用户名:<input type="text" name="username"><br/>
        密码:  <input type="text" name="password"><br/>
        年龄:  <input type="text" name="money"><br/>

        账户id:<input type="text" name="account.id"><br/>   引用对象的属性名
        账户地址:<input type="text" name="account.address"><br/>
        <button type="submit">提交</button>
    </form>

name属性的值  必须和 Bena对象中  set方法的名字 一样 ,   对于引用型数据,必须    属性名.名  才能封装进去

@Controller
@RequestMapping(path = "/params")
public class ParamsController {

    @RequestMapping(path = "/demo2")
    public void saveUser(User user){     直接传入 User 类型的对象
        System.out.println(user);    
    }} 

User{username='wyc', password='123456', money=18, account=Account{id=1, address='China'}}

封装 列表  集合类型

public class Account implements Serializable {
    private Integer id;
    private String address;}
public class UserList implements Serializable {
    private String username;
    private String password;
    private Integer money;

    private List<Account> accountList;       将多个账户封装在列表中
    private Map<String,Account> accountMap;  将账户封装在map集合的value中
}
    封装集合 列表-
    <form action="params/demo3" method="post"><br/>
        用户名:<input type="text" name="username"><br/>
        密码:  <input type="text" name="password"><br/>
        年龄:  <input type="text" name="money"><br/>

        账户1的id:<input type="text" name="accountList[0].id"><br/>
        账户1的地址:<input type="text" name="accountList[0].address"><br/>

        账户2的id:<input type="text" name="accountList[1].id"><br/>
        账户2的地址:<input type="text" name="accountList[1].address"><br/>

        账户3的id:<input type="text" name="accountMap['tree'].id"><br/>
        账户3的地址:<input type="text" name="accountMap['tree'].address"><br/>

        <button type="submit">提交</button>
    </form>

  对于列表   用列表名[ 索引 ] .属性名           对于集合       集合名["键的值"]. 属性名

    @RequestMapping(path = "/demo3")
    public void saveUserList(UserList user){
        System.out.println(user);
    }
UserList{username='wyc', password='123456', money=18, accountList=[Account{id=1, address='Chian'}, Account{id=2, address='jopan'}], accountMap={tree=Account{id=3, address='America'}}}

过滤器

  在post 请求中 tomcat 服务器  会产生中问乱码问题

 1.在 web.xml 中配置 过滤器

  配置过滤器
  <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>

 2.User{username='wyc', password='123456', money=18, account=Account{id=1, address='中国'}}

  自定义类型转换器

  Spring  会完成 一些格式的类型自动转换  列如日期  2018/9/02      当输入2018-8-09  就会封装失败 报错  所以要自己写一个转    换类

    -自定义类型转换
    <form action="params/demo4" method="post"><br/>
        用户名:<input type="text" name="username"><br/>
        密码:  <input type="text" name="password"><br/>
        生日: <input type="text" name="date"><br/>
        <button type="submit">提交</button>
    </form>
public class User implements Serializable {
    private String username;
    private String password;
    private Date date;}  

  自定义转换类   必须实现 Converter接口   泛型 把String转换成 Date日期格式   ,然后实现 convert方法  写转换逻辑 

public class DateUtils implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        if (source==null){
            throw new RuntimeException("没有输入日期");
        }
        DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(source);
        } catch (Exception e) {
            throw new RuntimeException("输入格式有问题");
        }}}
 配置自定义转换类型
    <bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">   固定的
        <property name="converters">  固定的
            <set>
                <bean class="com.utils.DateUtils"></bean>   写入自定义类
            </set>
        </property>
   </bean>

 <mvc:annotation-driven conversion-service="conversionServiceFactory"></mvc:annotation-driven>   开启自定义

@RequestMapping(path = "/demo4")
    public void Date(User user){
        System.out.println(user);
    }
User{username='15614140557', password='123456', date=Thu Aug 15 00:00:00 CST 2019}

获取原生ServletApi

  直接在方法的 参数类   要什么写什么

     @RequestMapping(path = "/Servlet")
    public void Servlet(HttpServletRequest request, HttpServletResponse response,){
        HttpSession session = request.getSession();
        String servletPath = request.getServletPath();
        ServletContext servletContext = session.getServletContext();
        
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值