Struts2学习——1300OGNL入门
2016-10-08 14:46
302 查看
背景
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。这样可以更好的取得数据。OGNL入门
1. 分析与结果
本文是以结果导向分析的方式进行的。首先先看index.jsp的页面及代码
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% String contextPath = request.getContextPath(); %> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> 访问属性 <a href="<%=contextPath %>/ognl.action?username=u&password=p">ognl</a> </body> </html>
可以看出代码就一句,一条超链接,链接到ognl.action带着参数username=u,password=p。
分析struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <include file="/com/bjsxt/struts2/ognl/struts-ognl.xml"/> </struts>
include一个xml,struts-ognl.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="ognl" extends="struts-default"> <action name="ognl" class="com.bjsxt.struts2.ognl.OgnlAction"> <result>/ognl.jsp</result> </action> </package> </struts>
从上述代码可以得到,action名称为ognl,相关Action是OgnlAction,只有一个result,服务器端跳转到ognl.jsp。
OgnlAction
package com.bjsxt.struts2.ognl; import com.opensymphony.xwork2.ActionSupport; public class OgnlAction extends ActionSupport { private String password; private User user; private String username; public String execute() { return SUCCESS; } public String getPassword() { return password; } public void setPassword< b78c /span>(String password) { this.password = password; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
可以看出OgnlAction用到了两种参数的接收方法,一个是Action直接接收,一种是MainModel接收。username和password是用Action方式接收,而user是用ModelDomain方式接收。OgnlAction的execute方法返回success,服务器端跳转到ognl.jsp。
ognl.jsp
<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib uri="/struts-tags" prefix="s" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>OGNL表达式语言学习</title> </head> <body> <ol> <li>访问值栈中的action的普通属性: username = <s:property value="username"/> </li> <li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li> </ol> <s:debug></s:debug> </body> </html>
由ognl.jsp代码可知,页面用
<s:property>标签调用了value stack里面的username值以及user中age属性的值。结果如下图
页面结果表明,可以拿到username的值,但是拿不到user中age的值,这个结果是显而易见的,value stack中显示,user=null。说明struts并不是自动new出model对象,而是要通过相关参数传入,才会new出相关对象。将index.jsp超链接改为下述代码:
<a href="<%=contextPath %>/ognl.action?username=u&password=p&user.age=9">ognl</a>
再测试相应结果
从页面结果可以看到user.age=9,value stack中user也有了相应值。
2. 总结
从上述实验中可以发现,并不是Action定义所有的Domain Model都会自动初始化的,只有在参数中传入了model相关成员变量,如此例中的user.age,Struts才会自动创建出model对象。除此之外有两点注意事项。也可以在Action中自己new对象,这个时候,也能从value stack中取出值
如果让Struts自动创建对象,那么model中必须要有空参构造函数存在,否则无法自动创建对象。
若有不足之处,请不吝赐教
相关文章推荐
- Struts2 入门学习笔记(三)——数据转换
- Struts2入门学习(一)
- Struts2入门学习心得
- struts2入门学习--类型转换
- 学习SSH框架记录片1----Struts2入门
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<Struts2_实战演练(中)>(三十九)
- java struts2入门学习实例--使用struts2快速实现上传
- java 从零开始,学习笔记之基础入门<Struts2_实战演练(下)>(四十)
- struts2学习笔记 -- day01struts开发包详解、入门案例环境搭建
- STRUTS2学习(一)——入门程序及基本流程
- Struts2 入门学习笔记(一)——基本配置
- java 从零开始,学习笔记之基础入门<Struts2_实战演练(中)>(三十九)
- struts2学习入门前
- Struts2学习教程之入门小白的开始基础
- Struts2学习入门
- java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器
- java struts2入门学习---异常处理和类型转换
- java struts2入门学习---文件下载的二种方式
- [原]java专业程序代写(qq:928900200),学习笔记之基础入门<Struts2_实战演练(上)>(三十八)
- java struts2入门学习---国际化