(十二)标签与属性范围

       在Struts2.x里面,每一个JSP页面一定要与Action紧密连接在一起,尤其是在Action进行了服务器端跳转之后,也同样的可以直接利用标签访问这个类中的私有属性

范例:定义一个新的Action

        此时直接在Action里面设置了一个mydept的VO类对象,随后定义了对象的内容,并且让其跳转到了一个指定的页面,但是这个页面使用标签输出内容.

package cn.zwb.action;
import com.opensymphony.xwork2.ActionSupport;
import cn.zwb.vo.Dept;
@SuppressWarnings("serial")
public class Deptaction extends ActionSupport {
	private Dept myddept=new Dept();
	public Dept getMyddept() {
		return myddept;
	}
	@Override
	public String execute() throws Exception {
		this.myddept.setDeptno(10);
		this.myddept.setDname("caiwu");
		return "dept.show";
	}
}

范例:定义dept_show.jsp页面

<body>
  	<h1>部门编号<s:property value="mydept.deptno"/></h1>
  	<h1>部门名称<s:property value="mydept.dname"/></h1>
  </body>

        这个时候所有的标签不需要做任何性的处理就可以找到跳转过来的Action本身所具备的内容

        但是在编写代码的过程之中,Struts2.x这种JSP与Action紧密连接的形式我们并不会习惯,因为大部分人都习惯利用request属性传递操作.

范例:利用request属性传递

	@Override
	public String execute() throws Exception {
		Dept dept=new Dept();
		dept.setDeptno(10);
		dept.setDname("财务部");
		ServletActionContext.getRequest().setAttribute("dept", dept);
		return "dept.show";
	}
}

        此时的标签无法找到属性范围中的内容,那么如果要想在Struts2.x的标签里面访问属性范围中的内容则在访问前请加上"#范围名称",例如"#request"表示request属性范围\

范例:修改标签

 	<h1>部门编号:<s:property value="#request.dept.deptno"/></h1>
  	<h1>部门名称:<s:property value="#request.dept.dname"/></h1>

        如果要想使用Struts的标签就必须使用OGNL的表达式语言完成.

        

猜你喜欢

转载自blog.csdn.net/qq1019648709/article/details/80565657