- 浏览: 633378 次
- 性别:
- 来自: 北京
最新评论
-
2047699523:
java hibernate demo使用实例教程源代码下载: ...
hibernate延迟加载的原理与实现 -
在世界的中心呼喚愛:
hantsy 写道这种lazy的解释只对了一半,按java p ...
hibernate延迟加载的原理与实现 -
moguicy:
看了下时间,不是楼主是否还在开发
【翻译】Wicket启示录——理论与实践(一) -
xfan0828:
"最后但并不是最不重要的一点就是" BZ, ...
【翻译】深入浅出 EJB3.1(上) -
doudou87323:
十分感谢,正在学习中,受益匪浅
【翻译】深入浅出 EJB3.1(上)
这是本文的最后部分,主要介绍的SmartURLs的重定向与安装。作者也表示在将来的Struts2版本中,SmartURLs会集成在Code-Behind中去的。下周有时间的话,可能会给大家推荐在ThoughtWorks工作的Dennis Byrne 最新发表的《JSF Anit-Patterns and Pitfalls》(JSF反模式与陷阱)
正文:
使用SmartURLs提交表单后,还可以重定向吗?
当表单通过验证后,我们会将其更新到数据库中去,然后重定向到一个确认页面去。重定向有一个重要的特点就是:重定向后的页面无法再刷新了。
Struts2是通过使用@Result Annotation来支持重定向的。如果需要用重定向,在你的Action使用这个Annotation即可。下面我们来看看“Example 7: HelloWorld.java (3)”中的使用方法
"Example 7: HelloWorld.java (3)"
@Result(name="success", type="redirect", location="hello-world-view") public class HelloWorld extends ActionSupport
在这个例子里,我们还加了一个“hello-world-view”Action。因此,我们只需要简单的将hello-world.jsp重命名一下,叫“hello-world-view.jsp”即可。
如果Action成功返回的话,那么系统就会通过我们的HelloWorldAction重定向到“hello-world-view.jsp”上去,然后显示出先前在“hello-world-input”Action中输入的那个“greeting”文本框内容。
默认情况下,Action的那些设置的property在request的作用域是可以保留下来的。但如果你要用重定向,我们就会开启一个新的request,因此那些property是取不到的。(注意:这里指的是property只是简单的set,get在Action中,并不是设置在Session或Appliation作用域中)。眼下最简单的解决方案就是将property存在session域中,现在让我们完成HelloWorld.java代码,请看"Example 8: HelloWorld.java (4)".
"Example 8: HelloWorld.java (4)"
package actions; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validator.annotations.*; import org.apache.struts2.interceptor.SessionAware; import org.texturemedia.smarturls.Result; @Validation() @Result(name="success", type="redirect-action", location="hello-world-view") public class HelloWorld extends ActionSupport implements SessionAware { private Map<String, String> session; public void setSession(Map value) { session = value; } protected Map<String, String> getSession() { Map<String, String> value = session; return value; } public static String GREETING_KEY = "greeting"; @RequiredStringValidator(message="Please enter a greeting!") public String getGreeting() { return (String) getSession().get(GREETING_KEY); } public void setGreeting(String value) { getSession().put(GREETING_KEY,value); } }
好了,打开浏览器,输入http://localhost:8080/smartapp/hello-world-input吧。当页面提交后,“hello-world”会将我们的property存于session作用域中,然后再重定向到“hello-world-view”。请看我们的“hello-world-view.jsp”。
"Example 9: hello-world-view.jsp"
<html> <body> <p> Hello World! </p> <p> ${greeting} </p> <p> <a href="hello-world-input.do">Try again!</a> </p> </body> </html>
(注意:在Struts2.0.11以后的版本,是不能直接用EL了)
好了,到这一步时,我们完整的HelloWorld的工作流程结束了。以下是涉及到的三个文件:
- "Example 6: hello-world-input.jsp",
- "Example 7: HelloWorld.java (3)", and
- "Example 4: hello-world-view.jsp"
但我也确实兑现了——没有一句XML代码。
怎么安装SmartURLs呢?
第一步:将Struts2和SmartURLs的jar包放在项目的lib下。
第二步:Struts2的Filter改成SmartURLs风格。请看"Example web.xml"
"Example 8: web.xml"
<filter> <filter-name> struts2 </filter-name> <filter-class> org.texturemedia.smarturls.SmartURLsFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern> /* </url-pattern> </filter-mapping>
第三步:修改struts.xml,增加几个“constant”结点,请看"Example 9: struts.xml".
"Example 9: struts.xml"
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <constant name="struts.devMode" value="false" /> <constant name="struts.action.extension" value="" /> <constant name="smarturls.action.packages" value="actions" /> <constant name="smarturls.base.result.location" value="/WEB-INF/results/" ></constant> <constant name="struts.custom.i18n.resources" value="support.package" ></constant> </struts>
怎么说呢,一般来说一个大的应用程序通过XML定义一些常量设置以及默认包配置是需要的,但绝对不像以前的Struts/Struts2,那些有那么多过度使用的XML配置。
SmartURLs的最小安装配置说明。
如果你有MyEclipse或Eclispe的WTP,可以很快建一个Dynamic Web Application项目。然后按照以下步骤即可:
只要Struts2几个必须依赖的核心包(freemarker, ognl, struts2-core, xwork2),然后SmartURLs插件包(java-net-commons , smarturls-s2).放入。
在WebContent或WebRoot下的WEB-INF文件夹下建立“results”文件夹来存放你的页面模板。
在src文件夹下,建立“Example 9”所说的struts.xml以及,再建立一个“actions”文件夹,作为你所有Struts2的Action根目录
完成后,可以开始了。
所有的就这么多吗?
我们刚才通过使用SmartURLs插件,已经算是最简化的使用Struts2了。最简单的方法也会同样也会让我们使用最少量的Annotation。除了Annotation外,SmartURLs还支持很多特性。
比如,我们可以给Action定义多个别名,或者调用Action的方法不一定就是“execute”。
SmartURLs文档有很多关于Annotation以及一些配置选项的说明。为进一步演示SmartURLs,一个完整的基于SmartURLs的MailReader应用程序实际可以随时下载布署。(下面有下载链接)
使用SmartURLs的惯例约定。我们只需要写少量代码就可快速创建一个强大的企业应用程序。节省下来的时间,也就可以更加投入到用户接口的改进或应用程序其它方方面面中去。
目前,SmartURLs还是作为Struts2的一个第三方插件,但在Struts2.1版本中会与标准的CodeBehind插件合并。当前的CodeBehind也有类似功能,但相对较弱。
相关资源
- SmartURLs plugin site (http://code.google.com/p/smarturls-s2/)
- SmartURLs MailReader Example for Java 5 (http://husted.com/smart-urls/smart-urls.war)
- Apache Struts site (http://struts.apache.org)
- smartapp.rar (3.8 MB)
- 描述: 最后一小节的源代码。运行后,可参见Readme.txt
- 下载次数: 193
发表评论
-
【翻译】深入浅出 EJB3.1(下)
2009-05-07 23:37 8805Global JNDI names( 统一的 全局 ... -
【翻译】深入浅出 EJB3.1(上)
2009-05-06 20:59 10990终于有空将EJB3.1的最新文章与大家分享,原文请看: ht ... -
【翻译】Java EE 6体系结构的变革
2009-02-09 10:01 4436又看到 Reza 同学为 -
【翻译】Java EE 6体系结构的变革(完)
2009-02-09 10:00 6402JSF 2.0 尽 ... -
垃圾收集器是一个“宝贝收藏家”?
2008-11-25 23:58 2987原文请看:http://java.dzone.com/ar ... -
深入理解JBoss Cache3.0——Naga
2008-11-12 09:12 9643原文请看: http://java.dzone.com/ ... -
【翻译】Rod Johnson——关于当选JCP执行委员会的之言片语
2008-11-03 10:42 3125SpringSource 在上月底被宣布被加入 JCP ... -
【翻译】EJB3.1真的来了吗?EJB3.1系列文章(五) 终章
2008-10-16 14:37 9073历时9 个多月的EJB3.1 系列文章终于要划上圆满的句 ... -
【翻译】Rod Johnson——平衡的质疑:Spring维护策略的再次调整(完)
2008-10-09 09:33 3962不管你承不承认,Spring实际上已经是实事上JAVA企业开 ... -
【云计算专家Joseph Ottinger系列】应用服务器本质论
2008-09-08 08:58 3421原文请看: http://www.t ... -
Spring破坏了JEE规范吗?
2008-09-02 13:33 4035[TTS 编辑注:这是 TTS 论坛上的原帖。我现在把它 ... -
【翻译】spring配置全书(下)——附PDF完整版下载
2008-07-14 12:30 10743JMS 命名空间简介 Schema URI ... -
【翻译】spring配置全书(上)
2008-07-07 23:11 8867作者简介: Craig Walls 是 Texa ... -
【翻译】EJB3.1真的来了吗?EJB3.1系列文章(四)
2008-06-18 23:10 38134前言 Raza 同学终于又出 EJB3.1 文章了 ... -
【翻译】Rod Johnson——Spring的宣言:开源,开放(完)
2008-06-10 16:51 5557前言 这是本文的第二部分,里面提到并回答许多Spring用 ... -
【翻译】Rod Johnson——Spring的宣言:开源,开放
2008-06-06 13:06 6038原文地址: http://blog.sp ... -
【翻译】EJB3.1真的来了吗?EJB3.1系列文章(三)
2008-05-01 13:53 8712文本继续和大家分享EJB3.1特性,今天谈到的EJB Lite ... -
【Danny hui】运用抽象工厂模式自己动手写一个IoC
2008-04-23 16:34 6274本文的作者Danny hui似乎是TTS上的新人,我从Goog ... -
【翻译】Wicket启示录——理论与实践(三)完
2008-04-11 01:09 6172接下来,我们再看看EditContact类,把新建联系人的话和 ... -
【翻译】Wicket启示录——理论与实践(二)
2008-04-09 23:47 5813第二部分 实践 Application(应用程序) 与 ...
相关推荐
可能因为包有点大了,始终传不上来,要用到的包用《【Struts专家Ted Husted系列】Struts2其实也可以CoC风格编程(一)》的附件中的lib即可,它们用的是一样的。 博文链接:https://superleo.iteye.com/blog/165976
我已经将这个实例子写在这里了。 博文链接:https://superleo.iteye.com/blog/165590
Struts在行动,使用领先的JAVA框架构建Web应用。 Ted Husted Cedric Dumoulin George Franciscus David Winterfeldt(著) Eric Lin(译) 注:本文仅用于个人研究,如有需要请购买正版。
此书详细介绍了 Struts 框架集合的相关技术
Ted Husted Cedric Dumoulin George Franciscus David Winterfeldt (著) Eric Liu (译) 分为八个章节,详细介绍了MVC和struts的原理。语言浅显直白,适合初学者看。
Struts在行动 使用领先的Java框架构建Web应用 Ted Husted Cedric Dumoulin George Franciscus David Winterfeldt (著) Eric Liu (译)
struts入门经典教材,Ted Husted 著
Struts In Action中文版 Ted Husted著 Eric Liu(铁手)译
Ted Husted 著 Eric Liu(铁手) 译 中文版 ------------------------------------------- 本书详细的记录了Structs的知识。相信对你学习理解Structs有很大的帮助,希望通过这本书可以让你学好Structs技术。
Struts In Action 使用领先的Java框架构建Web应用 中文版 Ted Husted 著 Eric Liu(铁手) 译
Struts in Action 中文版 Ted Husted 著 Eric Liu(铁手) 译
This document describes step-by-step how to write software for the TED-Kit 2 and its components using the API Library
ios mod menu template by ted