日期:2014-05-16  浏览次数:20403 次

springmvc+velocity+ Rest Services(xml,json)实例

项目结构截图如下,该项目由maven构建的web项目,实例简单,无数据库连接操作,功能演示的请求地址分别在controller包下的三个类中,本例中的请求地址为:

http://localhost:8080/spring-mvc-velocity-bootstrap/greet           --默认欢迎

http://localhost:8080/spring-mvc-velocity-bootstrap/greet/zhangsan        --欢迎某人,这里是zhangsan,可任意

http://localhost:8080/spring-mvc-velocity-bootstrap/hello   --默认hello

http://localhost:8080/spring-mvc-velocity-bootstrap/hello-world     --hello world

http://localhost:8080/spring-mvc-velocity-bootstrap/hello-redirect     --重定向到hello world请求

http://localhost:8080/spring-mvc-velocity-bootstrap/user/zhang/san.json     --请求参数为json格式

http://localhost:8080/spring-mvc-velocity-bootstrap/user/zhang/san.xml      --请求参数为xml格式


该项目为简单的springmvc+velocity+rest service,且无数据库连接操作,下面给出controller包下的请求配置,和spring的xml文件配置,和velocity的模板配置,和web.xml的加载,监听配置。


三个controller类的请求配置的代码依次为:

package net.exacode.bootstrap.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Presents how to pass some values to controller using URL.
 * 
 * @author pmendelski
 * 
 */
@Controller
public class GreetingsController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping(value = "/greet/{name}", method = RequestMethod.GET)
	public String greetPath(@PathVariable String name, ModelMap model) {
		logger.debug("Method greetPath");
		model.addAttribute("name", name);
		return "greetings";
	}

	@RequestMapping(value = "/greet", method = RequestMethod.GET)
	public String greetRequest(
			@RequestParam(required = false, defaultValue = "John Doe") String name,
			ModelMap model) {
		logger.debug("Method greetRequest");
		model.addAttribute("name", name);
		return "greetings";
	}
}

package net.exacode.bootstrap.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Simple hello world controller.
 * Presents basic usage of SpringMVC and Velocity.
 * @author pmendelski
 *
 */
@Controller
public class HelloWorldController {
	
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String hello() {
		logger.debug("Method hello");
		return "hello";
	}

	@RequestMapping(value = "/hello-world", method = RequestMethod.GET)
	public String helloWorld() {
		logger.debug("Method helloWorld");
		return "hello-world";
	}
	
	@RequestMapping(value = "/hello-redirect", method = RequestMethod.GET)
	public String helloRedirect() {
		logger.debug("Method helloRedirect");
		return "redirect:/hello-world";
	}

}