Samuel Learning

  • 联系
  •  
  • 管理

文章分类(203)

  • 01 ASP/HTML(6)
  • 02 Script/CSS(16)
  • 03 C/C++
  • 04 XML(4)
  • 05 微软编程(26)
  • 06 J2EE(60)
  • 09 Linux(14)
  • 10 Database(27)
  • 11 报表打印
  • 12 打包安装(1)
  • 13 模式重构(2)
  • 14 系统架构
  • 15 敏捷开发(11)
  • 16 案例分析(30)
  • 17 Workflow(1)
  • 18 配置管理(1)
  • 19 项目管理
  • 20 英语(4)

新闻分类(52)

  • CXF学习
  • Hibernate学习(1)
  • iBatis学习(16)
  • Struts1学习(1)
  • Struts2学习(34)

.NET资源

  • adxmenu
  • C# Open Source
  • DNN Directory
  • M2Land
  • Windows Form FAQ
  • 中国DNN
  • 中国DNN联盟

Ajax

  • DoJo
  • GWT
  • JQuery
  • jquery autocomplete
  • jquery flexgrid
  • JQuery Form
  • jquery masked input
  • JQuery UI
  • jquery validation
  • Jquery 图表
  • jquery报表
  • jquery插件集合
  • Qooxdoo
  • Tibco GI
  • YUI
  • YUI-EXT
  • ZeroKode

Java开源

  • ABLE
  • Agile Tao
  • Ajax4JSF
  • Alfresco
  • AppFuse
  • Compiere
  • Equinox
  • Findbugs
  • Geronimo
  • Grails
  • Harmony
  • Hibernate论坛
  • JAG
  • Java开源大全
  • Java视线论坛
  • jBPM工作流
  • JSFTutorials
  • Nereide ERP
  • Ofbiz ERP
  • Opentaps ERP
  • operamasks
  • Petstore
  • Prototype.js
  • RIFE
  • Runa
  • SpringSide
  • Struts Menu
  • Sun Glassfish
  • Trails
  • YUI4JSF
  • 满江红

Mobile

  • Sencha

WEB资源

  • DHTML中心
  • DHTML参考手册
  • DHTML文档
  • EclipsePlugin
  • Firebug
  • GRO Clinux
  • jMaki
  • JSTL文档
  • LoadIcon
  • Openlaszlo
  • Struts Menu 展示
  • Web Test Tools
  • WebCtrs
  • Webdeveloper
  • 中国RIA开发者论坛

Workflow

  • E-Workflow
  • JBPM
  • OpenWFE
  • OSWorkflow
  • WFMC
  • Workflow Research

其他连接

  • confach
  • CPP
  • ejay
  • Giovanni
  • 丹佛
  • 交大e-learning
  • 交大研究生院
  • 可恶的猫
  • 天天@blog
  • 我的相册
  • 阿飞

大牛人

  • 32篇JBPM
  • David.Turing
  • HongSoft@业务集成
  • Joel
  • Koen Aers
  • Martinfowler
  • Raible Matt
  • Raible Wiki
  • Scott W.Ambler
  • Tom Baeyens
  • Uncle Bob
  • 一个世界在等待
  • 子在川上曰
  • 小布老师
  • 小明
  • 差沙
  • 徐昊
  • 江南白衣
  • 汪博士
  • 汪小金
  • 银狐999

开源软件

  • 2Bizbox ERP
  • CompiereCRM&ERP
  • EGW
  • Vtiger CRM
  • webERP

敏捷

  • Canoo
  • Cruisecontrol
  • DBUnit
  • EL4Ant
  • Extreme Programming
  • Fit
  • Fitnesse
  • JFrog
  • Liquibase
  • Maven
  • MockObjects
  • Selenium
  • Squish
  • xpairtise
  • XPlanner
  • XProgramming
  • 敏捷联盟

数据库

  • Oracle 中国
  • Oracle-ERP
  • Oracle在线社区

未归类

  • Aquarius Orm Studio
  • mambo建站系统
  • Oracle产品下载
  • 远程同步管理工具Capivara

经典框架

  • Apache Shale
  • formdef-struts
  • FreeMarker 主页
  • JBoss Seam
  • JSF 中心
  • JSF 入门应用
  • JSF中国
  • MyFaces官方
  • Spring 社区
  • Spring专业网站
  • Spring中文论坛
  • Spring参考手册
  • Spring官方网站
  • strecks-struts
  • Struts1
  • Struts2
  • Struts-layout
  • StrutsWiKi
  • Tapestry WIKI
  • Tapestry 官方
  • Tapestry4开发指南
  • Tapestry中文文档
  • Webwork2文档
  • Wicket

网络教程

  • Laliluna
  • RoseIndia
  • Sang Shin
  • Visualbuilder

著名站点

  • Buildix
  • Dev2Dev
  • IBM dev中国
  • InfoQ
  • ITPub
  • Java Eye
  • Java Research
  • JavaRead
  • JavaWorldTW
  • Matrix
  • PHP100
  • PHPX
  • SpringSideWiKi
  • TheServerSide
  • TWPHP
  • 中国工作流论坛

项目管理

  • 管理人网

最新评论

View Post

Struts2:在Struts2和XWork1中用JSP2和JSTL[笔记]

S2/X1 and its taglib is oriented towards OGNL, which is using a value stack
for all action properties. These values are not direct available for the
expression language of JSP2/JSTL1.1.

S2/X1和他们的标签库是面向OGNL的,它对所有Action属性用Value栈的。这些值不能直接在JSP2和JSTL1.1种获取。

