取web元素
访问Web元素
a)Map类型
i.IoC
ii.依赖Struts2
b)原始类型
i.IoC
ii.依赖Struts2
struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constantname="struts.devMode"value="true"/>
<packagename="login"extends="struts-default"namespace="/login">
<actionname="login*"class="com.bjsxt.struts2.user.action.LoginAction{1}">
<result>/user_login_success.jsp</result>
</action>
</package>
</struts>
index.jsp
<?xmlversion="1.0"encoding="GB18030"?>
<%@pagelanguage="java"contentType="text/html;charset=GB18030"
pageEncoding="GB18030"%>
<%Stringcontext=request.getContextPath();%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=GB18030"/>
<title>Inserttitlehere</title>
</head>
<body>
取得Map类型request,session,application,真实类型HttpServletRequest,HttpSession,ServletContext的引用:
<ol>
<li>前三者:依赖于容器</li>
<li>前三者:IOC</li>(只用这种)
<li>后三者:依赖于容器</li>
<li>后三者:IOC</li>
</ol>
<br/>
<formname="f"action=""method="post">
用户名:<inputtype="text"name="name"/>
密码:<inputtype="text"name="password"/>
<br/>
<inputtype="button"value="submit1"onclick="javascript:document.f.action='login/login1';document.f.submit();"/>
<inputtype="button"value="submit2"onclick="javascript:document.f.action='login/login2';document.f.submit();"/>
<inputtype="button"value="submit3"onclick="javascript:document.f.action='login/login3';document.f.submit();"/>
<inputtype="button"value="submit4"onclick="javascript:document.f.action='login/login4';document.f.submit();"/>
</form>
</body>
</html>
user_login_success.jsp
<?xmlversion="1.0"encoding="GB18030"?>
<%@pagelanguage="java"contentType="text/html;charset=GB18030"
pageEncoding="GB18030"%>
<%@tagliburi="/struts-tags"prefix="s"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=GB18030"/>
<title>Inserttitlehere</title>
</head>
<body>
UserLoginSuccess!
<br/>
<s:propertyvalue="#request.r1"/>|<%=request.getAttribute("r1")%><br/>
<s:propertyvalue="#session.s1"/>|<%=session.getAttribute("s1")%><br/>
<s:propertyvalue="#application.a1"/>|<%=application.getAttribute("a1")%><br/>
<s:propertyvalue="#attr.a1"/><br/>
<s:propertyvalue="#attr.s1"/><br/>
<s:propertyvalue="#attr.r1"/><br/>
<s:debug></s:debug>
<br/>
</body>
</html>
LoginAction2.java
/依赖注入、控制反转,最常用的一种方法
packagecom.bjsxt.struts2.user.action;
importjava.util.Map;
importorg.apache.struts2.interceptor.ApplicationAware;
importorg.apache.struts2.interceptor.RequestAware;
importorg.apache.struts2.interceptor.SessionAware;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginAction2extendsActionSupportimplementsRequestAware,SessionAware,ApplicationAware{
privateMap<String,Object>request;
privateMap<String,Object>session;
privateMap<String,Object>application;
//DIdependencyinjection
//IoCinverseofcontrol
publicStringexecute(){
request.put("r1","r1");
session.put("s1","s1");
application.put("a1","a1");
returnSUCCESS;
}
@Override
publicvoidsetRequest(Map<String,Object>request){
this.request=request;
}
@Override
publicvoidsetSession(Map<String,Object>session){
this.session=session;
}
@Override
publicvoidsetApplication(Map<String,Object>application){
this.application=application;
}
}
LoginAction1.java
/依赖于容器
packagecom.bjsxt.struts2.user.action;
importjava.util.Map;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginAction1extendsActionSupport{
privateMaprequest;
privateMapsession;
privateMapapplication;
publicLoginAction1(){
request=(Map)ActionContext.getContext().get("request");
session=ActionContext.getContext().getSession();
application=ActionContext.getContext().getApplication();
}
publicStringexecute(){
request.put("r1","r1");
session.put("s1","s1");
application.put("a1","a1");
returnSUCCESS;
}
}
LoginAction3.java
/依赖于容器http
packagecom.bjsxt.struts2.user.action;
importjavax.servlet.ServletContext;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginAction3extendsActionSupport{
privateHttpServletRequestrequest;
privateHttpSessionsession;
privateServletContextapplication;
publicLoginAction3(){
request=ServletActionContext.getRequest();
session=request.getSession();
application=session.getServletContext();
}
publicStringexecute(){
request.setAttribute("r1","r1");
session.setAttribute("s1","s1");
application.setAttribute("a1","a1");
returnSUCCESS;
}
}
LoginAction4.java
packagecom.bjsxt.struts2.user.action;
importjavax.servlet.ServletContext;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
importorg.apache.struts2.interceptor.ServletRequestAware;
importcom.opensymphony.xwork2.ActionSupport;
publicclassLoginAction4extendsActionSupportimplementsServletRequestAware{
privateHttpServletRequestrequest;
privateHttpSessionsession;
privateServletContextapplication;
publicStringexecute(){
request.setAttribute("r1","r1");
session.setAttribute("s1","s1");
application.setAttribute("a1","a1");
returnSUCCESS;
}
@Override
publicvoidsetServletRequest(HttpServletRequestrequest){
this.request=request;
this.session=request.getSession();
this.application=session.getServletContext();
}
}