zoukankan      html  css  js  c++  java
  • Strut2 ognl取出存放在request,session,application和对象栈的中的值

    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"/>
    

      

    4.在对象栈中person对象作为key,利用set方法存放在对象栈的栈顶,取出它的name属性

      a.存放数据

    	public String testObjectToRoot_Set(){
    		Person person=new Person();
    		person.setAge(new Integer(1));
    		person.setName("person");
    		person.setCommit("person");
    		ValueStack valueStack = ActionContext.getContext().getValueStack();
    		valueStack.set("person", person);
    		return "root_set";
    	}
    

      b.取出数据

       	<s:property value="person.name" /><br>
    

      

    5.构造map,在页面上构造map

       	构造map:<br>
       	<!-- 
       		这个标签对应的html代码
       		<input type="radio" id="fool" value="fool"/><label for="fool">bool</label>
    		<input type="radio" id="foo2" value="foo2"/><label for="foo2">boo2</label>
    		从上面可以看出,map中key相当于radio中的value和id,map中的value相当于页面上显示的值
       	 -->
       	<s:radio list="#{'fool':'bool','foo2':'boo2' }"></s:radio><br>
    

      

    6.构造map,在后台构造map,然后存放在request域中,在页面取出来

      a.构造map,放入request域中

    	public String testMap_Request(){
    		Map<String,String> map=new HashMap<String, String>();
    		map.put("male", "男");
    		map.put("girl","女");
    		
    		ServletActionContext.getRequest().setAttribute("map", map);
    		return "map_request";
    	}
    

      b.在页面取出来

       	 从request域中获取map<br>
       	 <s:property value="#request.map.male"/><br>
    

      

    7.用put方法保存map至context中的map

      a.存放map

    	public String testMap_Put(){
    		ActionContext context = ActionContext.getContext();
    		Map<String,String> map=new HashMap<String, String>();
    		map.put("male", "男");
    		map.put("girl","女");
    		context.put("map",map);
    		return "map_put";
    
    	}
    

      b.取出map中的数据

       	 从map中取出利用put方法存放的map<br>
       	 <s:property value="#map.male"/><br>
    

      

    8.把一个字符串放到request域中,然后用%输出

      a.存放数据

    	public String testRequest_Percent(){
    		ServletActionContext.getRequest().setAttribute("request_username","username");
    		return "request_percent";
    	}
    

      b.取出数据

       	 利用%输出request域中的内容<br>
       	 <s:textfield name="username" label="%{#request.request_username}"></s:textfield><br>
    

      

  • 相关阅读:
    哇,博客开通啦
    前端与后端数据交互的方式之ajax
    apply()方法和call()方法
    元素居中的方法
    JS中兼容问题的汇总
    关于元素尺寸问题的汇总
    小案例之随机点名系统
    圣杯布局与双飞翼布局
    js自动分页加载所有数据
    浏览器工作流程
  • 原文地址:https://www.cnblogs.com/callyblog/p/7533065.html
Copyright © 2011-2022 走看看