However, it's easy to populate the request attribute set, with all gettable properties of an action object. You need to provide an interceptor that does the job, by register a PreResultListener which is invoked after the return of Action.execute() but before the rendering of the result .

但是,在一个Action对象中所有可get的属性,是很容易更新请求属性集的。你需要提供一个interceptor来作这件工作,在调用Action.execute之后,渲染结果之前,注册一个PreResultListener。

The interceptor below is using Jakarta BeanUtils. It first extracts all getters
of the current action, invokes them one at the time and stores the values into a map.

下面的interceptor用了Jakarta BeanUtils. 它首先获取所有当前action的getters,调用他们,并且把值保存到map中。

Then it iterates over the map and populates the request attribute set.
The double iteration is not needed, it's just there for clarity.

然后遍历map,组装请求属性集,双重遍历是不需要的,这儿仅仅未了清晰。

class ActionPropertyExportInterceptor

package com.whatever.interceptors;

import org,apache.struts2.StrutsStatics;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.AroundInterceptor;
import com.opensymphony.xwork.interceptor.PreResultListener;
import org.apache.commons.beanutils.PropertyUtils;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.util.*;

/**
* Populates HTTP Request Attributes with all gettable properties of the current action.
*/
public class ActionPropertyExportInterceptor extends AroundInterceptor {
protected void before(ActionInvocation invocation) throws Exception {
invocation.addPreResultListener( new PropertyExporter() );
}
protected void after(ActionInvocation dispatcher, String result) throws Exception { }

public static class PropertyExporter implements PreResultListener {
private static final List ignore = Arrays.asList(new String[] {"class", "texts"}); //skip getClass,...

//Invoked after Action.execute() but before Result
//Calls all getters of the action and insert the values into the request
public void beforeResult(ActionInvocation invocation, String resultCode) {
Map props = extractGetterPropertyValues( invocation.getAction() );
HttpServletRequest request = getRequest(invocation);
for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
Map.Entry e = (Map.Entry) it.next();
request.setAttribute((String) e.getKey(), e.getValue());
}
}

public Map extractGetterPropertyValues(Object bean) {
PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(bean);
Map props = new HashMap();
for (int i = 0; i < descr.length; i++) {
PropertyDescriptor d = descr[i];
if (d.getReadMethod() == null) continue;
if (ignore.contains(d.getName())) continue;

try {
props.put(d.getName(), PropertyUtils.getProperty(bean, d.getName()));
} catch (Exception e) { }
}
return props;
}

public HttpServletRequest getRequest(ActionInvocation invocation) {
return (HttpServletRequest) invocation.getInvocationContext().get(WebWorkStatics.HTTP_REQUEST);
}
}
}

Don't forget to declare the interceptor in your struts.xml file and insert it
into your interceptor stack.

别忘了在struts.xml里声明interceptor,把它插入你interceptor栈中。

struts.xml snippet

<interceptor name="export" class="com.whatever.interceptors.ActionPropertyExportInterceptor" />
. . .
<interceptor-stack name="standard-interceptors">
<interceptor-ref name="timer" />
<interceptor-ref name="logger" />
<interceptor-ref name="params" />
* <interceptor-ref name="export"/>*
<interceptor-ref name="validateParams"/>
<interceptor-ref name="awarePlugger" />
</interceptor-stack>

Your action need to provide getters for all properties that should be exported into the request attribute set.

你的action中需要未被放入request范围的属性提供get方法

class ViewUser

public class ViewUser extends ActionSupport {
private int id;
private User user;

public String execute() throws Exception {
user = findUser( getId() );
return Action.SUCCESS;
}

public int getId() {return id;}
public void setId(int id) {this.id = id;}
* public User getUser() {return user;}*

private User findUser(int id) {...}
}

The User class might look like this

User类可能如下所示:

class User

import java.util.Date;
public class User {
private int id;
private String firstName, lastName, email;
private String street, zip, city;
private Date date;

public String getFirstName() {return firstName;}
//..._getters and setters_...
}

Finally, using the samples above you can write your JSP2 page like this.

最后,通过上面的例子,你可以写自己的jsp2,类似下面:

ViewUser.jsp

<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>Info about ${user.firstName}</title>
</head>
<body>
<h1>Info about ${user.firstName} ${user.lastName} [OS:ID=${user.id}]</h1>
<table border="1" cellspacing="0" cellpadding="2" width="90%" >
<tr>
<th>Name</th> <td>${user.firstName} ${user.lastName}</td>
</tr>
<tr>
<th>Created</th> <td><fmt:formatDate value="${user.date}" pattern="yyyy-MM-dd HH:mm"/></td>
</tr>
<tr>
<th>Email</th> <td>${user.email}</td>
</tr>
<tr>
<th>Address</th> <td>${user.street} ${user.zip} ${fn:toUpperCase(user.city)}</td>
</tr>
</table>
</body>
</html>

Displaying validation errors with JSTL

<c:if test="${!empty fieldErrors || !empty actionErrors}">
<div class="red">
<ul>
<c:forEach items="${fieldErrors}" var="fieldError">
<c:forEach items="${fieldError.value}" var="error">
<li>${error}</li>
</c:forEach>
</c:forEach>
<c:forEach items="${actionErrors}" var="actionError">
<li>${actionError}</li>
</c:forEach>
</ul>
</div>
</c:if>

posted on 2007-04-29 09:39 MingIsMe 阅读(994) 评论(0)  编辑  收藏 所属分类: Struts2学习

 
Powered by:
BlogJava
Copyright © MingIsMe