package cn.edu.hpu.getValue; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{ //实现RequestAware接口,此类就知道Request的存在,Session和Application一样 //IOC设计思想,也称DI。 //DI dependency injection依赖注入 //IoC inverse of control控制反转 //struts帮我们初始化了Request这些值 //本来自己控制,自己去new或去取,现在让别人去取,变成了控制反转(Spring在用) private Map<String,Object> request; private Map<String,Object> session; private Map<String,Object> application; public String execute(){ request.put("r1", "r1"); session.put("s1", "s1"); application.put("a1", "a1"); return SUCCESS; } @Override public void setRequest(Map<String, Object> request) { this.request=request; } @Override public void setSession(Map<String, Object> session) { this.session=session; } @Override public void setApplication(Map<String, Object> application) { this.application=application; } }
<package name="login" namespace="/login" extends="struts-default"> <action name="login*" class="cn.edu.hpu.getValue.LoginAction{1}"> <result>/User_login_success.jsp</result> </action> </package>
<input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();"/>
<%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>User_login_success</title> </head> <body> User Login Success!!<br/> <s:property value="#request.r1"/> | <%=request.getAttribute("r1")%><br/> <s:property value="#application.a1"/> | <%=application.getAttribue("a1")%><br/> <s:property value="#session.s1"/> | <%=session.getAttribute("s1")%><br/> <br/> <s:debug></s:debug> </body> </html>
原理:Map里面放的request中的值,在前台用request.getAttribute("r1")可以直接拿到,说明struts在Map和HttpServletRequest中间有关联。即是struts将Map中的所有值复制到对应的request等引用中,在前台直接可以从request等引用中取值。
转载请注明出处:http://blog.csdn.net/acmman/article/details/44239039