项目结构截图如下,该项目由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"; } }
package net.exacode.bootstrap.web.controller; import net.exacode.bootstrap.web.model.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; 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.ResponseBody; /** * Service hello world controller. * Presents basic usage of SpringMVC and REST service API. * @author pmendelski * */ @Controller public class UserServiceController { private final Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping(value = "/user/{name}/{surname}.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody User getUserJson(@PathVariable String name, @PathVariable String surname) { logger.trace("Responding service request"); User user = new User(name, surname); return user; } @RequestMapping(value = "/user/{name}/{surname}.xml", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE) public @ResponseBody User getUserXml(@PathVariable String name, @PathVariable String surname) { logger.trace("Responding service request"); User user = new User(name, surname); return user; } }
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <mvc:resources mapping="/resources/**" location="/resources/" /> <context:component-scan base-package="net.exacode.bootstrap.web" /> <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="configLocation"> <value>/WEB-INF/velocity/velocity.properties</value> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="cache" value="false" /> <property name="layoutUrl" value="/layout/main.vm" /> <property name="prefix" value="/templates/" /> <property name="suffix" value=".vm" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" /> </bean> <!-- Internationalization --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:i18n/messages" /> <property name="defaultEncoding" value="UTF-8" /> </bean> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang" /> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="defaultLocale" value="en" /> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <ref bean="localeChangeInterceptor" /> </property> </bean> </beans>
然后是三个velocity的模板和属性文件的配置:
#define($content) <p>#springMessage("Hello"): $name</p> <p>#springMessage("Greetings")</p> #end
#define($content) #springMessage("Hello") #end
#set($page_title="#springMessage(‘HelloWorld‘)") #define($content) #springMessage("HelloWorld") #end
velocimacro.permissions.allow.inline=true velocimacro.permissions.allow.inline.to.replace.global=true velocimacro.permissions.allow.inline.local.scope=true input.encoding=UTF-8 output.encoding=UTF-8 resource.loader=webapp, class class.resource.loader.description=Velocity Classpath Resource Loader class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader webapp.resource.loader.path=/WEB-INF/velocity/ webapp.resource.loader.cache=false
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <servlet> <servlet-name>springDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springmvc+velocity+ Rest Services(xml,json)实例,布布扣,bubuko.com
springmvc+velocity+ Rest Services(xml,json)实例
原文:http://blog.csdn.net/phantomes/article/details/22848987