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



猜你喜欢

转载自sillycat.iteye.com/blog/1547292
今日推荐