struts forward到servlet

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方法的名称决定的,跟是否存在该属性无关。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值