知识大全 Struts2中访问web元素
Posted 知
篇首语:在任何情况下,你都要学习——以更换学习内容作为你的休息。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Struts2中访问web元素相关的知识,希望对你有一定的参考价值。
Struts2中访问web元素 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
取得Map类型request session application 真实类型 HttpServletRequest HttpSession ServletContext的引用:
前三者 依赖于容器
前三者 IOC (只用这种)
后三者 依赖于容器
后三者 IOC
一 方法一 ActionContext方式
一般在Action类的构造方法 或execute()方法中获取
[java]
public class LoginAction extends ActionSupport
private Map request;
private Map session;
private Map application;
public LoginAction ()
request = (Map)ActionContext getContext() get( request );
session = ActionContext getContext() getSession();
application = ActionContext getContext() getApplication();
public String execute()
request put( r r );
session put( s s );
application put( a a );
return SUCCESS;
public class LoginAction extends ActionSupport
private Map request;
private Map session;
private Map application;
public LoginAction ()
request = (Map)ActionContext getContext() get( request );
session = ActionContext getContext() getSession();
application = ActionContext getContext() getApplication();
public String execute()
request put( r r );
session put( s s );
application put( a a );
return SUCCESS;
然后在Jsp页面中获取相关web元素
[]
<body>
User Login Success!
<br />
<s:property value= #request r /> | <%=request getAttribute( r ) %> <br />
<s:property value= #session s /> | <%=session getAttribute( s ) %> <br />
<s:property value= #application a /> | <%=application getAttribute( a ) %> <br />
<s:property value= #attr a /><br />
<s:property value= #attr s /><br />
<s:property value= #attr r /><br />
<s:debug></s:debug>
<br />
</body>
<body>
User Login Success!
<br />
<s:property value= #request r /> | <%=request getAttribute( r ) %> <br />
<s:property value= #session s /> | <%=session getAttribute( s ) %> <br />
<s:property value= #application a /> | <%=application getAttribute( a ) %> <br />
<s:property value= #attr a /><br />
<s:property value= #attr s /><br />
<s:property value= #attr r /><br />
<s:debug></s:debug>
<br />
</body>
注 因为request session application对象Struts 将在放入到Action Context中
因此需要使用#key来访问对象们
后面的是java脚本代码的访问方式
一 方式二 Ioc(控制反转)—推荐使用
让Action类实现RequestAware SessionAware ApplicationAware接口 然后重写他们的set方法(setRequest setSession setApplication) 通过依赖注入 控制反转(原来自己控制 现在由别人来控制值 )
[java]
import apache struts interceptor ApplicationAware;
import apache struts interceptor RequestAware;
import apache struts interceptor SessionAware;
import opensymphony xwork ActionSupport;
public class LoginAction 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( r r );
session put( s s );
application put( a a );
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;
import apache struts interceptor ApplicationAware;
import apache struts interceptor RequestAware;
import apache struts interceptor SessionAware;
import opensymphony xwork ActionSupport;
public class LoginAction 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( r r );
session put( s s );
application put( a a );
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;
在视图(JSP)页面中获取相关对象 同方式一
一 方式三 获取原类型
获取是的HttpServletRequest/HttpSession/ServletContext
[java]
public class LoginAction extends ActionSupport
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction ()
request = ServletActionContext getRequest();
session = request getSession();
application = session getServletContext();
public String execute()
request setAttribute( r r );
session setAttribute( s s );
application setAttribute( a a );
return SUCCESS;
public class LoginAction extends ActionSupport
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public LoginAction ()
request = ServletActionContext getRequest();
session = request getSession();
application = session getServletContext();
public String execute()
request setAttribute( r r );
session setAttribute( s s );
application setAttribute( a a );
return SUCCESS;
一 方式四 获取原类型 控制反转
首先需要Action实现 apache struts interceptor ServletRequestAware接口 然后重写setServletRequest()方法 获取HttpServletRequest对象 再通过HttpServletRequest对象取昨HttpSession和ServletContext对象
[java]
import javax servlet ServletContext;
import javax servlet HttpServletRequest;
import javax servlet HttpSession;
import apache struts interceptor ServletRequestAware;
import opensymphony xwork ActionSupport;
public class LoginAction extends ActionSupport implements ServletRequestAware
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute()
request setAttribute( r r );
session setAttribute( s s );
application setAttribute( a a );
return SUCCESS;
@Override
public void setServletRequest(HttpServletRequest request)
this request = request;
this session = request getSession();
this application = session getServletContext();
import javax servlet ServletContext;
import javax servlet HttpServletRequest;
import javax servlet HttpSession;
import apache struts interceptor ServletRequestAware;
import opensymphony xwork ActionSupport;
public class LoginAction extends ActionSupport implements ServletRequestAware
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String execute()
request setAttribute( r r );
session setAttribute( s s );
application setAttribute( a a );
return SUCCESS;
@Override
public void setServletRequest(HttpServletRequest request)
this request = request;
this session = request getSession();
this application = session getServletContext();
cha138/Article/program/Java/ky/201311/28170
相关参考
教你如何配置Struts2web.xml文件 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! St
知识大全 PHP5中使用Web服务访问J2EE应用程序(2)
PHP5中使用Web服务访问J2EE应用程序(2) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
晕了整了一个晚上就是无法访问原来是因为用了do的后缀名改成action的后缀名就可以访问了而且只能用XXXaction的形式否则无法访问 看来还是由于对struts的过滤机制不是很清楚 但是我
struts异常处理 在web开发中经常要处理异常这样无论用户进行怎样的操作都不会把异常页面展现给用户这样可以给用户一个好的感受现在来说下struts进行开发中进行的异常处理 在servlet
在很多Web应用中为了完成不同的工作一个HTMLform标签中可能有两个或多个submit按钮如下面的代码所示 <![if!supportLineBreakNewLine]> &l
如何解决Web访问时提示输入密码? 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 我的IIS服务
在web应用中对页面的访问控制通常通过程序来控制流程为登录>设置session>访问受限页面时检查session是否存在如果不存在禁止访问 对于较小型的web应用可以通过tomcat
指定需要应用特定的配置设置的资源 配置结构的示例 <configuration> <location> <locationpath=pathallowover
struts中一些常量 <!指定Web应用的默认编码集相当于调用HttpServletRequest的setCharacterEncoding方法> <constantnam
客户端访问WebService方法的一个细节 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 之前