`
sillycat
  • 浏览: 2542666 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Roo Docs(1) Wedding Sample

 
阅读更多
Spring Roo Docs(1) Wedding Sample

Chapter2. Beginning With Roo: The Tutorial
2.2 Alternative Tutorial: The Wedding RSVP Application
Creating a Persistent Project
>mkdir wedding
>cd wedding
>roo
roo>project --topLevelPackage com.sillycat.wedding
roo>jpa setup --provider HIBERNATE --database HYPERSONIC_PERSISTENT
The database file is stored in ~/wedding.* by default. Here is the command to show them.
roo>database properties list
roo>database properties set --key database.url --value jdbc:hsqldb:wedding
It is ok to change this file directly with IDE. But it want to rebuild the project with filename.roo. You can use ROO to change the properties value.

Creating an Entity
roo>entity jpa --class com.sillycat.wedding.models.Rsvp
.aj file, in a nutshell, there are AspectJ inter-type declarations(ITDs)

roo>field string --fieldName code --notNull --sizeMin 1 --sizeMax 30
roo>field string --fieldName email --sizeMax 30
roo>field number --fieldName attending --type java.lang.Integer
roo>field string --fieldName specialRequests --sizeMax 100
roo>field date --fieldName confirmed --type java.util.Date

--notNull --sizeMin --sizeMax argument refer to the new Bean Validation standard, which is otherwise known as JSR 303.
And this particular standard offers automatic web and persistence tier validation, including creating the correct DDL for the tables.

Proving It Works: JUnit, Web Tier and Selenium
JUnit Integration test
roo>test integration

This integration test will verify the common JPA operations like persist, remove, find, merge and etc.

Add web tier as well
roo>controller scaffold ~.web.RsvpController

This web tier provided by Roo builds on Spring Framework3's excellent REST support. And we can use selenium test our web tier.

roo>selenium test --controller ~.web.RsvpController

we can run the JUnit test with command
roo>perform tests

quit the roo and start tomcat with maven command
>mvn tomcat:run
>mvn selenium:selenese

Security and Logging
We will fine-tune the Log4J-based logging.
roo>logging setup --package WEB --level DEBUG

At present anyone can access our web site and use the exsting RESTful administrative backend to create, update and delete RSVPs.

According to requirements, we wanted to use invitations codes to ensure only invited guests can RSVP.

>security setup

Manual Controllers, Dynamic Finders and Email Support
Creat a manual controller with command
roo>controller class --class ~.web.PublicRsvpController

Our objective is to retrieve the correct RSVP for a particular invitation code. We will treat each invitation code as a unique login name. Dynamic Finder. JPA QL.
roo>finder list --class ~.models.Rsvp --filter code

Add this finder to our system
roo>finder add --finderName  findRsvpsByCodeEquals

prepare the email sender
roo>email sender setup --hostServer smtp.gmail.com --username 4myhappylife@gmail.com --password password --port 587 --protocol SMTP
roo>field email template --class ~.web.PublicRsvpController

Final Steps
Change the file applicationContext-security.xml to enable the security check.
<http auto-config="true" use-expressions="true">
<form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
        <logout logout-url="/resources/j_spring_security_logout" />
        <!-- Configure these elements to secure URIs in your application -->
        <intercept-url pattern="/rsvps/**" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/login**" access="permitAll" />
        <intercept-url pattern="/publicrsvp/**" access="permitAll" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/**" access="isAuthenticated()" />
    </http>
    <!-- Configure Authentication mechanism -->
    <authentication-manager alias="authenticationManager">
        <!-- SHA-256 values can be produced using 'echo -n your_desired_password | sha256sum' (using normal *nix environments) -->
        <authentication-provider>
            <user-service>
                <user name="admin" password="ignored" authorities="ROLE_ADMIN" />
                <user name="user1" password="ignored" authorities="ROLE_USER" />
                <user name="user2" password="ignored" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

Spring Security does't realize we're ignoring passwords, so we need to edit the
src/main/webapp/WEB-INF/views/login.jspx and an <input name="j_password" type="hidden" value="ignored" /> line within the form.
<div>
      <input id="j_password" type='hidden' name='j_password' value='ignored' />
</div>

Setting up email codes, and lookup code and create and fetch Rsvp in PublicRsvpController.java
@RequestMapping
public String get(ModelMap modelMap) {
modelMap.put("rsvp", getRsvp());
return "publicrsvp/publicrsvp";
}

@RequestMapping(method = RequestMethod.POST)
public String post(@ModelAttribute("rsvp") Rsvp rsvp, ModelMap modelMap) {
rsvp.setConfirmed(new Date());
if (rsvp.getId() == null) {
rsvp.persist();
} else {
rsvp.merge();
}
if (rsvp.getEmail().length() > 0) {
sendMessage("Carl <magic_dreamer@126.com>",
"RSVP to our wedding", rsvp.getEmail(),
"Your RSVP has been saved: " + rsvp.toString());
}
modelMap.put("rsvp", rsvp);
return "publicrsvp/thanks";

}

private Rsvp getRsvp() {
Rsvp rsvp = new Rsvp();
try {
String code = SecurityContextHolder.getContext()
.getAuthentication().getName();
rsvp.setCode(code);
rsvp = (Rsvp) Rsvp.findRsvpsByCodeEquals(code).getSingleResult();
} catch (DataAccessException ignored) {

}
return rsvp;
}

Copy and rename the template publicrsvp/index.jspx to thanks.jspx
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags"
xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<div>"Your RSVP has been confirmed: ${rsvp}".</div>
</div>

Copy and rename the template rsvp/create.jspx to publicrsvp/publicrsvp.jspx
<form:create id="fc_com_sillycat_wedding_models_Rsvp"
modelAttribute="rsvp" path="/publicrsvp" render="${empty dependencies}"
z="ljAD4P5uXRFqRI8AFiu0rOIXndU=">
<field:input field="code" id="c_com_sillycat_wedding_models_Rsvp_code"
max="30" min="1" required="true" z="lVc2JbMBUNmIMqGIuXeedt0RnDY=" />
<field:input field="email"
id="c_com_sillycat_wedding_models_Rsvp_email" max="30"
validationMessageCode="field_invalid_email"
z="xyUIdENd33YA6sFXUYBJInPPQ9c=" />
<field:input field="attending"
id="c_com_sillycat_wedding_models_Rsvp_attending"
validationMessageCode="field_invalid_integer"
z="51w2MRa7wL+mnX12lrVWCFESVxI=" />
<field:textarea field="specialRequests"
id="c_com_sillycat_wedding_models_Rsvp_specialRequests"
z="F1IVYZ4BIBHNm3DpKj8FFeNPTMs=" />
</form:create>
And change the views.xml under publicrsvp
<definition extends="default" name="publicrsvp/thanks">
<put-attribute name="body"
value="/WEB-INF/views/publicrsvp/thanks.jspx" />
</definition>
<definition extends="default" name="publicrsvp/publicrsvp">
<put-attribute name="body"
value="/WEB-INF/views/publicrsvp/publicrsvp.jspx" />
</definition>
The very last step is to edit index.jspx to link our homepage to our publicrsvp
<a href="publicrsvp">Enter the System</a>

references:
http://blog.springsource.com/2009/05/27/roo-part-2/

books:
spring-roo-docs.pdf

分享到:
评论

相关推荐

    spring-roo-docs

    ### SpringRoo-ReferenceDocumentation 1.2.5.RELEASE 关键知识点解析 #### 一、SpringRoo简介 **1.1 SpringRoo是什么?** SpringRoo是一款旨在提高开发效率的工具,它能够帮助开发者快速搭建基于Spring框架的...

    os-springroo2-sample_code

    【os-springroo2-sample_code】项目是一个关于Spring Roo的示例代码库,它展示了如何使用Spring Roo框架来快速开发应用程序。Spring Roo是Spring框架的一部分,它提供了一种简化和加速Java应用开发的方式,通过自动...

    Spring Roo In Action

    Spring Roo是一个轻量级的Java开发工具,用于快速搭建Spring应用。它采用了一种新的方式来开发Spring应用程序,即通过命令行而非传统的集成开发环境(IDE)。Spring Roo提供了一系列命令,使得开发者可以轻松地创建...

    Spring Roo 简介,第 4 部分: 用 Spring Roo 和 Cloud Foundry 在云中快速开发应用程序

    1. **代码生成**:Spring Roo 可以自动生成常见的 Java 代码,包括实体类、DAO、服务层和控制器,大大减少了手动编码的工作量。 2. **快速原型**:通过命令行界面,开发者可以迅速创建和配置项目,实现快速原型设计...

    spring roo使用文档

    ### Spring Roo 使用文档知识点概述 #### 一、Spring Roo 简介 - **Spring Roo** 是一个基于命令行的工具,旨在简化 Spring 应用程序的开发过程。 - **功能定位**:它通过提供一系列预定义的命令来加速应用程序的...

    spring roo in action

    Spring Roo是一个用于快速开发Java应用程序的框架,它结合了Spring生态系统的强大功能,尤其是对Spring MVC、Spring Security、Spring Tiles、Spring Web Flow以及Spring测试支持等方面。 Spring Roo利用了一种...

    Spring ROO

    **Spring ROO详解** Spring ROO是Spring框架下的一个快速开发工具,旨在简化Java应用程序的构建过程,尤其针对企业级应用。它通过自动化任务、代码生成以及最佳实践的应用,极大地提高了开发效率。Spring ROO的核心...

    Spring Roo命令文档

    **Spring Roo命令文档** Spring Roo是Spring框架的一部分,它提供了一种快速开发工具,帮助开发者在Java应用中创建和管理代码。Roo通过自动化过程,简化了常见的开发任务,如设置项目结构、创建实体类、生成数据库...

    spring roo 1.1.3 学习资料

    1. **自动代码生成**:Spring Roo可以根据用户的指令自动生成Java类、配置文件和视图层代码。例如,它可以生成JPA实体、DAO、Service层接口及其实现,以及基于Thymeleaf或JSP的视图页面,极大地提高了开发效率。 2....

    SpringRoo 官方文档-版本 2.0.0.RC1

    **SpringRoo 版本 2.0.0.RC1** 是一个重要的里程碑,标志着 SpringRoo 向更加成熟和强大的方向发展。这一版本在多个方面进行了改进,包括增强的可扩展性、改进的用户体验以及更紧密地与Spring技术栈集成。 #### 二...

    spring-roo-2.0.0.RC1.zip

    这个"spring-roo-2.0.0.RC1.zip"压缩包包含的是Spring Roo的2.0.0 Release Candidate 1版本,这是一个预发布版本,意味着它是对正式版本的接近最终测试,开发者可以提前体验新功能并提供反馈。 Spring Roo的核心...

    spring-roo-1.1.5.RELEAS

    1. `spring-roo-1.1.5.RELEASE.jar`:Spring Roo的主要可执行文件,包含了所有必需的类和库。 2. `lib/` 目录:包含Spring Roo运行所需的第三方库和依赖。 3. `docs/` 目录:可能包含用户指南、API文档和其他帮助...

    spring-roo-1.1.0.M1.zip_54587.m1_M1 ssh_Spring Roo download_spri

    "spring-roo-1.1.0.M1.zip_54587.m1_M1 ssh_Spring Roo download_spri"这个标题暗示了这是一个关于Spring Roo的早期版本下载,具体为1.1.0的M1( Milestone 1)迭代。M1是软件开发中的一个里程碑版本,通常在正式...

    spring roo 生成数据库表

    Spring Roo是Spring框架家族中的一个工具,用于加速Java开发,特别是企业级应用的构建。它通过命令行界面或集成开发环境(IDE)插件提供了一种快速开发的方式,可以帮助开发者生成代码、设置依赖和配置,使得开发...

    springroo快速学习

    1. 创建项目:使用`roo new`命令初始化一个新的Spring项目。这会生成一个基本的Maven或Gradle项目结构,包括必要的Spring配置文件和默认的包结构。 2. 添加依赖:SpringRoo允许使用`roo add dependency`命令快速添加...

    spring-roo-1.3.2.zip

    1. **Spring框架集成**:Spring Roo是构建在Spring框架之上,它允许开发者利用Spring的强大功能,如依赖注入、AOP(面向切面编程)和数据访问层等,同时简化了配置和代码编写。 2. **代码生成**:Spring Roo可以...

    Spring Roo - Reference Documentation

    ### Spring Roo - 高效开发框架的深度解析 #### 引言:Spring Roo与生产力的飞跃 Spring Roo作为Spring框架的扩展,旨在显著提升开发者在构建企业级Java应用时的效率。它通过自动化代码生成、简化项目搭建以及提供...

    Spring Roo 简介

    - ***nix**:创建指向 `$ROO_HOME/bin/roo.sh` 的符号链接(例如,使用 `sudo ln -s ~/spring-roo-1.x.x/bin/roo.sh /usr/local/bin/roo`)。 ##### 构建过程 1. **打开终端或命令提示符**。 2. **导航到 Spring ...

    vaadin-springRoo可运行的例子

    Vaadin和Spring Roo是两个强大的Java开发框架,它们在创建高效、用户友好的Web应用程序方面发挥着重要作用。这个"vaadin-springRoo可运行的例子"是一个整合了这两个框架的实际项目,提供了完整的war包和源代码,使得...

Global site tag (gtag.js) - Google Analytics