访问web元素:取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用(不懂)
//index.jsp
取得Map类型request,session,application;
取真实类型 HttpServletRequest,HttpSession,ServletContext的引用:
<ol>
<li>
前三者:依赖于容器
</li>
<li>
前三者:IOC(只用这种,控制反转)
</li>
<li>
后三者:依赖于容器
</li>
<li>
后三者:IOC
</li>
</ol>
<br /><!-- name = f -->
<form name="f" action="" method="post">
用户名:
<input type="text" name="name" />
密码:
<input type="text" name="password" />
<br /><!-- 多种按钮提交同一 form--> <!-- document.f取到form,用javaScript多态指定 -->
<input type="button" value="submit1"
onclick="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2"
onclick="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3"
onclick="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4"
onclick="javascript:document.f.action='login/login4';document.f.submit();" />
</form>
//user_login_success.jsp
User Login Success!
<br />
<s:property value="#request.r1" /> <!-- s:property 用标签形式访问Stack Context -->
|
<%=request.getAttribute("r1")%> <!-- 用JSP访问,取key = r1的值 -->
<br />
<s:property value="#session.s1" />
|
<%=session.getAttribute("s1")%>
<br />
<s:property value="#application.a1" />
|
<%=application.getAttribute("a1")%>
<br />
<s:property value="#attr.a1" /> <!-- attribute会搜session和application,分不清楚具体访问哪个(很少用) -->
<br />
<s:property value="#attr.s1" />
<br />
<s:property value="#attr.r1" />
<br />
<s:debug></s:debug>
<br />
//struts.xml配置
<package name="login" extends="struts-default" namespace="/login">
<action name="login*" class="com.bjsxt.struts2.user.action.LoginAction{1}">
<result>/user_login_success.jsp</result>
</action>
</package>
//action
package com.bjsxt.struts2.user.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction1 extends ActionSupport {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@SuppressWarnings("unchecked")
public LoginAction1() {
request = (Map)ActionContext.getContext().get("request"); //取得request的值
session = ActionContext.getContext().getSession(); //拿到action执行的上下文
application = ActionContext.getContext().getApplication();
}
public String execute() {
request.put("r1", "r1"); //向容器map中放入值,同时复制HttpServletRequest对象
session.put("s1", "s1"); //key——value
application.put("a1", "a1");
return SUCCESS;
}
}
package com.bjsxt.struts2.user.action;
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;
@SuppressWarnings("serial")
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
private Map<String, Object> request; //key —— String
private Map<String, Object> session; // value —— Object
private Map<String, Object> application;
//DI dependency injection 依赖注入
//IoC inverse of control 控制反转
public String execute() {
request.put("r1", "r1"); //其他人调用时,往request注入值,不用自己初始化,拿来就可以用了
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
}
public void setRequest(Map<String, Object> request) { //实现RequestAware接口
this.request = request;
}
public void setSession(Map<String, Object> session) { //实现SessionAware接口
this.session = session;
}
public void setApplication(Map<String, Object> application) { //实现ApplicationAware接口
this.application = application;
}
}
package com.bjsxt.struts2.user.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction4 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}
package com.bjsxt.struts2.user.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3() {
request = ServletActionContext.getRequest(); //拿到request
session = request.getSession(); //拿到session
application = session.getServletContext();
}
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return SUCCESS;
}
}