`
kyo100900
  • 浏览: 633378 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【Struts专家Ted Husted系列】Struts2其实也可以CoC风格编程(三)完

阅读更多

这是本文的最后部分,主要介绍的SmartURLs的重定向与安装。作者也表示在将来的Struts2版本中,SmartURLs会集成在Code-Behind中去的。下周有时间的话,可能会给大家推荐在ThoughtWorks工作的Dennis Byrne 最新发表的《JSF Anit-Patterns and Pitfalls》(JSF反模式与陷阱)smile

正文:

使用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-viewAction。因此,我们只需要简单的将hello-world.jsp重命名一下,叫“hello-world-view.jsp”即可。

 

如果Action成功返回的话,那么系统就会通过我们的HelloWorldAction重定向到“hello-world-view.jsp”上去,然后显示出先前在“hello-world-inputAction中输入的那个“greeting”文本框内容。

 

默认情况下,Action的那些设置的propertyrequest的作用域是可以保留下来的。但如果你要用重定向,我们就会开启一个新的request,因此那些property是取不到的。(注意:这里指的是property只是简单的set,getAction中,并不是设置在SessionAppliation作用域中)。眼下最简单的解决方案就是将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呢?

 

第一步:将Struts2SmartURLsjar包放在项目的lib下。

第二步:Struts2Filter改成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的最小安装配置说明。

如果你有MyEclipseEclispeWTP,可以很快建一个Dynamic Web Application项目。然后按照以下步骤即可:

只要Struts2几个必须依赖的核心包(freemarker, ognl, struts2-core, xwork2),然后SmartURLs插件包(java-net-commons , smarturls-s2).放入。

 

WebContentWebRoot下的WEB-INF文件夹下建立“results”文件夹来存放你的页面模板。

 

src文件夹下,建立“Example 9”所说的struts.xml以及,再建立一个“actions”文件夹,作为你所有Struts2Action根目录

 

完成后,可以开始了。

所有的就这么多吗?

 

我们刚才通过使用SmartURLs插件,已经算是最简化的使用Struts2了。最简单的方法也会同样也会让我们使用最少量的Annotation。除了Annotation外,SmartURLs还支持很多特性。

比如,我们可以给Action定义多个别名,或者调用Action的方法不一定就是“execute”。

 

SmartURLs文档有很多关于Annotation以及一些配置选项的说明。为进一步演示SmartURLs,一个完整的基于SmartURLsMailReader应用程序实际可以随时下载布署。(下面有下载链接)

 

使用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
14
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics