孟孟的博客

看到即得到!

Struts2-结果跳转和servlet API

结果跳转方式

1、转发
<!--  转发 -->
<action name="Demo1Action" class="cn.itheima.a_result.Demo1Action" method="execute" >
	<result name="success" type="dispatcher" >/hello.jsp</result>
</action>


2、重定向
<!-- 重定向 -->
<action name="Demo2Action" class="cn.itheima.a_result.Demo2Action" method="execute" >
	<result name="success" type="redirect" >/hello.jsp</result>
</action>

3、转发到Action
<!-- 转发到Action -->
<action name="Demo3Action" class="cn.itheima.a_result.Demo3Action" method="execute" >
<result name="success" type="chain">
	<!-- action的名字 -->
	<param name="actionName">Demo1Action</param>
	<!-- action所在的命名空间 -->
	<param name="namespace">/</param>
</result>
</action>

4、重定向到Action

<!-- 重定向到Action -->
<action name="Demo4Action" class="cn.itheima.a_result.Demo4Action" method="execute" >
<result  name="success"  type="redirectAction">
	<!-- action的名字 -->
	<param name="actionName">Demo1Action</param>
	<!-- action所在的命名空间 -->
	<param name="namespace">/</param>
</result>
</action>

访问servletAPI方式

1、原理



2、通过ActionContext
public class Demo5Action extends ActionSupport {
	public String execute() throws Exception {
		//request域=> map (struts2并不推荐使用原生request域)
		//不推荐
		Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
		//推荐
		ActionContext.getContext().put("name", "requestTom");
		//session域 => map
		Map<String, Object> sessionScope = ActionContext.getContext().getSession();
		sessionScope.put("name", "sessionTom");
		//application域=>map
		Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
		applicationScope.put("name", "applicationTom");
		
		return SUCCESS;
	}
}

3、通过ServletActionContext
public class Demo6Action extends ActionSupport {
	//并不推荐
	public String execute() throws Exception {
		//原生request
		HttpServletRequest request = ServletActionContext.getRequest();
		//原生session
		HttpSession session = request.getSession();
		//原生response
		HttpServletResponse response = ServletActionContext.getResponse();
		//原生servletContext
		ServletContext servletContext = ServletActionContext.getServletContext();
		
                return SUCCESS;
	}
}


4、通过实现接口方式
public class Demo7Action extends ActionSupport implements ServletRequestAware {
	private HttpServletRequest request;

	public String execute() throws Exception { 
		System.out.println("原生request:"+request);
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
阅读更多
版权声明:如需转载,请备注链接: https://blog.csdn.net/W_Meng_H/article/details/80318488
个人分类: Struts2
所属专栏: SSH框架
想对作者说点什么? 我来说一句

jsp与Servlet跳转

2011年11月27日 154KB 下载

没有更多推荐了,返回首页

不良信息举报

Struts2-结果跳转和servlet API

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