【问题标题】:Struts 2 OGNL . Not able to retrieve data using %{} syntaxStruts 2 OGNL 。无法使用 %{} 语法检索数据
【发布时间】:2016-11-14 13:19:02
【问题描述】:

我开发了一个简单的 Struts 2 项目。但是当我试图从 valuestack 中检索数据时,我没有得到任何数据。在提供页面时,基本上 %{} 语法无法识别。

我的索引.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="justwork.action">
<input type="text" name="name" value="Xarvis">
<input type="submit" >
</form>
</body>
</html>

我的 struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd"> 
<package name="TestFramework" extends="struts-default" >
<action name="justwork" class="org.pranab.actions.OGNLTest">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

我的 OGNLTest.java

 package org.pranab.actions;
 import com.opensymphony.xwork2.ActionContext;
 public class OGNLTest {

    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String execute(){
     return "success";
        }}

终于我的success.jsp

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
       <h2>SUCCESS</h2>

         <s:property default="%{name}" value="name"/>
         <br>
%{name}
</body>
</html>

提交时的输出:

Output

是否需要在 strust.xml 或属性中进行开关才能启用 %{} 语法??

【问题讨论】:

    标签: jsp struts2 el ognl struts-tags


    【解决方案1】:

    你搞错了:


    编辑

    default attribute is of type String,不是像value 这样的对象,因此不会被评估。

    在里面放一个静态字符串。

    【讨论】:

    • 感谢安德里亚的意见。但是当我在 struts 2 标签中使用 %{} 时,它也不起作用。
    • 是的,这就是值...。但假设我为 value 属性提供了 ValueStack 中不存在的另一个属性。像 。然后由于值堆栈中不存在值,因此我的默认属性应该接管。但是标签的输出也是:%{name}
    • 是的,默认属性是字符串类型,因此我使用 OGNL EL 表达式强制从 valuestack 中检索值,以便我的默认属性将存储动态值而不是静态值。这就是针对默认属性使用 EL 的全部意义
    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 2013-06-21
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2014-01-15
    • 2015-12-01
    相关资源
    最近更新 更多