1

I have developed a simple Struts 2 project.but when I am trying to retrieve the data from valuestack I am not getting any data. Basically %{} syntax is not recognized while the page is getting served.

My Index.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>

My 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>

my 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";
        }}

finally my 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 on Submit:

Output

Is there a requirement of a switch in strust.xml or properties to enable the %{} syntax??

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243

1 Answers1

0

You're making confusion:


EDIT

default attribute is of type String, not Object like value, hence it won't be evaluated.

Put a static String in it.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank Andrea for input. But when I am using %{} inside the struts 2 tags then also it's not working. – Pranab Bharadwaj Nov 14 '16 at 14:41
  • Yes that's the value .... but suppose I provided the value attribute with another property that doesn't exist in ValueStack. like . Then as value doesn't exist in value stack then my default attribute should take over . But then also the output from the tag is : %{name} – Pranab Bharadwaj Nov 15 '16 at 02:20
  • yes,default attribute is of strig type, hence I am using OGNL EL expression to force retrival of value from valuestack so that my default attribute will store a dynamic value instead of static one. That's the whole point of using EL against the default attribute – Pranab Bharadwaj Nov 29 '16 at 02:20