SSH框架笔记之(struts篇-请求数据处理)

1.请求参数封装,类型转换,校验,国际化信息提示,拦截器

 

        对于mvc模式的web层,必须要负责解析http请求参数,并将其封装到model对象中

        StrutsPrepareAndExecuteFilter:控制器

         jsp:视图

         Action:模型或者控制器

 

2.struts2(强大的类型装换机制)来完成-----------------------属性驱动和模型驱动

参数拦截器完后的参数封装ParameterInterceptor

                              1.action本身作为model对象,通过成员setter封装(属性驱动)

                                  因为是属性驱动,它是多例的(不会有多线程问题),struts1是单例的(有多线程问题,多请求中当有属性重复时,参数解析就会错误),action在此作model,但又不能将action传到业务层,故将action在赋值给javaBean传递

    <input type="text" name="name"/>

public class UserAction extends ActionSupport{
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    @Actions(value={
            @Action(value = "login",results={@Result(name="success",location="/index.jsp")}),
            @Action(value = "login1",results={@Result(name="success",location="/index.jsp")})
    })
    public String execute() throws Exception {
         User user=new User();
          user.setName(name);
          user.setAge(age);
        System.out.println(name+": "+age);
        //ServletActionContext.getResponse().
        return SUCCESS;
    }
}

                               2.创建独立model对象,通过ongl表达式封装(属性驱动)

                                 <input type="text" name="user.name"/>

                               ognl的写法。它会调用Action.setUser(),对象的setName()

灵活,多model可放到一个表单中:<input type="text" name="user.name"/>

                                                           <input type="text" name="product.name"/>

 

public class UserAction extends ActionSupport{
    private User user;
    

    public void getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(user.age);
        return SUCCESS;
    }
}

 

                              3.使用modelDriver接口,对请求数据进行封装。(模型驱动)

它使用了两个拦截器来完成参数的封装到model对象中ParameterInterCeptor+ModelDrivenInterceptor

//ModelDriven<T>  ,ModelDriven<Object>  泛型,用来指定model对象类型
public class UserAction extends ActionSupport implements ModelDriven<UserEntity> {
    //这里必须手动实例化
    private UserEntity user;
    @Override
    @Actions(value={
            @Action(value = "login",results={@Result(name="success",location="/index.jsp")}),
            @Action(value = "login1",results={@Result(name="success",location="/index.jsp")})
    })
    public String execute() throws Exception {
        System.out.println(user.getName());
        //ServletActionContext.getResponse().
        return SUCCESS;
    }

    @Override
    //返回model对象,将参数封装到返回的model对象中,同时也指定了模型model(对象)
    public UserEntity getModel() {
        return user;
    }
}

 

封装复杂类型(collection【就是list数组】和map【map和collection比就是多了个key】)

 

<input type="text" name="user[0].name"/>

 <input type="text" name="user[3].name"/>

public class UserAction extends ActionSupport{
    private List<User> user;
    

    public List<User> getUser() {
        return user;
    }

    public void setUser(List<User> user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(user.age);
        return SUCCESS;
    }
}

封装到map

<input type="text" name="map['one'].name"/>

 <input type="text" name="map['three'].name"/>

public class UserAction extends ActionSupport{
    private Map<String,User> map;
    

    public Map<String,User> getMap() {
        return map;
    }

    public void setMap(Map<String,User> map) {
        this.map = map;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(map.get("one"));
        return SUCCESS;
    }
}

struts后台校验前台提交的表单数据的正确性:

        在action所在包下创建校验的xml,命名方式:Action类名-validation.xml(针对所有方法进行校验)

                                                               更换命名:Action类名-【方法的访问路径】user_register-validation.xml=======表示只对user类的regisetr方法进行校验。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值