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

mongodb(2)Setup the Velocity Jquery Groovy Service AOP

 
阅读更多
mongodb(2)Setup the Velocity Jquery Groovy Service AOP

1. Velocity with Jquery
Because I use velocity here, so I need to install the latest velocity plugin for eclipse 3.7 is
http://veloeclipse.googlecode.com/svn/trunk/update/

It is said that there is conflict when using jquery and velocity together. But I do not see any issues here.
my pom.xml is as follow to see the version:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.0.0-beta-3</version>
</dependency>

One problem to get the content path from velocity file, I solve this problem with velocity tool.
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="layoutUrl" value="layout/layout.vm" />
<property name="cache" value="false" />
<property name="suffix" value=".vm" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
</bean>

The toolbox.xml will be configurated as follow under WEB-INF, I just install one of all the tools:
<toolbox>
<tool> 
        <key>link</key> 
        <scope>request</scope> 
        <class>org.apache.velocity.tools.view.tools.LinkTool</class> 
    </tool>
</toolbox>

Velocity page will import the resources as follow:
<link rel="stylesheet" type="text/css" media="screen" href="${link.getContextPath()}/resources/css/style.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="${link.getContextPath()}/resources/js/custom.js"></script>

2. Groovy Controller and Request with JSON response
my UserController.groovy
package com.sillycat.easynosql.web;

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus

import com.sillycat.easynosql.model.Role
import com.sillycat.easynosql.model.User
import com.sillycat.easynosql.model.dto.UserListDTO
import com.sillycat.easynosql.service.UserService


@Controller
@RequestMapping("/users")
class UserController {

@Autowired
UserService userService

@RequestMapping(value="/records")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody UserListDTO getUsers() {
UserListDTO userListDTO = new UserListDTO();
userListDTO.setUsers(userService.readAll());
return userListDTO;
}

@RequestMapping(value="/get")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody User get(@RequestBody User user) {
return userService.read(user);
}

@RequestMapping(value="/create", method=RequestMethod.POST)
public @ResponseBody User create(
@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName,
@RequestParam("role") Integer role) {
Role newRole = new Role();
newRole.setRole(role);
User newUser = new User();
newUser.setUsername(username);
newUser.setPassword(password);
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
newUser.setRole(newRole);
return userService.create(newUser);
}

@RequestMapping(value="/update", method=RequestMethod.POST)
public @ResponseBody User update(
@RequestParam("userName") String userName,
@RequestParam("firstName") String firstName,
@RequestParam("lastName") String lastName,
@RequestParam("role") Integer role) {
Role existingRole = new Role();
existingRole.setRole(role);

User existingUser = new User();
existingUser.setUsername(userName);
existingUser.setFirstName(firstName);
existingUser.setLastName(lastName);
existingUser.setRole(existingRole);

return userService.update(existingUser);
}

@RequestMapping(value="/delete", method=RequestMethod.POST)
public @ResponseBody Boolean delete(
@RequestParam("username") String username) {

User existingUser = new User();
existingUser.setUsername(username);

return userService.delete(existingUser);
}

}

The request URLs will be as follow:
urlHolder.records = "${link.getContextPath()}/users/records";
urlHolder.add = "${link.getContextPath()}/users/create";
urlHolder.edit = "${link.getContextPath()}/users/update";
urlHolder.del = "${link.getContextPath()}/users/delete";

3. AOP Configuration
AOP Log class is as follow:
package com.sillycat.easynosql.aop.log;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
import org.springframework.aop.interceptor.CustomizableTraceInterceptor;
public class TraceInterceptor extends CustomizableTraceInterceptor {
private static final long serialVersionUID = 287162721460370957L;
protected static Logger logger4J = Logger.getLogger("aop");
@Override
protected void writeToLog(Log logger, String message, Throwable ex) {
if (ex != null) {
logger4J.debug(message, ex);
} else {
logger4J.debug(message);
}
}
@Override
protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger) {
return true;
}
}

AOP configuration for spring xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<!-- For parsing classes with @Aspect annotation -->
<aop:aspectj-autoproxy/>
<bean id="customizableTraceInterceptor" class="com.sillycat.easynosql.aop.log.TraceInterceptor"
p:enterMessage="Entering $[targetClassShortName].$[methodName]($[arguments])"
p:exitMessage="Leaving $[targetClassShortName].$[methodName](): $[returnValue]"/>
<aop:config>
  <aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * com.sillycat.easynosql.service..*(..))"/>
</aop:config>
</beans>

log4j.properties configuration:

log4j.rootLogger=ERROR,console

#Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=log/easynosql.log
log4j.appender.R.DatePattern = '.'yyyy-MM-dd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{MM-dd HH:mm:ss} [%p] %l - %m%n

log4j.logger.aop=DEBUG,console

log4j.additivity.aop=false

