Note that there are some explanatory texts on larger screens.

plurals
  1. POStruts2: List being accessed out of its scope
    text
    copied!<p>I have a struts2 action class which looks something like this:</p> <pre><code>//import relevant packages public class Product implements SessionAware, ServletRequestAware, ServletResponseAware, ServletContextAware { private String productName; private String description; private String price; private ServletContext servletContext; private HttpServletRequest servletRequest; private HttpServletResponse servletResponse; private Map sessionMap; //getters and setters here public void setServletRequest(HttpServletRequest servletRequest) { this.servletRequest = servletRequest; } public void setSession(Map map) { this.sessionMap = map; } public void setServletResponse(HttpServletResponse servletResponse) { this.servletResponse = servletResponse; } public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } public String execute() { // do something here return "success"; } public List&lt;String&gt; getCountries() { List&lt;String&gt; countries = new ArrayList&lt;String&gt;(); countries.add("Australia"); countries.add("Fiji"); countries.add("New Zealand"); countries.add("Vanuatu"); return countries; } } </code></pre> <p>sruts.xml has the following contents:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"&gt; &lt;struts&gt; &lt;constant name="struts.enable.DynamicMethodInvocation" value="false" /&gt; &lt;constant name="struts.devMode" value="true" /&gt; &lt;package name="package.name" namespace="/" extends="struts-default"&gt; &lt;action name="Product_input"&gt; &lt;result&gt;/jsp/Product.jsp&lt;/result&gt; &lt;/action&gt; &lt;action name="Product_save" class="package.name.Product" method="execute"&gt; &lt;result&gt;/jsp/Details.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; &lt;/struts&gt; </code></pre> <p>Product.jsp consists of a simple form:</p> <pre><code>&lt;s:form action="Product_save"&gt; &lt;s:textfield label="Product Name" key="productName"/&gt; &lt;s:textfield label="Description" key="description"/&gt; &lt;s:textfield label="Price" key="price"/&gt; &lt;s:submit/&gt; &lt;/s:form&gt; </code></pre> <p>Details.jsp displays the contents entered in the form:</p> <pre><code>&lt;h5&gt;Details:&lt;/h5&gt; Product Name: &lt;s:property value="productName" /&gt; &lt;br /&gt; Description: &lt;s:property value="description" /&gt; &lt;br /&gt; Price: $ &lt;s:property value="price" /&gt; &lt;/br&gt; &lt;s:property value="countries[0]" /&gt; &lt;/br&gt; </code></pre> <p>The last line in Details.jsp tries to access the countries list declared in the getCountries() in action class and ideally it should not print anything since the method is never accessed nor the countries list is part of the action class attributes (and hence while creating the object of type Product in the Value stack, it shouldn't have countries).</p> <p><strong>But</strong> it does print Australia (the indexed value of list) along with the other form properties. How/Why is this happening?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload