我有一个 struts2 动作类,它看起来像这样:
//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<String> getCountries() {
List<String> countries = new ArrayList<String>();
countries.add("Australia");
countries.add("Fiji");
countries.add("New Zealand");
countries.add("Vanuatu");
return countries;
}
}
sruts.xml 有以下内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="package.name" namespace="/" extends="struts-default">
<action name="Product_input">
<result>/jsp/Product.jsp</result>
</action>
<action name="Product_save" class="package.name.Product" method="execute">
<result>/jsp/Details.jsp</result>
</action>
</package>
</struts>
Product.jsp 包含一个简单的形式:
<s:form action="Product_save">
<s:textfield label="Product Name" key="productName"/>
<s:textfield label="Description" key="description"/>
<s:textfield label="Price" key="price"/>
<s:submit/>
</s:form>
Details.jsp 显示表单中输入的内容:
<h5>Details:</h5>
Product Name:
<s:property value="productName" />
<br /> Description:
<s:property value="description" />
<br /> Price: $
<s:property value="price" /> </br>
<s:property value="countries[0]" /> </br>
Details.jsp 中的最后一行尝试访问在动作类的 getCountries() 中声明的国家/地区列表,理想情况下它不应该打印任何内容,因为该方法从未被访问过,国家列表也是动作类属性的一部分(因此,虽然在 Value 堆栈中创建 Product 类型的对象,它不应该有国家)。
但它确实打印了澳大利亚(列表的索引值)以及其他表单属性。如何/为什么会发生这种情况?