- 爱易网页
 
                        - 
                            数据库教程
 
                        - mongodb(二)Setup the Velocity Jquery Groovy Service AOP 
 
                         
                    
                    
                    日期:2014-05-16  浏览次数:20832 次 
                    
                        
                         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;