标签:style blog color io 使用 java ar sp div
1. 实现接口: implements SessionAware,RequestAware,ApplicationAware
//自动取得session,request,application元素
//可以自己取得元素: session = ActionContext.getContext().getSession();
2. 创建Map元素
private Map session; //一般都是用这种
private Map requsest;
private Map application;
3. 输入map元素的键与值
session.put("sessionDemo", "session is run");
requsest.put("requestDemo","request is run");
application.put("applicationDemo","application is run");
4. 在实现的public void setSession(Map<String, Object> session)方法中添加
this.session = session;
5. 在前台页面取得值
5.1 使用jsp标签取值
前台输入jsp的标签: <%@taglib uri="/struts-tags" prefix="s" %>
或直接取值:
<s:property value="#session.sessionDemo"/> || <%=session.getAttribute("sessionDemo") %><br>
<s:property value="#request.requestDemo"/> || <%=request.getAttribute("requestDemo") %><br>
<s:property value="#application.applicationDemo"/> || <%=application.getAttribute("applicationDemo") %>
取得值相同
//java代码部分
1 public class UserAction extends ActionSupport implements SessionAware,RequestAware,ApplicationAware{ 2 private User user; 3 private Map session; 4 private Map requsest; 5 private Map application; 6 7 public String add(){ 8 9 // session = ActionContext.getContext().getSession(); 10 session.put("sessionDemo", "session is run"); 11 requsest.put("requestDemo","request is run"); 12 application.put("applicationDemo","application is run"); 13 14 15 System.out.println("User is run."); 16 System.out.println("name="+ user.getName()); 17 System.out.println("age="+user.getAge()); 18 return SUCCESS; 19 } 20 public User getUser() { 21 return user; 22 } 23 public void setUser(User user) { 24 System.out.println("setUser run"); 25 this.user = user; 26 } 27 @Override 28 public void setApplication(Map<String, Object> application) { 29 // TODO 自动生成的方法存根 30 this.application = application; 31 } 32 @Override 33 public void setRequest(Map<String, Object> request) { 34 // TODO 自动生成的方法存根 35 this.requsest = request; 36 } 37 @Override 38 public void setSession(Map<String, Object> session) { 39 // TODO 自动生成的方法存根 40 this.session = session; 41 } 42 43 44 }
//jsp部分
1 <body> 2 This is User page <br> 3 4 <s:property value="#session.sessionDemo"/> || <%=session.getAttribute("sessionDemo") %><br> 5 <s:property value="#request.requestDemo"/> || <%=request.getAttribute("requestDemo") %><br> 6 <s:property value="#application.applicationDemo"/> || <%=application.getAttribute("applicationDemo") %> 7 8 </body>
标签:style blog color io 使用 java ar sp div
原文地址:http://www.cnblogs.com/sidianshui/p/3995082.html