`
congpeixue
  • 浏览: 269848 次
  • 性别: Icon_minigender_1
  • 来自: ...
社区版块
存档分类
最新评论

Click framework 的 Page类(四)

阅读更多
Template Path

跳转到新页面的另一种可选方法是设置要被呈现的页面模板的路径。 使用这种方式需确保模板
呈现前, 模板所需要的任何东西都必须创建完毕。

我们的改进过的例子如下:

public boolean onViewClick() {
   Long id = viewLink.getValueLong();
   Customer customer = CustomerDAO.findByPK(id); 
    
   addModel("customer", customer);
   
   // Set the Page's path to a new value
   setPath("view-customer.htm");
   
   return false;
}


Node: customer 对象是如何传递到模板中的。 当你迁移到另一个页面时, 这种使用Page
model的方法是不合法的 , 因为在第二个Page对象被创建前, 第一个Page对象已被销毁, 任何model值都已被丢失。

Redirect

Redirects是页面间导航的另一种方式,参考HttpServletResponse.sendRedirect(location)
获得更详细的资料。
redirects 所做的最大的事情是它在用户浏览器里提供了一个准确的URL,该URL用于匹配将要
呈现的页面。
redirects 的缺点是: 当请求一个页面时, 用户浏览器会创建一个往返的通信通道。 这样做
不仅耗费时间, 而且所有page和request信息将被丢失。

一个重定向到 logout.htm 的例子如下:

public boolean onLogoutClick() {
   setRedirect("/logout.htm");
   return false;
} 


如果重定向位置以“/”字符串开头, 将会在重定向位置的前面加上程序上下文的路径。
例如: 当一个部署到上下文为mycorp的程序调用setRedirect("/customer/details.htm") 时,
将会把请求重定向到“"/mycorp/customer/details.htm”。


你也可以通过目标Page的class来获取重定向的路径。 例如:
public boolean onLogoutClick() {
   String path = getContext().getPagePath(Logout.class);
   setRedirect(path);
   return false;
} 


当使用这种重定向方法时, 需确保目标Page class对应唯一的一个路径。

使用重定向到一个简单方式是在redirect 方法中直接指定要重定向的Page class。 例如:

public boolean onLogoutClick() {
   setRedirect(Logout.class);
   return false;
} 



Redirect Parameter Passing


你可以使用URL 请求参数在重定向的页面间传递信息。 ClickServlet 会使用 HttpServletResponse.encodeRedirectURL
自动地编码URL。

在这个例子中, 用户点击一个OK按钮来确认一次支付。 onOkClick()按钮处理器获得支付
交易id, 然后使用此经过URL编码的id重定向到 trans-complete.htm。
public class Payment extends Page {
   ..
   
   public boolean onOkClick() {
      if (form.isValid()) {
         // Process payment
         ..
         
         // Get transaction id
         Long transId = OrderDAO.purchase(order);
   
         setRedirect("trans-complete.htm?transId=" + transId);
      
         return false; 
      } 
  
      return true;
   }
} 


对应trans-complete.htm 的Page类可以通过请求参数transId来获得此次交易id。


public class TransComplete extends Page {
   
   /**
    * @see Page#onInit()
    */
   public void onInit() {
      String transId = getContext().getRequest().getParameter("transId");
   
      if (transId != null) {
      
         // Get order details
         Order order = OrderDAO.findOrderByPK(new Long(transId));
         if (order != null) {
            addModel("order", order);
         }
      }
   }
}


Post Redirect

上面的参数传递例子也是一个Post 重定向例子。Post重定向对防止用户通过点击刷新按钮来
提交表单是非常重要的。


Page Templating

Click支持Page Templating, 它可以帮助你在你个web程序中建议一个标准的试图并能
大大减少你需要维护的html代码量。

为了实现模板功能, 定义了一个基于Page的框架模板。 该基于Page的模板类覆写了Page的 getTemplate()
方法, 放回框架模板的路径。 例如:
public class BorderedPage extends Page {

    /**
     * @see Page#getTemplate()
     */
    public String getTemplate() {
        return "/border.htm";  
    }
} 


BorderedPage 模板border.htm:

<html>
   <head>
     <title>$title</title>
     <link rel="stylesheet" type="text/css" href="style.css" title="Style"/>
   </head>
   <body>
 
     <h2 class="title">$title</h2>
 
     #parse($path)
 
   </body>
 </html> 

其它页面使用Velocity的 #parse指令将它们的上下文添加到此模板中,$path的值会自动地被ClickServlet
添加到VelocityContext 中。

如下提供了一个例子:

引用
<page path="home.htm" classname="Home"/>



public class Home extends BorderedPage {
   
     public String title = "Home";

}


Home页面的内容home.htm
<b>Welcome</b> to Home page your starting point for the application.


当Homepage 接到一个请求时(home.htm), Velocity会将 border.htm 和home.htm融合在一起并返回。

 <html>
   <head>
     <title>Home</title>
     <link rel="stylesheet" type="text/css" href="style.css" title="Style"/>
   </head>
   <body>
 
     <h2 class="title">Home</h2>
 
     <b>Welcome</b> to Home page your application starting point.
 
   </body>
 </html> 



Note :Home page定义一个被border.htm模板引用的title模型的方式。

使用相同的方式也支持Jsp模板, 请参考Click Examples 提供了例子。

分享到:
评论

相关推荐

    Apache Click User Guide

    Apache Click is a simple JEE web application framework for commercial Java developers. Apache Click is an open source project, licensed under the Apache license . Click uses an event based programming...

    click-2.3.0.zip

    Click is a J2EE web application framework featuring a Page and Component oriented design. Click is an open source project licensed using the Apache license.

    robotframework-zoomba:扩展的Robot Framework库使测试GUI,RESTSOAP API,Mobile和Windows Desktop更加容易

    RobotFramework-Zoomba 介绍 Robotframework-Zoomba是使用跨越GUI,REST API,SOAP API,移动和Windows桌面(WinAppDriver)自动化的库的集合。 这些库是现有库 , , 和。...Click Element locator

    RobotFramework常见语法.pdf

    [Documentation] 截图 Capture Page Screenshot [Arguments] ${JSContent} [Documentation] 执⾏JS Execute JavaScript ${JSContent} [Arguments] ${location} [Documentation] 选择Frame Select Frame ${location} ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    The .NET Framework Class Library (FCL) 43 C# and Other .NET Languages 45 The Common Type System (CTS) 46 The Common Language Specification (CLS) 46 Summary 46 2 Getting Started with C# and ...

    UiPath高级开发者认证-操作题

    2. On the landing page, Dashboard, click on the Work items menu item. Scrape the data in all the pages of the table, page by page, ensuring error handling and recovery. 3. For each page: - Filter the ...

    Java Language Conversion Assistant

    Windows 2000, Windows Server 2003, Windows XP Visual Studio .NET 2003 Instructions Click the Download button in the upper right-hand corner of this page to start the download, or choose a different ...

    微软Kinect for Windows7 SDK Beta开发工具包(32)

    On this page, click Download to start the Kinect for Windows SDK beta download. Click Run to start Setup, and follow the instructions in the Setup Wizard. Or, to save the download on your computer ...

    微软Kinect for Windows7 SDK Beta开发工具包(64)

    On this page, click Download to start the Kinect for Windows SDK beta download. Click Run to start Setup, and follow the instructions in the Setup Wizard. Or, to save the download on your computer ...

    北京中科信软 Visual Basic.NET培训

    接口和类的设计 SQL语句,存储过程 二 .NET框架&C#编程 面向对象的基本知识体系 .NET中的面向对象设计与应用 C#与设计模式 泛型 C#中的新特性 程序集 .NET应用程序部署和版本控制 委托和事件 线程 反射 ...

    Login Control

    Right-click anywhere in the pane and select "Customize Toolbox...", click ".Net Framework" tab, browse and locate JoeWoods.LoginControl.dll as you did in step 1. This will create the LoginControl ...

    VB.NET Developer's Guide(4574).pdf

    Customizing the Start Page 139 Accessibility Options 141 Summary 142 Solutions Fast Track 142 Frequently Asked Questions 143 Chapter 4 Common Language Runtime 145 Introduction 146 Component ...

    Selenium WebDriver Practical Guide 最新 原版

    Perform advanced actions such as drag-and-drop, double-click, right-click, and action builders on web pages Explore the advanced features of WebDriver such as handling cookies, taking ...

    appium-pageobjectmodel:使用Java + Maven + TestNG进行页面对象模型设计。 支持并行

    阿皮页面对象模型 使用Java + Maven + TestNG进行页面对象模型设计的Appium移动测试自动化框架。 框架遵循许多行业最佳实践,并在单个代码库中支持Android和iOS。 在“最高评分的Udemy...UI命令(例如click,sendkeys

    SignalR 2.0 and MVC 5即时通讯完整版示例

    在Visual Studio中,创建一个面向.NET Framework 4.5的C#ASP.NET应用程序,将其命名为SignalRChat,然后单击确定。 创建网页 在New ASP.NET Project对话框中选择MVC,然后点击Change Authentication。 创建网页 ...

    asp.net知识库

    体验.net2.0的优雅(四):Provider、策略、控制反转和依赖注入 泛型最佳实践 asp.net 2.0下嵌套masterpage页的可视化编辑 C# 2.0与泛型 动态调用对象的属性和方法——性能和灵活性兼备的方法 泛型技巧系列:用泛型...

    IE浏览器自定义的下载管理器(VBIEDownloadMa nager)

    Click the "Download" button on this page, and then VBWebDownloader.exe will be launched. In VBWebDownloader.exe, you will find that the url is ...

    ASP.NET3.5从入门到精通

    15.1.4 .NET Framework 绘图类 15.2 图形编程 15.2.1 Graphics 类 15.2.2 绘制基本图形 15.2.3 图形绘制实例 15.3 绘制文字特效 15.3.1 投影特效 15.3.2 倒影特效 15.3.3 旋转特效 15.4 绘制图片 15.4.1 载入图像...

    英文原版-Instant PHP Web Scraping 1st Edition

    However, web scraping can provide the tools and framework to accomplish this with the click of a button. It’s no wonder, then, that web scraping is a desirable weapon in any programmer’s arsenal....

Global site tag (gtag.js) - Google Analytics