Struts2学习——1300OGNL入门

来源:互联网 发布:手机excel求和软件 编辑:程序博客网 时间:2024/05/29 16:48

背景

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(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中必须要有空参构造函数存在,否则无法自动创建对象。

若有不足之处,请不吝赐教

0 0
原创粉丝点击