1. 程式人生 > >Strut2 ognl取出存放在request,session,application和對象棧的中的值

Strut2 ognl取出存放在request,session,application和對象棧的中的值

str message cti public ica java pre test stack

1.取出request,session,applicaiton中的值

  a.往裏面加入request,session,application中加入值

	public String testServlet(){
		ServletActionContext.getRequest().setAttribute("request_username", "username");
		ActionContext.getContext().getSession().put("session_username", "username");
		ServletActionContext.getServletContext().setAttribute("application_username","username");
		
		return "servlet";
	}

  取值:

   	輸出request,session,application域中的值,由於訪問的是map,所以要加個#號<br>
   	request的值:<s:property value="#request.request_username" /><br>
   	session的值:<s:property value="#session.session_username" /><br>
   	application的值:<s:property value="#application.application_username" /><br>

  

2.用valuestack中的對象棧的set方法存放的數據,把對象封裝成一個hashmap,放入棧頂

  a.放值

	public String testValueStack_Set(){
		ValueStack valueStack = ActionContext.getContext().getValueStack();
		valueStack.set("msg", "message");
		return "value_stack_set";
	}

  b.取值

   	輸出valuestack的set方法存放的數據,實際輸出的是棧頂的數據<br>
   	<s:property value="msg"/><br>

  

3. 在person,student和ognlAction中同時有commit,測試頁面會輸出棧中第一個commit,也就是OgnlAction中的commit

  a.放值,用add方法放

	public String testValueStack_Deep(){
		Person person=new Person();
		person.setAge(new Integer(1));
		person.setName("person");
		person.setCommit("person");
		
		Student student=new Student();
		student.setAge(2);
		student.setName("student");
		student.setCommit("student");
		
		ValueStack valueStack = ActionContext.getContext().getValueStack();
		CompoundRoot root = valueStack.getRoot();
		root.add(person);
		root.add(student);
		
		return "valueStack_deep";
		
	}

  取值:

   	在person,student和ognlAction中同時有commit,測試頁面會輸出棧中第一個commit,也就是OgnlAction中的commit
   	<s:property value="commit"/>

  

Strut2 ognl取出存放在request,session,application和對象棧的中的值