JPG
JPG

Reputation: 1253

Unable to get value from value stack in struts2 using <s:action>

What I want: I want to retrieve the value from List collection.

I’m practicing/learning struts 2 framework. But, I am confused about OGNL behavior. These are my files:

Index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
    <hr>
    <s:action name="one" />
    <s:property value="list_fruits[0]" />
</body>
</html>

MyAction.java

package abc;

import java.util.*;
import org.apache.struts2.dispatcher.RequestMap;
import com.opensymphony.xwork2.*;

public class MyAction extends ActionSupport {
    private List list_fruits;

    public List getList_fruits() {
        return list_fruits;
    }

    public void setList_fruits(List list_fruits) {
        this.list_fruits = list_fruits;
    }

    public String doOne() {
        list_fruits = new ArrayList();
        list_fruits.add("banana");
        list_fruits.add("apple");
        list_fruits.add("mango");

        /*RequestMap rm = (RequestMap) ActionContext.getContext().get("request");
        rm.put("req_scope", list_fruits);*/
        return "sendToOne";
    }
}

one.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
    <h1>ONE.JSP</h1>
    <br>
    <s:property value="list_fruits[0]" />
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="devMode" value="true" />
    <package name="vns" extends="struts-default">

        <action name="one" class="abc.MyAction" method="doOne">
            <result name="sendToOne">/one.jsp</result>
        </action>

    </package>
</struts>

I am experiencing following behaviors:

Case1: When I put this (below) code in index.jsp, I get NO value printed.

<s:action name="one"/>
<s:property value="list_fruits[0]"/>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Case2: When I put this (below) code in index.jsp, the value gets printed from one.jsp because in this scene I included attribute executeResult=”true”

<s:action name="one" executeResult="true"/>
<s:property value="list_fruits[0]"/>    <!-- still NOT printed here, but gets printed from one.jsp -->

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Case3: When I put this (below) code in MyAction.java and index.jsp, then the value get printed on screen (index.jsp).

MyAction.java

RequestMap rm=(RequestMap)ActionContext.getContext().get("request");    
rm.put("req_scope", list_fruits);

index.jsp

<s:action name="one"/> <!-- removed executeResult="true" -->
<s:property value="#request.req_scope[0]"/> 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

in Case2 value gets printed due to one.jsp and not due to index.jsp

I want to know why I am NOT getting any value printed in Case1 whereas in Case2 and Case3, there is no such problem. Why is it so? Can anyone guide me?

Upvotes: 0

Views: 858

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

AleksandrM already answered you, but since you said

still not clear. :( please explain.

You can use it for :

  1. calling an Action and use executeResult="true" to get the result;
  2. calling an Action and push it on the main page ValueStack with var attribute, then reference it with # :

    <s:action   name="one"       var="instance" /> 
    <s:property value="#instance.list_fruits[0]"/>
    
  3. calling an Action that sets something in request / session scope, and then retrieve those values by using #attr or #session;

  4. calling an Action that does something (e.g. persists into the database the timestamp of when the page has been opened).

But you can do any of this single things server side, and BETTER.

This is why you shouldn't use the <s:action/> tag: it breaks most of the S2 framework conventions and mechanisms and it is a bad practice (or at least it isn't a good one).

Upvotes: 1

Related Questions