java 变量级别及action向jsp传值[整理]

test.java

public class testAction implements Action {
 // 下面是处理用户请求的execute方法
 public String execute() throws Exception {
  // 获取ActionContext实例,通过该实例访问Servlet API
  ActionContext actx = ActionContext.getContext();
  String user = "mimi";
  // 设置成application属性
  actx.getApplication().put("user", user);
  // 设置成一个HttpSession属性
  actx.getSession().put("user", user);
  // 设置成一个HttpRequest属性
  actx.put("user", user);
  return SUCCESS;
 }
}

test.jsp

 <body>
  <a href=test/test.action>Action test</a>
   applicationScope:${applicationScope.user} 
   sessionScope:${sessionScope.user} 
   requestScope:${requestScope.user}
 </body>

================================================================
遍历页面request

  <% 
     
  for   (Enumeration e   =   request.getAttributeNames(); e.hasMoreElements();)   {
        Object o  =  e.nextElement();
        out.println((String) o  +   "      :        "   +  request.getAttribute((String) o)  +   " <br><hr> " );
    } 

 
%>


1 : request.getAuthType() = null 
2 : request.getCharacterEncoding() = null 
3 : request.getContentLength() = -1 
4 : request.getContentType() = null 
5 :  request.getContextPath()  = /imageCaixun 
6 :  request.getMethod()  = GET 
7 : request.getPathInfo() = null 
8 : request.getPathTranslated() = null 
9 : request.getQueryString() = null 
10 :  request.getRealPath("/")  = D:\project\bank_image\web\ 
11 :  request.getRemoteAddr()  = 127.0.0.1 
12 :  request.getRemoteHost()  = 127.0.0.1 
13 :  request.getRemoteUser()  = null 
14 :  request.getRequestURI()  = /imageCaixun/demo.jsp 
15 :  request.getRequestURL()  = http://localhost/imageCaixun/demo.jsp 
16 :  request.getServletPath()  = /demo.jsp 
17 :  request.getServerName()  = localhost 
18 :  request.getServerPort()  = 80

================================================================
如果在Action类里有一个简单的变量,并且已经写好了它的get和set方法
private String test="jsp value test"; 
那么我们就可以在页面上直接获取,而不通过struts2或者webwork的标签. 
首页jsp页面要引入JAR包  
action所在的包 com.XXX.XXX
struts2包为org.apache.struts2.ServletActionContext
 
然后在页面上定义一个ServletActionContext变量
<%
HttpServletRequest request1 = ServletActionContext.getRequest();
String test=request1.getAttribute("test");
%>
 
然后输出就可以看到结果
<%=test%>
注:由于JSP本身已经有了自己的request对象,所以我们定义HttpServletRequest要重新选择一个名字,否则将会报错.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值