Struts2 简介(二)
来源:互联网 发布:生死狙击软件免费版 编辑:程序博客网 时间:2024/05/29 03:08
1.Action访问ServletAPI
1.1 ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
1.2 实现ServletRequestAware 接口
原因: 在执行动作之前有拦截器给你注入这些实例 interceptor — servletcofig
2 结果视图 Result
2.1 result元素的配置
属性:name:指定配置逻辑视图名,对应着动作方法的返回值。Name默认值:success。
type:到达目标的形式,默认值为dispatcher 转发
2.2 Struts2 提供的结果集类型
1、chain:用户转发到另一个动作
2、dispatcher:转发到tsp页面
3、redirect:用户重定向到另外一个jsp页面
4、redirectAction:用户重定向到另外一个action
5、stream:用于想浏览器返回一个Inputstream的结果类型。用户文件上传下载。
6、plainText:用于显示某个页面的原始代码的结果类型
2 .数据封装
作为 MVC 框架, 必须要负责解析Http请求参数, 并将其封装到Model 对象中Struts2 提供了非常强大的类型转换机制用于请求数据封装到model对象.
2.1 静态参数封装(了解)
<package name="staticparam" extends="struts-default"> <action name="staticParam" class="StaticParamAction的路径" method="login"> <param name="name">隔壁老王 </param> <param name="password">222222</param> </action> </package> //action中要写set/get方法 //底层依靠 staticParams 拦截器, 将静态参数放入值栈中,而action就在值栈中的栈顶, 自然就会找到该action中的对应属性,然后进行赋值.
2.2 动态参数封装
2.2.1 属性驱动
2.2.1.1 普通属性驱动,提供get/set方法
Action: public class UserAction extends ActionSupport{ private String name; private String password; public String login(){ System.out.println("u:"+name+"p:"+password); return null; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}struts.xml: <package name="param" extends="struts-default"> <action name="login1" class="UserAction的路径" method="login"> </action> </package>jsp:<form action="${pageContext.request.contextPath}/login1.action"> <label>姓名:</label> <input type="text" name="name"> <br> <label>密码:</label> <input type="password" name="password"> <br> <input type="submit" value="提交"></form>//跟静态参数封装一样,只不过这里获取的是表单中的参数,也就是请求发送过来的数 据。依靠的拦截器为params. 其中该拦截器做的事有两件,一是对提交的参数进行数据 校验,判断是否合法,判断是否合法的标准就是拦截器中的excludeParams参数的正则 表达式的值。二是将其封装到值栈中的栈顶元素中去,而当前action就在栈顶,所以能 够将参数放入action中。
2.2.1.2 ognl表达式来封装数据
Action:public class PersonAction extends ActionSupport { private Person person; //person中有 name,password 及相应的get/set方法 public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public String login(){ System.out.println(person); return null; }}jsp:<form action="${pageContext.request.contextPath}/login2.action"> <label>姓名:</label> <input type="text" name="person.name"> //利用 ognl 表达式 <br> <label>密码:</label> <input type="password" name="person.password"> <br> <input type="submit" value="提交"></form>//在jsp页面中的 person.username 和person.password 其实就是ognl表达式,代表着往根 (root,值栈valueStack)中存放值,而值栈中的栈顶元素也就是为当前action,我们在 action中设置person的get、set属性,即可以让存进来的值匹配到,进而将对应属性赋值成功
2.2.2模型驱动
Action:public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ private Customer customer=new Customer(); //Customer 是一个普通的javaBean 里面有 name,password对应的get/set方法 public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public String login(){ System.out.println(map.toString()); return ERROR; } @Override public Customer getModel() { //需要实例化 return customer; }}jsp:<form action="${pageContext.request.contextPath}/login3.action"> <label>姓名:</label> <input type="text" name="name"> <label>密码:</label> <input type="password" name="password"> <br> <input type="submit" value="提交"> </form>//必须实现 ModelDriven 接口,提供一个方法getModel(),初始化对象实例。 modelDriven 拦截器将getModel方法返回的结果压入值栈,而我们的表单参数会从值栈中从上往下进行查找,自然就直接将参数封装到对象中了。
阅读全文
1 0
- Struts2 简介(二)
- Struts2 简介(三)
- Struts2系列(1)---Struts2框架简介
- Struts2入门基础之struts的简介和体系结构(二)
- 深入struts2(二)
- Struts2 i18n(二)
- struts2学习(二)
- struts2从零开始(二)
- struts2 (二)
- struts2(二)
- Struts2标签(二)
- struts2笔记(二)
- struts2 实战(二)
- Struts2框架(二)
- 深入浅出struts2(二)
- struts2 入门(二)
- struts2(二)
- struts2(二)
- python学习(4)条件判断
- 基于opencv的双目标定
- Ununtu 16.04 安装 Anaconda详解
- Linux命令源代码阅读yes
- linux关闭触摸板命令
- Struts2 简介(二)
- SAP库存库龄逻辑分析
- Spring:源码解读Spring IOC原理
- 颜色匹配 opencv版
- Java枚举
- CentOS7安装Gradle
- ionic中Node Sass does not yet support your current environment解决办法Node sass安装
- n皇后问题
- 解决Visual Studio 2012 软件问题