import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.stereotype.Controller;
import org.springframework.context.annotation.Scope;
import org.apache.struts2.interceptor.RequestAware;
@SuppressWarnings("all")
@Scope("prototype")
@Controller("forwardAction")
public class ForwardAction extends ActionSupport implements RequestAware {
@Override
public String execute() throws Exception {
this.getRequest().put("attr", "value set in HttpRequest");
return SUCCESS;
}
private Map<String, Object> request;
private String color;
//private String valueOnStack;
public Map<String, Object> getRequest() {
return request;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getValueOnStack() {
return "value return from getValueOnStack() method";
}
/**public void setValueOnStack(String valueOnStack) {
this.valueOnStack = valueOnStack;
}*/
}
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts2.ServletActionContext;
@SuppressWarnings("all")
public class ShowServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.getOutputStream().println("<html>");
response.getOutputStream().println("<head>");
response.getOutputStream().println("</head>");
response.getOutputStream().println("<body>");
response.getOutputStream().println("<p>Hello from ShowServlet.doPost()</p>");
response.getOutputStream().println("<p>Attribute set in struts 2 Action = " +
request.getAttribute("attr") + "</p>");
response.getOutputStream().println("<p>Request parameters = " +
request.getParameter("color") + "</p>");
String valueOnStack = (String) ServletActionContext.getValueStack(request).findValue("valueOnStack");
response.getOutputStream().println("<p>Value received from Action property on ValueStack = " +
valueOnStack + "</p>");
response.getOutputStream().println("</body>");
response.getOutputStream().println("</html>");
}
}
struts.xml中的配置:
<action name="ForwardAction" class="forwardAction">
<result name="success">/showServlet</result>
</action>
Action模型在ValueStack中的属性值是根据Action的get方法的名称决定的,跟是否存在该属性无关。