01_12_Struts2_访问Web元素
1. 配置struts.xml文件
<package name="login" namespace="/login" extends="struts-default">
<action name="login*" class="com.sumapay.action.LoginAction{1}">
<result>
/user_login_success.jsp
</result>
</action>
</package>
2. 配置jsp页面
user_login.jsp
<body>
取得Map类型request、session、application,真实类型HttpServletRequest、HttpServletSession、ServletContext的引用
<ol>
<li>
前三者:依赖于容器
</li>
<li>
前三者:IOC
</li>
<li>
后三者:依赖于容器
</li>
<li>
后三者:IOC
</li>
</ol>
<form name="f" action="" method="post">
用户名:<input type="text" name="name" />
密码:<input type="text" name="password" />
<br/>
<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>
</body>
user_login_success.jsp
<body>
<s:property value="#request.r1" />
|
<%=request.getAttribute("r1")%>
<br />
<s:property value="#session.s1" />
|
<%=session.getAttribute("s1")%>
<br />
<s:property value="#application.a1" />
|
<%=application.getAttribute("a1")%>
<br />
<s:debug></s:debug>
</body>
3. 实现Action类
3.1LoginAction1.java
package com.sumapay.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction1 extends ActionSupport{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private Map request;
private Map session;
private Map application;
public LoginAction1() {
request = (Map) ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}
public String execute() {
System.out.println("調用execute方法");
System.out.println("##############");
System.out.println(name);
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return "success";
}
}
3.2LoginAction2.java
package com.sumapay.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;
public class LoginAction2 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware{
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
//DI dependency injection
//Ioc inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return "success";
}
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
3.3LoginAction3.java
package com.sumapay.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;
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
//DI dependency injection
//Ioc inverse of control
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return "success";
}
}
3.4LoginAction4.java
package com.sumapay.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;
public class LoginAction3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
//DI dependency injection
//Ioc inverse of control
public String execute() {
request.setAttribute("r1", "r1");
session.setAttribute("s1", "s1");
application.setAttribute("a1", "a1");
return "success";
}
}