Struts2 Notes II

Summary of this article: Struts2 -> JSP page parameters, VS (ValueStack), OGNL expressions, Struts2 <s/> tags


One: Struts2 -> JSP page parameters

1. Collect fragmented data and objects

Background code:

 

 

Jsp page

 

 

Entity class:

 

 

2. List, Set, Map collection

 

 

 

Two: Value Stack (ValueStack)

1. Concept: The logical storage area in Struts2, which manages all the data involved in a request.

 

2. Function: ValueStack provides a common independent stack structure (Root), and encapsulates the operation of the scope (Context) to simplify the user's data access method (OGNL).

3. Benefits: Decoupling from the view layer (JSP is inefficient, only JSP has built-in objects), and template technology will be selected in actual combat (freemarker, Velocity [vəˈlɒsəti] have no built-in objects)

4. Note: Each time an Action is requested, a separate ValueStack object will be generated.

 

5. Root area (List): commonly used // stack structure, search from the top of the stack down when accessing. If "[0].property name" is used, all subsets starting with 0 are accessed. If "[1].property_name" is used, all subsets starting with 1 are accessed.

 

I. Concept: A stack structure storage area provided by Struts, the main operation area, easy to access, mainly used to operate Action-related data. //Store the Action object itself and request parameters.

 

II. Access the root zone:

 

 

 

 

6. Context area (Map): occasionally used (users can manually store data in session and application)

I. 概念:Struts提供的一个Map结构存储区域,主要用来操作JavaEE作用域。

 

 

 

 

7.经验:ValueStack的两种使用方式

I.通过Action的root区存储 请求数据、临时数据。

II.通过ValueStack手动存入数据(vs.setValue(expr,obj)),将保存在context区中。

III.使用Struts2对于操作数据时,都需要使用ValueStack进行,会由ValueStack对接JavaEE标准中的几个作用域对象。//不再直接操作内置对象

 

注意:同一个浏览器发送多次请求,每个请求对应一个ValueStack(一次请求有效),但共享同一个session、application。(URL没有重写的话,Cookie不能禁)

8.总结Struts2中的OGNL(对象图导航语言):操作ValueStack的必要途径!

I.使用OGNL时必须找到一个根对象(可以成为根对象的有5个[ root、request、session、application、parameters]默认是root),访问时可通过"根"查找到与之关联的所有对象。需要使用其他"根"时,则需要应用"#根对象名称"的语法访问,但同时也会增加访问常用对象的繁琐程度。

 

II.context存在的主要原因就是,在OGNL中不支持多个root对象,如果需要访问其他作用域对象时,则需要使用OGNL定位到Context中的不同对象,再进行访问。

 

三:ognl表达式(Object-Graph Navigation Language):对象导航图语言

1. 概念:

I. 语法与EL相似、功能强大、简单的表达式语言。

II. 可直接访问对象的getters方法、还可直接访问对象的普通方法。

III. OGNL是一套独立的、完整的技术组件、不属于Struts本身。

 

2. 在已有的List中取值:

 


3. 在已有的Map中取值:

 

4. 总结:

I. List:直接书写属性名称,即可完成List中的逐个对象查找,以及目标属性的获取。

II. Map:#为前缀+key查找Map中的目标对象,再导航查找各个属性。

 


四:Struts2标签 +ognl表达式完成在JSP页面的取值  ->替代EL+JSTL

1. 作用:逻辑处理,用以替代EL表达式。

2. 使用方式:

I. 引入标签库:<%@taglib uri="/struts-tags" prefix="s" %>

II. 选择标签:

1). 单条数据展示:

2). 条件判断:

 

3). 遍历:

 

 

3. Struts2高级标签:

I. 获取日期类型、并格式化:

 

II. URL重写:

1). JSP到JSP:

 

2). JSP到Action:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325459812&siteId=291194637