SoftArea 2016-07-18 02:24 采纳率: 0%
浏览 2319

Struts2 OGNL表达式访问普通类的构造方法取不出值

ognl.jsp代码

 <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>OGNL</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
   Ognl Page!<br/>   
    <ol>
        <li>访问值栈中的普通属性   取出username:<s:property value="username"/></li>
        <li>访问值栈中的普通属性  取出password:<s:property value="password"/></li>
        <li>访问值栈中的对象的属性  取出user.Age:<s:property value="user.age"/></li>
        <li>访问值栈中的对象的属性  取出cat.dog.name:<s:property value="cat.dog.name"/></li>
        <li>访问值栈中的成员变量的普通方法 取出password的长度:<s:property value="password.length()"/></li>
        <li>访问值栈中的对象的普通方法 取出cat.miaomiao:<s:property value="cat.miaomiao()"/></li>
        <li>访问值栈中的action的普通方法 取出helloaction:<s:property value="helloaction()"/></li>
    </ol>
    <hr>
    <ol>
        <li>访问静态成员:<s:property value="@com.mytest.ognl.S@staticStr"/></li>
        <li>访问静态方法:<s:property value="@com.mytest.ognl.S@StaticMethord()"/></li>
        <!-- 静态方法的访问应该在配置struts.xml中配置
        <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
        默认的default.properties中属性值是false
        -->
        <li>访问Math类的方法:<s:property value="@@max(2,9)"/></li>
        <!-- 访问Math类的方法时候用两个@@符号,只限于访问Math类 -->
    </ol>
    <hr>
    <ol>
        <li>访问普通类的构造方法:<s:property value="new com.mytest.ognl.User(8)"/></li>
    </ol>
    <hr>
    <ol>
        <li></li>
    </ol>    
    <s:debug></s:debug>
  </body>
</html>

User.java代码

 package com.mytest.ognl;

public class User {

    private int Age;

    public User(){}

    public User(int Age){
        super();
        this.Age=Age;
    }

    public void setAge(int age) {
        Age = age;
    }

    public int getAge() {
        return Age;
    }

    public String toString(){
        return "user" + Age;
    }

}

执行结果:
图片说明

访问普通类的构造方法:那个地方的值总是取不出来?怎么回事呀?求大神帮忙给解决下。

  • 写回答

4条回答 默认 最新

  • SoftArea 2016-07-18 02:36
    关注

    在线等!求大神给解决

    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