references:
http://krams915.blogspot.com/2012/01/spring-mvc-31-implement-crud-with_7897.html
http://krams915.blogspot.com/2012/01/spring-mvc-31-implement-crud-with_3288.html
http://krams915.blogspot.com/2012/01/spring-mvc-31-implement-crud-with_20.html
http://krams915.blogspot.com/2012/01/spring-mvc-31-implement-crud-with.html
http://www.springsource.org/spring-data
http://www.springsource.org/spring-data/mongodb
http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/
http://code.jquery.com/jquery-1.7.2.min.js



分享到:
评论

相关推荐

    nodejs+express+mongodb+bootstrap+jquery+ejs写的电影demo

    标题中的“nodejs+express+mongodb+bootstrap+jquery+ejs写的电影demo”表明这是一个使用Node.js、Express框架、MongoDB数据库、Bootstrap前端框架、jQuery库以及EJS模板引擎开发的电影相关的应用程序示例。...

    delphi所有笔记,还有mongodb innosetup sql等

    总结来说,这些笔记涵盖了从客户端应用程序开发(Delphi)、数据库管理(MongoDB)、安装部署(InnoSetup)到数据管理(SQL)的广泛知识,对于希望全面理解软件开发流程的开发者来说是非常宝贵的资源。通过深入学习...

    基于java语言,使用Springboot + Mongodb + Groovy + Es等框架搭建的轻量级实时风控引擎

    2. **MongoDB**: MongoDB是一个NoSQL数据库,适合处理大量非结构化和半结构化数据。在风控系统中,可能需要存储用户行为、交易记录等多种异构数据,MongoDB的灵活性和高扩展性使其成为理想选择。 3. **Groovy**:...

    groovy配置MongoDB所需jar包

    groovy配置MongoDB所需jar包 1.gmongo-1.5.jar 2.mongo-java-driver-3.4.0-rc1.jar 3.mongodb-driver-3.4.0-rc1.jar

    MongoDB: The Definitive Guide

    MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable general­purpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...

    MongoDB The Definitive Guide 2011 中文+英文

    MongoDB: The Definitive Guide by Kristina Chodorow and Michael Dirolf Copyright © 2010 Kristina Chodorow and Michael Dirolf. All rights reserved.

    mongodb在Linux的配置文件

    本篇将详细介绍MongoDB在Linux上的配置文件,日志配置以及服务(service)配置。 1. MongoDB配置文件: MongoDB的主要配置文件名为`mongod.conf`,通常位于 `/etc/mongodb/` 或 `/etc/mongod.conf` 目录下。这个文件...

    mongodb 2

    mongodb 2

    springmvc mongodb jquery 实现用户操作

    在IT行业中,SpringMVC、MongoDB和jQuery是三个非常重要的技术组件,分别在Web开发的后端、数据库管理和前端交互方面发挥着关键作用。在这个项目中,"springmvc mongodb jquery 实现用户操作",我们将深入探讨如何...

    Java调用Groovy,实时动态加载数据库groovy脚本

    2. 创建GroovyClassLoader:使用这个类加载器可以动态加载和执行Groovy脚本。它继承自Java的ClassLoader,能解析Groovy源码并生成字节码。 3. 加载并执行Groovy脚本:通过GroovyClassLoader的`parseClass()`方法...

    The Little MongoDB Book 中文版.pdf

    ### 《The Little MongoDB Book》知识点总结 #### 1. 关于版权与许可 - **许可类型**:本书采用Creative Commons Attribution-NonCommercial 3.0 Unported license(简称CC BY-NC 3.0)许可协议发布。 - **使用权限...

    MongoDB权威指南:MongoDB:The Definitive Guide

    MongoDB是一种流行的开源文档型数据库,它以其灵活性、可扩展性和高性能在现代数据存储解决方案中占据了一席之地。《MongoDB权威指南》是学习和掌握MongoDB的重要参考资料,该书涵盖了MongoDB的基础知识到高级应用,...

    gmongo, mongodb驱动程序的Groovy包装器.zip

    gmongo, mongodb驱动程序的Groovy包装器 GMongo 这个项目的目的是提供一个更加简单。易于使用和更少的冗长的API,使用Groovy编程语言来处理 mongodb 。在这里可以找到更多信息: ...

    douban_Website, 基于NodeJs MongoDB jQuery搭建的豆瓣电影音乐网站.zip

    2. **MongoDB**:MongoDB是一个NoSQL数据库,它支持灵活的数据模型,对于频繁变化的数据结构,如电影和音乐信息,MongoDB提供了很好的适应性。在这个项目中,MongoDB可能会存储电影和音乐的元数据,如标题、导演、...

    基于Nodejs+Express+MongoDB+jQuery+Bootstrap搭建的电影网站

    使用jQuery和Bootsrap完成网站前端JS脚本和样式处理; 前后端的数据请求交互通过Ajax完成; 引入了Moment.js格式化前端页面显示时间; 2、项目后端搭建: 使用NodeJs的express框架完成电影网站后端搭建; 使用mongodb...

    MongoDB The Definitive Guide 2nd 原版pdf by Chodorow

    In the last 10 years, the Internet has challenged relational databases in ways nobody could have foreseen. Having used MySQL at large and growing Internet companies during this time, I’ve seen this ...

Global site tag (gtag.js) - Google Analytics